Drupal.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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.6.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. * Retrieves 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. * @param string $class
  298. * (optional) A class name to instantiate.
  299. *
  300. * @return \Drupal\Core\DependencyInjection\ClassResolverInterface|object
  301. * The class resolver or if $class is provided, a class instance with a
  302. * given class definition.
  303. *
  304. * @throws \InvalidArgumentException
  305. * If $class does not exist.
  306. */
  307. public static function classResolver($class = NULL) {
  308. if ($class) {
  309. return static::getContainer()->get('class_resolver')->getInstanceFromDefinition($class);
  310. }
  311. return static::getContainer()->get('class_resolver');
  312. }
  313. /**
  314. * Returns an expirable key value store collection.
  315. *
  316. * @param string $collection
  317. * The name of the collection holding key and value pairs.
  318. *
  319. * @return \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  320. * An expirable key value store collection.
  321. */
  322. public static function keyValueExpirable($collection) {
  323. return static::getContainer()->get('keyvalue.expirable')->get($collection);
  324. }
  325. /**
  326. * Returns the locking layer instance.
  327. *
  328. * @return \Drupal\Core\Lock\LockBackendInterface
  329. *
  330. * @ingroup lock
  331. */
  332. public static function lock() {
  333. return static::getContainer()->get('lock');
  334. }
  335. /**
  336. * Retrieves a configuration object.
  337. *
  338. * This is the main entry point to the configuration API. Calling
  339. * @code \Drupal::config('book.admin') @endcode will return a configuration
  340. * object in which the book module can store its administrative settings.
  341. *
  342. * @param string $name
  343. * The name of the configuration object to retrieve. The name corresponds to
  344. * a configuration file. For @code \Drupal::config('book.admin') @endcode, the config
  345. * object returned will contain the contents of book.admin configuration file.
  346. *
  347. * @return \Drupal\Core\Config\ImmutableConfig
  348. * An immutable configuration object.
  349. */
  350. public static function config($name) {
  351. return static::getContainer()->get('config.factory')->get($name);
  352. }
  353. /**
  354. * Retrieves the configuration factory.
  355. *
  356. * This is mostly used to change the override settings on the configuration
  357. * factory. For example, changing the language, or turning all overrides on
  358. * or off.
  359. *
  360. * @return \Drupal\Core\Config\ConfigFactoryInterface
  361. * The configuration factory service.
  362. */
  363. public static function configFactory() {
  364. return static::getContainer()->get('config.factory');
  365. }
  366. /**
  367. * Returns a queue for the given queue name.
  368. *
  369. * The following values can be set in your settings.php file's $settings
  370. * array to define which services are used for queues:
  371. * - queue_reliable_service_$name: The container service to use for the
  372. * reliable queue $name.
  373. * - queue_service_$name: The container service to use for the
  374. * queue $name.
  375. * - queue_default: The container service to use by default for queues
  376. * without overrides. This defaults to 'queue.database'.
  377. *
  378. * @param string $name
  379. * The name of the queue to work with.
  380. * @param bool $reliable
  381. * (optional) TRUE if the ordering of items and guaranteeing every item
  382. * executes at least once is important, FALSE if scalability is the main
  383. * concern. Defaults to FALSE.
  384. *
  385. * @return \Drupal\Core\Queue\QueueInterface
  386. * The queue object for a given name.
  387. */
  388. public static function queue($name, $reliable = FALSE) {
  389. return static::getContainer()->get('queue')->get($name, $reliable);
  390. }
  391. /**
  392. * Returns a key/value storage collection.
  393. *
  394. * @param string $collection
  395. * Name of the key/value collection to return.
  396. *
  397. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  398. */
  399. public static function keyValue($collection) {
  400. return static::getContainer()->get('keyvalue')->get($collection);
  401. }
  402. /**
  403. * Returns the state storage service.
  404. *
  405. * Use this to store machine-generated data, local to a specific environment
  406. * that does not need deploying and does not need human editing; for example,
  407. * the last time cron was run. Data which needs to be edited by humans and
  408. * needs to be the same across development, production, etc. environments
  409. * (for example, the system maintenance message) should use \Drupal::config() instead.
  410. *
  411. * @return \Drupal\Core\State\StateInterface
  412. */
  413. public static function state() {
  414. return static::getContainer()->get('state');
  415. }
  416. /**
  417. * Returns the default http client.
  418. *
  419. * @return \GuzzleHttp\Client
  420. * A guzzle http client instance.
  421. */
  422. public static function httpClient() {
  423. return static::getContainer()->get('http_client');
  424. }
  425. /**
  426. * Returns the entity query object for this entity type.
  427. *
  428. * @param string $entity_type
  429. * The entity type (for example, node) for which the query object should be
  430. * returned.
  431. * @param string $conjunction
  432. * (optional) Either 'AND' if all conditions in the query need to apply, or
  433. * 'OR' if any of them is sufficient. Defaults to 'AND'.
  434. *
  435. * @return \Drupal\Core\Entity\Query\QueryInterface
  436. * The query object that can query the given entity type.
  437. */
  438. public static function entityQuery($entity_type, $conjunction = 'AND') {
  439. return static::entityTypeManager()->getStorage($entity_type)->getQuery($conjunction);
  440. }
  441. /**
  442. * Returns the entity query aggregate object for this entity type.
  443. *
  444. * @param string $entity_type
  445. * The entity type (for example, node) for which the query object should be
  446. * returned.
  447. * @param string $conjunction
  448. * (optional) Either 'AND' if all conditions in the query need to apply, or
  449. * 'OR' if any of them is sufficient. Defaults to 'AND'.
  450. *
  451. * @return \Drupal\Core\Entity\Query\QueryAggregateInterface
  452. * The query object that can query the given entity type.
  453. */
  454. public static function entityQueryAggregate($entity_type, $conjunction = 'AND') {
  455. return static::entityTypeManager()->getStorage($entity_type)->getAggregateQuery($conjunction);
  456. }
  457. /**
  458. * Returns the flood instance.
  459. *
  460. * @return \Drupal\Core\Flood\FloodInterface
  461. */
  462. public static function flood() {
  463. return static::getContainer()->get('flood');
  464. }
  465. /**
  466. * Returns the module handler.
  467. *
  468. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  469. */
  470. public static function moduleHandler() {
  471. return static::getContainer()->get('module_handler');
  472. }
  473. /**
  474. * Returns the typed data manager service.
  475. *
  476. * Use the typed data manager service for creating typed data objects.
  477. *
  478. * @return \Drupal\Core\TypedData\TypedDataManagerInterface
  479. * The typed data manager.
  480. *
  481. * @see \Drupal\Core\TypedData\TypedDataManager::create()
  482. */
  483. public static function typedDataManager() {
  484. return static::getContainer()->get('typed_data_manager');
  485. }
  486. /**
  487. * Returns the token service.
  488. *
  489. * @return \Drupal\Core\Utility\Token
  490. * The token service.
  491. */
  492. public static function token() {
  493. return static::getContainer()->get('token');
  494. }
  495. /**
  496. * Returns the url generator service.
  497. *
  498. * @return \Drupal\Core\Routing\UrlGeneratorInterface
  499. * The url generator service.
  500. */
  501. public static function urlGenerator() {
  502. return static::getContainer()->get('url_generator');
  503. }
  504. /**
  505. * Generates a URL string for a specific route based on the given parameters.
  506. *
  507. * This method is a convenience wrapper for generating URL strings for URLs
  508. * that have Drupal routes (that is, most pages generated by Drupal) using
  509. * the \Drupal\Core\Url object. See \Drupal\Core\Url::fromRoute() for
  510. * detailed documentation. For non-routed local URIs relative to
  511. * the base path (like robots.txt) use Url::fromUri()->toString() with the
  512. * base: scheme.
  513. *
  514. * @param string $route_name
  515. * The name of the route.
  516. * @param array $route_parameters
  517. * (optional) An associative array of parameter names and values.
  518. * @param array $options
  519. * (optional) An associative array of additional options.
  520. * @param bool $collect_bubbleable_metadata
  521. * (optional) Defaults to FALSE. When TRUE, both the generated URL and its
  522. * associated bubbleable metadata are returned.
  523. *
  524. * @return string|\Drupal\Core\GeneratedUrl
  525. * A string containing a URL to the given path.
  526. * When $collect_bubbleable_metadata is TRUE, a GeneratedUrl object is
  527. * returned, containing the generated URL plus bubbleable metadata.
  528. *
  529. * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
  530. * @see \Drupal\Core\Url
  531. * @see \Drupal\Core\Url::fromRoute()
  532. * @see \Drupal\Core\Url::fromUri()
  533. *
  534. * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
  535. * Instead create a \Drupal\Core\Url object directly, for example using
  536. * Url::fromRoute().
  537. */
  538. public static function url($route_name, $route_parameters = [], $options = [], $collect_bubbleable_metadata = FALSE) {
  539. return static::getContainer()->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options, $collect_bubbleable_metadata);
  540. }
  541. /**
  542. * Returns the link generator service.
  543. *
  544. * @return \Drupal\Core\Utility\LinkGeneratorInterface
  545. */
  546. public static function linkGenerator() {
  547. return static::getContainer()->get('link_generator');
  548. }
  549. /**
  550. * Renders a link with a given link text and Url object.
  551. *
  552. * This method is a convenience wrapper for the link generator service's
  553. * generate() method.
  554. *
  555. * @param string $text
  556. * The link text for the anchor tag.
  557. * @param \Drupal\Core\Url $url
  558. * The URL object used for the link.
  559. *
  560. * @return \Drupal\Core\GeneratedLink
  561. * A GeneratedLink object containing a link to the given route and
  562. * parameters and bubbleable metadata.
  563. *
  564. * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
  565. * @see \Drupal\Core\Url
  566. *
  567. * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
  568. * Use \Drupal\Core\Link instead.
  569. * Example:
  570. * @code
  571. * $link = Link::fromTextAndUrl($text, $url);
  572. * @endcode
  573. */
  574. public static function l($text, Url $url) {
  575. return static::getContainer()->get('link_generator')->generate($text, $url);
  576. }
  577. /**
  578. * Returns the string translation service.
  579. *
  580. * @return \Drupal\Core\StringTranslation\TranslationManager
  581. * The string translation manager.
  582. */
  583. public static function translation() {
  584. return static::getContainer()->get('string_translation');
  585. }
  586. /**
  587. * Returns the language manager service.
  588. *
  589. * @return \Drupal\Core\Language\LanguageManagerInterface
  590. * The language manager.
  591. */
  592. public static function languageManager() {
  593. return static::getContainer()->get('language_manager');
  594. }
  595. /**
  596. * Returns the CSRF token manager service.
  597. *
  598. * The generated token is based on the session ID of the current user. Normally,
  599. * anonymous users do not have a session, so the generated token will be
  600. * different on every page request. To generate a token for users without a
  601. * session, manually start a session prior to calling this function.
  602. *
  603. * @return \Drupal\Core\Access\CsrfTokenGenerator
  604. * The CSRF token manager.
  605. *
  606. * @see \Drupal\Core\Session\SessionManager::start()
  607. */
  608. public static function csrfToken() {
  609. return static::getContainer()->get('csrf_token');
  610. }
  611. /**
  612. * Returns the transliteration service.
  613. *
  614. * @return \Drupal\Core\Transliteration\PhpTransliteration
  615. * The transliteration manager.
  616. */
  617. public static function transliteration() {
  618. return static::getContainer()->get('transliteration');
  619. }
  620. /**
  621. * Returns the form builder service.
  622. *
  623. * @return \Drupal\Core\Form\FormBuilderInterface
  624. * The form builder.
  625. */
  626. public static function formBuilder() {
  627. return static::getContainer()->get('form_builder');
  628. }
  629. /**
  630. * Gets the theme service.
  631. *
  632. * @return \Drupal\Core\Theme\ThemeManagerInterface
  633. */
  634. public static function theme() {
  635. return static::getContainer()->get('theme.manager');
  636. }
  637. /**
  638. * Gets the syncing state.
  639. *
  640. * @return bool
  641. * Returns TRUE is syncing flag set.
  642. */
  643. public static function isConfigSyncing() {
  644. return static::getContainer()->get('config.installer')->isSyncing();
  645. }
  646. /**
  647. * Returns a channel logger object.
  648. *
  649. * @param string $channel
  650. * The name of the channel. Can be any string, but the general practice is
  651. * to use the name of the subsystem calling this.
  652. *
  653. * @return \Psr\Log\LoggerInterface
  654. * The logger for this channel.
  655. */
  656. public static function logger($channel) {
  657. return static::getContainer()->get('logger.factory')->get($channel);
  658. }
  659. /**
  660. * Returns the menu tree.
  661. *
  662. * @return \Drupal\Core\Menu\MenuLinkTreeInterface
  663. * The menu tree.
  664. */
  665. public static function menuTree() {
  666. return static::getContainer()->get('menu.link_tree');
  667. }
  668. /**
  669. * Returns the path validator.
  670. *
  671. * @return \Drupal\Core\Path\PathValidatorInterface
  672. */
  673. public static function pathValidator() {
  674. return static::getContainer()->get('path.validator');
  675. }
  676. /**
  677. * Returns the access manager service.
  678. *
  679. * @return \Drupal\Core\Access\AccessManagerInterface
  680. * The access manager service.
  681. */
  682. public static function accessManager() {
  683. return static::getContainer()->get('access_manager');
  684. }
  685. /**
  686. * Returns the redirect destination helper.
  687. *
  688. * @return \Drupal\Core\Routing\RedirectDestinationInterface
  689. * The redirect destination helper.
  690. */
  691. public static function destination() {
  692. return static::getContainer()->get('redirect.destination');
  693. }
  694. /**
  695. * Returns the entity definition update manager.
  696. *
  697. * @return \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
  698. * The entity definition update manager.
  699. */
  700. public static function entityDefinitionUpdateManager() {
  701. return static::getContainer()->get('entity.definition_update_manager');
  702. }
  703. /**
  704. * Returns the time service.
  705. *
  706. * @return \Drupal\Component\Datetime\TimeInterface
  707. * The time service.
  708. */
  709. public static function time() {
  710. return static::getContainer()->get('datetime.time');
  711. }
  712. /**
  713. * Returns the messenger.
  714. *
  715. * @return \Drupal\Core\Messenger\MessengerInterface
  716. * The messenger.
  717. */
  718. public static function messenger() {
  719. // @todo Replace with service once LegacyMessenger is removed in 9.0.0.
  720. // @see https://www.drupal.org/node/2928994
  721. return new LegacyMessenger();
  722. }
  723. }