Drupal.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal.
  5. */
  6. use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
  7. use Drupal\Core\Messenger\LegacyMessenger;
  8. use Drupal\Core\Url;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * Static Service Container wrapper.
  12. *
  13. * Generally, code in Drupal should accept its dependencies via either
  14. * constructor injection or setter method injection. However, there are cases,
  15. * particularly in legacy procedural code, where that is infeasible. This
  16. * class acts as a unified global accessor to arbitrary services within the
  17. * system in order to ease the transition from procedural code to injected OO
  18. * code.
  19. *
  20. * The container is built by the kernel and passed in to this class which stores
  21. * it statically. The container always contains the services from
  22. * \Drupal\Core\CoreServiceProvider, the service providers of enabled modules and any other
  23. * service providers defined in $GLOBALS['conf']['container_service_providers'].
  24. *
  25. * This class exists only to support legacy code that cannot be dependency
  26. * injected. If your code needs it, consider refactoring it to be object
  27. * oriented, if possible. When this is not possible, for instance in the case of
  28. * hook implementations, and your code is more than a few non-reusable lines, it
  29. * is recommended to instantiate an object implementing the actual logic.
  30. *
  31. * @code
  32. * // Legacy procedural code.
  33. * function hook_do_stuff() {
  34. * $lock = lock()->acquire('stuff_lock');
  35. * // ...
  36. * }
  37. *
  38. * // Correct procedural code.
  39. * function hook_do_stuff() {
  40. * $lock = \Drupal::lock()->acquire('stuff_lock');
  41. * // ...
  42. * }
  43. *
  44. * // The preferred way: dependency injected code.
  45. * function hook_do_stuff() {
  46. * // Move the actual implementation to a class and instantiate it.
  47. * $instance = new StuffDoingClass(\Drupal::lock());
  48. * $instance->doStuff();
  49. *
  50. * // Or, even better, rely on the service container to avoid hard coding a
  51. * // specific interface implementation, so that the actual logic can be
  52. * // swapped. This might not always make sense, but in general it is a good
  53. * // practice.
  54. * \Drupal::service('stuff.doing')->doStuff();
  55. * }
  56. *
  57. * interface StuffDoingInterface {
  58. * public function doStuff();
  59. * }
  60. *
  61. * class StuffDoingClass implements StuffDoingInterface {
  62. * protected $lockBackend;
  63. *
  64. * public function __construct(LockBackendInterface $lock_backend) {
  65. * $this->lockBackend = $lock_backend;
  66. * }
  67. *
  68. * public function doStuff() {
  69. * $lock = $this->lockBackend->acquire('stuff_lock');
  70. * // ...
  71. * }
  72. * }
  73. * @endcode
  74. *
  75. * @see \Drupal\Core\DrupalKernel
  76. */
  77. class Drupal {
  78. /**
  79. * The current system version.
  80. */
  81. const VERSION = '8.5.3';
  82. /**
  83. * Core API compatibility.
  84. */
  85. const CORE_COMPATIBILITY = '8.x';
  86. /**
  87. * Core minimum schema version.
  88. */
  89. const CORE_MINIMUM_SCHEMA_VERSION = 8000;
  90. /**
  91. * The currently active container object, or NULL if not initialized yet.
  92. *
  93. * @var \Symfony\Component\DependencyInjection\ContainerInterface|null
  94. */
  95. protected static $container;
  96. /**
  97. * Sets a new global container.
  98. *
  99. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  100. * A new container instance to replace the current.
  101. */
  102. public static function setContainer(ContainerInterface $container) {
  103. static::$container = $container;
  104. }
  105. /**
  106. * Unsets the global container.
  107. */
  108. public static function unsetContainer() {
  109. static::$container = NULL;
  110. }
  111. /**
  112. * Returns the currently active global container.
  113. *
  114. * @return \Symfony\Component\DependencyInjection\ContainerInterface|null
  115. *
  116. * @throws \Drupal\Core\DependencyInjection\ContainerNotInitializedException
  117. */
  118. public static function getContainer() {
  119. if (static::$container === NULL) {
  120. throw new ContainerNotInitializedException('\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.');
  121. }
  122. return static::$container;
  123. }
  124. /**
  125. * Returns TRUE if the container has been initialized, FALSE otherwise.
  126. *
  127. * @return bool
  128. */
  129. public static function hasContainer() {
  130. return static::$container !== NULL;
  131. }
  132. /**
  133. * Retrieves a service from the container.
  134. *
  135. * Use this method if the desired service is not one of those with a dedicated
  136. * accessor method below. If it is listed below, those methods are preferred
  137. * as they can return useful type hints.
  138. *
  139. * @param string $id
  140. * The ID of the service to retrieve.
  141. *
  142. * @return mixed
  143. * The specified service.
  144. */
  145. public static function service($id) {
  146. return static::getContainer()->get($id);
  147. }
  148. /**
  149. * Indicates if a service is defined in the container.
  150. *
  151. * @param string $id
  152. * The ID of the service to check.
  153. *
  154. * @return bool
  155. * TRUE if the specified service exists, FALSE otherwise.
  156. */
  157. public static function hasService($id) {
  158. // Check hasContainer() first in order to always return a Boolean.
  159. return static::hasContainer() && static::getContainer()->has($id);
  160. }
  161. /**
  162. * Gets the app root.
  163. *
  164. * @return string
  165. */
  166. public static function root() {
  167. return static::getContainer()->get('app.root');
  168. }
  169. /**
  170. * Gets the active install profile.
  171. *
  172. * @return string|null
  173. * The name of the active install profile.
  174. */
  175. public static function installProfile() {
  176. return static::getContainer()->getParameter('install_profile');
  177. }
  178. /**
  179. * Indicates if there is a currently active request object.
  180. *
  181. * @return bool
  182. * TRUE if there is a currently active request object, FALSE otherwise.
  183. */
  184. public static function hasRequest() {
  185. // Check hasContainer() first in order to always return a Boolean.
  186. return static::hasContainer() && static::getContainer()->has('request_stack') && static::getContainer()->get('request_stack')->getCurrentRequest() !== NULL;
  187. }
  188. /**
  189. * Retrieves the currently active request object.
  190. *
  191. * Note: The use of this wrapper in particular is especially discouraged. Most
  192. * code should not need to access the request directly. Doing so means it
  193. * will only function when handling an HTTP request, and will require special
  194. * modification or wrapping when run from a command line tool, from certain
  195. * queue processors, or from automated tests.
  196. *
  197. * If code must access the request, it is considerably better to register
  198. * an object with the Service Container and give it a setRequest() method
  199. * that is configured to run when the service is created. That way, the
  200. * correct request object can always be provided by the container and the
  201. * service can still be unit tested.
  202. *
  203. * If this method must be used, never save the request object that is
  204. * returned. Doing so may lead to inconsistencies as the request object is
  205. * volatile and may change at various times, such as during a subrequest.
  206. *
  207. * @return \Symfony\Component\HttpFoundation\Request
  208. * The currently active request object.
  209. */
  210. public static function request() {
  211. return static::getContainer()->get('request_stack')->getCurrentRequest();
  212. }
  213. /**
  214. * Retrives the request stack.
  215. *
  216. * @return \Symfony\Component\HttpFoundation\RequestStack
  217. * The request stack
  218. */
  219. public static function requestStack() {
  220. return static::getContainer()->get('request_stack');
  221. }
  222. /**
  223. * Retrieves the currently active route match object.
  224. *
  225. * @return \Drupal\Core\Routing\RouteMatchInterface
  226. * The currently active route match object.
  227. */
  228. public static function routeMatch() {
  229. return static::getContainer()->get('current_route_match');
  230. }
  231. /**
  232. * Gets the current active user.
  233. *
  234. * @return \Drupal\Core\Session\AccountProxyInterface
  235. */
  236. public static function currentUser() {
  237. return static::getContainer()->get('current_user');
  238. }
  239. /**
  240. * Retrieves the entity manager service.
  241. *
  242. * @return \Drupal\Core\Entity\EntityManagerInterface
  243. * The entity manager service.
  244. *
  245. * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
  246. * Use \Drupal::entityTypeManager() instead in most cases. If the needed
  247. * method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the
  248. * deprecated \Drupal\Core\Entity\EntityManager to find the
  249. * correct interface or service.
  250. */
  251. public static function entityManager() {
  252. return static::getContainer()->get('entity.manager');
  253. }
  254. /**
  255. * Retrieves the entity type manager.
  256. *
  257. * @return \Drupal\Core\Entity\EntityTypeManagerInterface
  258. * The entity type manager.
  259. */
  260. public static function entityTypeManager() {
  261. return static::getContainer()->get('entity_type.manager');
  262. }
  263. /**
  264. * Returns the current primary database.
  265. *
  266. * @return \Drupal\Core\Database\Connection
  267. * The current active database's master connection.
  268. */
  269. public static function database() {
  270. return static::getContainer()->get('database');
  271. }
  272. /**
  273. * Returns the requested cache bin.
  274. *
  275. * @param string $bin
  276. * (optional) The cache bin for which the cache object should be returned,
  277. * defaults to 'default'.
  278. *
  279. * @return \Drupal\Core\Cache\CacheBackendInterface
  280. * The cache object associated with the specified bin.
  281. *
  282. * @ingroup cache
  283. */
  284. public static function cache($bin = 'default') {
  285. return static::getContainer()->get('cache.' . $bin);
  286. }
  287. /**
  288. * Retrieves the class resolver.
  289. *
  290. * This is to be used in procedural code such as module files to instantiate
  291. * an object of a class that implements
  292. * \Drupal\Core\DependencyInjection\ContainerInjectionInterface.
  293. *
  294. * One common usecase is to provide a class which contains the actual code
  295. * of a hook implementation, without having to create a service.
  296. *
  297. * @return \Drupal\Core\DependencyInjection\ClassResolverInterface
  298. * The class resolver.
  299. */
  300. public static function classResolver() {
  301. return static::getContainer()->get('class_resolver');
  302. }
  303. /**
  304. * Returns an expirable key value store collection.
  305. *
  306. * @param string $collection
  307. * The name of the collection holding key and value pairs.
  308. *
  309. * @return \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  310. * An expirable key value store collection.
  311. */
  312. public static function keyValueExpirable($collection) {
  313. return static::getContainer()->get('keyvalue.expirable')->get($collection);
  314. }
  315. /**
  316. * Returns the locking layer instance.
  317. *
  318. * @return \Drupal\Core\Lock\LockBackendInterface
  319. *
  320. * @ingroup lock
  321. */
  322. public static function lock() {
  323. return static::getContainer()->get('lock');
  324. }
  325. /**
  326. * Retrieves a configuration object.
  327. *
  328. * This is the main entry point to the configuration API. Calling
  329. * @code \Drupal::config('book.admin') @endcode will return a configuration
  330. * object in which the book module can store its administrative settings.
  331. *
  332. * @param string $name
  333. * The name of the configuration object to retrieve. The name corresponds to
  334. * a configuration file. For @code \Drupal::config('book.admin') @endcode, the config
  335. * object returned will contain the contents of book.admin configuration file.
  336. *
  337. * @return \Drupal\Core\Config\ImmutableConfig
  338. * An immutable configuration object.
  339. */
  340. public static function config($name) {
  341. return static::getContainer()->get('config.factory')->get($name);
  342. }
  343. /**
  344. * Retrieves the configuration factory.
  345. *
  346. * This is mostly used to change the override settings on the configuration
  347. * factory. For example, changing the language, or turning all overrides on
  348. * or off.
  349. *
  350. * @return \Drupal\Core\Config\ConfigFactoryInterface
  351. * The configuration factory service.
  352. */
  353. public static function configFactory() {
  354. return static::getContainer()->get('config.factory');
  355. }
  356. /**
  357. * Returns a queue for the given queue name.
  358. *
  359. * The following values can be set in your settings.php file's $settings
  360. * array to define which services are used for queues:
  361. * - queue_reliable_service_$name: The container service to use for the
  362. * reliable queue $name.
  363. * - queue_service_$name: The container service to use for the
  364. * queue $name.
  365. * - queue_default: The container service to use by default for queues
  366. * without overrides. This defaults to 'queue.database'.
  367. *
  368. * @param string $name
  369. * The name of the queue to work with.
  370. * @param bool $reliable
  371. * (optional) TRUE if the ordering of items and guaranteeing every item
  372. * executes at least once is important, FALSE if scalability is the main
  373. * concern. Defaults to FALSE.
  374. *
  375. * @return \Drupal\Core\Queue\QueueInterface
  376. * The queue object for a given name.
  377. */
  378. public static function queue($name, $reliable = FALSE) {
  379. return static::getContainer()->get('queue')->get($name, $reliable);
  380. }
  381. /**
  382. * Returns a key/value storage collection.
  383. *
  384. * @param string $collection
  385. * Name of the key/value collection to return.
  386. *
  387. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  388. */
  389. public static function keyValue($collection) {
  390. return static::getContainer()->get('keyvalue')->get($collection);
  391. }
  392. /**
  393. * Returns the state storage service.
  394. *
  395. * Use this to store machine-generated data, local to a specific environment
  396. * that does not need deploying and does not need human editing; for example,
  397. * the last time cron was run. Data which needs to be edited by humans and
  398. * needs to be the same across development, production, etc. environments
  399. * (for example, the system maintenance message) should use \Drupal::config() instead.
  400. *
  401. * @return \Drupal\Core\State\StateInterface
  402. */
  403. public static function state() {
  404. return static::getContainer()->get('state');
  405. }
  406. /**
  407. * Returns the default http client.
  408. *
  409. * @return \GuzzleHttp\Client
  410. * A guzzle http client instance.
  411. */
  412. public static function httpClient() {
  413. return static::getContainer()->get('http_client');
  414. }
  415. /**
  416. * Returns the entity query object for this entity type.
  417. *
  418. * @param string $entity_type
  419. * The entity type (for example, node) for which the query object should be
  420. * returned.
  421. * @param string $conjunction
  422. * (optional) Either 'AND' if all conditions in the query need to apply, or
  423. * 'OR' if any of them is sufficient. Defaults to 'AND'.
  424. *
  425. * @return \Drupal\Core\Entity\Query\QueryInterface
  426. * The query object that can query the given entity type.
  427. */
  428. public static function entityQuery($entity_type, $conjunction = 'AND') {
  429. return static::entityTypeManager()->getStorage($entity_type)->getQuery($conjunction);
  430. }
  431. /**
  432. * Returns the entity query aggregate object for this entity type.
  433. *
  434. * @param string $entity_type
  435. * The entity type (for example, node) for which the query object should be
  436. * returned.
  437. * @param string $conjunction
  438. * (optional) Either 'AND' if all conditions in the query need to apply, or
  439. * 'OR' if any of them is sufficient. Defaults to 'AND'.
  440. *
  441. * @return \Drupal\Core\Entity\Query\QueryAggregateInterface
  442. * The query object that can query the given entity type.
  443. */
  444. public static function entityQueryAggregate($entity_type, $conjunction = 'AND') {
  445. return static::entityTypeManager()->getStorage($entity_type)->getAggregateQuery($conjunction);
  446. }
  447. /**
  448. * Returns the flood instance.
  449. *
  450. * @return \Drupal\Core\Flood\FloodInterface
  451. */
  452. public static function flood() {
  453. return static::getContainer()->get('flood');
  454. }
  455. /**
  456. * Returns the module handler.
  457. *
  458. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  459. */
  460. public static function moduleHandler() {
  461. return static::getContainer()->get('module_handler');
  462. }
  463. /**
  464. * Returns the typed data manager service.
  465. *
  466. * Use the typed data manager service for creating typed data objects.
  467. *
  468. * @return \Drupal\Core\TypedData\TypedDataManagerInterface
  469. * The typed data manager.
  470. *
  471. * @see \Drupal\Core\TypedData\TypedDataManager::create()
  472. */
  473. public static function typedDataManager() {
  474. return static::getContainer()->get('typed_data_manager');
  475. }
  476. /**
  477. * Returns the token service.
  478. *
  479. * @return \Drupal\Core\Utility\Token
  480. * The token service.
  481. */
  482. public static function token() {
  483. return static::getContainer()->get('token');
  484. }
  485. /**
  486. * Returns the url generator service.
  487. *
  488. * @return \Drupal\Core\Routing\UrlGeneratorInterface
  489. * The url generator service.
  490. */
  491. public static function urlGenerator() {
  492. return static::getContainer()->get('url_generator');
  493. }
  494. /**
  495. * Generates a URL string for a specific route based on the given parameters.
  496. *
  497. * This method is a convenience wrapper for generating URL strings for URLs
  498. * that have Drupal routes (that is, most pages generated by Drupal) using
  499. * the \Drupal\Core\Url object. See \Drupal\Core\Url::fromRoute() for
  500. * detailed documentation. For non-routed local URIs relative to
  501. * the base path (like robots.txt) use Url::fromUri()->toString() with the
  502. * base: scheme.
  503. *
  504. * @param string $route_name
  505. * The name of the route.
  506. * @param array $route_parameters
  507. * (optional) An associative array of parameter names and values.
  508. * @param array $options
  509. * (optional) An associative array of additional options.
  510. * @param bool $collect_bubbleable_metadata
  511. * (optional) Defaults to FALSE. When TRUE, both the generated URL and its
  512. * associated bubbleable metadata are returned.
  513. *
  514. * @return string|\Drupal\Core\GeneratedUrl
  515. * A string containing a URL to the given path.
  516. * When $collect_bubbleable_metadata is TRUE, a GeneratedUrl object is
  517. * returned, containing the generated URL plus bubbleable metadata.
  518. *
  519. * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
  520. * @see \Drupal\Core\Url
  521. * @see \Drupal\Core\Url::fromRoute()
  522. * @see \Drupal\Core\Url::fromUri()
  523. *
  524. * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
  525. * Instead create a \Drupal\Core\Url object directly, for example using
  526. * Url::fromRoute().
  527. */
  528. public static function url($route_name, $route_parameters = [], $options = [], $collect_bubbleable_metadata = FALSE) {
  529. return static::getContainer()->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options, $collect_bubbleable_metadata);
  530. }
  531. /**
  532. * Returns the link generator service.
  533. *
  534. * @return \Drupal\Core\Utility\LinkGeneratorInterface
  535. */
  536. public static function linkGenerator() {
  537. return static::getContainer()->get('link_generator');
  538. }
  539. /**
  540. * Renders a link with a given link text and Url object.
  541. *
  542. * This method is a convenience wrapper for the link generator service's
  543. * generate() method.
  544. *
  545. * @param string $text
  546. * The link text for the anchor tag.
  547. * @param \Drupal\Core\Url $url
  548. * The URL object used for the link.
  549. *
  550. * @return \Drupal\Core\GeneratedLink
  551. * A GeneratedLink object containing a link to the given route and
  552. * parameters and bubbleable metadata.
  553. *
  554. * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
  555. * @see \Drupal\Core\Url
  556. *
  557. * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
  558. * Use \Drupal\Core\Link instead.
  559. * Example:
  560. * @code
  561. * $link = Link::fromTextAndUrl($text, $url);
  562. * @endcode
  563. */
  564. public static function l($text, Url $url) {
  565. return static::getContainer()->get('link_generator')->generate($text, $url);
  566. }
  567. /**
  568. * Returns the string translation service.
  569. *
  570. * @return \Drupal\Core\StringTranslation\TranslationManager
  571. * The string translation manager.
  572. */
  573. public static function translation() {
  574. return static::getContainer()->get('string_translation');
  575. }
  576. /**
  577. * Returns the language manager service.
  578. *
  579. * @return \Drupal\Core\Language\LanguageManagerInterface
  580. * The language manager.
  581. */
  582. public static function languageManager() {
  583. return static::getContainer()->get('language_manager');
  584. }
  585. /**
  586. * Returns the CSRF token manager service.
  587. *
  588. * The generated token is based on the session ID of the current user. Normally,
  589. * anonymous users do not have a session, so the generated token will be
  590. * different on every page request. To generate a token for users without a
  591. * session, manually start a session prior to calling this function.
  592. *
  593. * @return \Drupal\Core\Access\CsrfTokenGenerator
  594. * The CSRF token manager.
  595. *
  596. * @see \Drupal\Core\Session\SessionManager::start()
  597. */
  598. public static function csrfToken() {
  599. return static::getContainer()->get('csrf_token');
  600. }
  601. /**
  602. * Returns the transliteration service.
  603. *
  604. * @return \Drupal\Core\Transliteration\PhpTransliteration
  605. * The transliteration manager.
  606. */
  607. public static function transliteration() {
  608. return static::getContainer()->get('transliteration');
  609. }
  610. /**
  611. * Returns the form builder service.
  612. *
  613. * @return \Drupal\Core\Form\FormBuilderInterface
  614. * The form builder.
  615. */
  616. public static function formBuilder() {
  617. return static::getContainer()->get('form_builder');
  618. }
  619. /**
  620. * Gets the theme service.
  621. *
  622. * @return \Drupal\Core\Theme\ThemeManagerInterface
  623. */
  624. public static function theme() {
  625. return static::getContainer()->get('theme.manager');
  626. }
  627. /**
  628. * Gets the syncing state.
  629. *
  630. * @return bool
  631. * Returns TRUE is syncing flag set.
  632. */
  633. public static function isConfigSyncing() {
  634. return static::getContainer()->get('config.installer')->isSyncing();
  635. }
  636. /**
  637. * Returns a channel logger object.
  638. *
  639. * @param string $channel
  640. * The name of the channel. Can be any string, but the general practice is
  641. * to use the name of the subsystem calling this.
  642. *
  643. * @return \Psr\Log\LoggerInterface
  644. * The logger for this channel.
  645. */
  646. public static function logger($channel) {
  647. return static::getContainer()->get('logger.factory')->get($channel);
  648. }
  649. /**
  650. * Returns the menu tree.
  651. *
  652. * @return \Drupal\Core\Menu\MenuLinkTreeInterface
  653. * The menu tree.
  654. */
  655. public static function menuTree() {
  656. return static::getContainer()->get('menu.link_tree');
  657. }
  658. /**
  659. * Returns the path validator.
  660. *
  661. * @return \Drupal\Core\Path\PathValidatorInterface
  662. */
  663. public static function pathValidator() {
  664. return static::getContainer()->get('path.validator');
  665. }
  666. /**
  667. * Returns the access manager service.
  668. *
  669. * @return \Drupal\Core\Access\AccessManagerInterface
  670. * The access manager service.
  671. */
  672. public static function accessManager() {
  673. return static::getContainer()->get('access_manager');
  674. }
  675. /**
  676. * Returns the redirect destination helper.
  677. *
  678. * @return \Drupal\Core\Routing\RedirectDestinationInterface
  679. * The redirect destination helper.
  680. */
  681. public static function destination() {
  682. return static::getContainer()->get('redirect.destination');
  683. }
  684. /**
  685. * Returns the entity definition update manager.
  686. *
  687. * @return \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
  688. * The entity definition update manager.
  689. */
  690. public static function entityDefinitionUpdateManager() {
  691. return static::getContainer()->get('entity.definition_update_manager');
  692. }
  693. /**
  694. * Returns the time service.
  695. *
  696. * @return \Drupal\Component\Datetime\TimeInterface
  697. * The time service.
  698. */
  699. public static function time() {
  700. return static::getContainer()->get('datetime.time');
  701. }
  702. /**
  703. * Returns the messenger.
  704. *
  705. * @return \Drupal\Core\Messenger\MessengerInterface
  706. * The messenger.
  707. */
  708. public static function messenger() {
  709. // @todo Replace with service once LegacyMessenger is removed in 9.0.0.
  710. // @see https://www.drupal.org/node/2928994
  711. return new LegacyMessenger();
  712. }
  713. }