Drupal.php 24.2 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal.
6 7
 */

8
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
9
use Drupal\Core\Messenger\LegacyMessenger;
10
use Drupal\Core\Url;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12 13 14 15 16 17 18 19 20 21 22 23 24

/**
 * Static Service Container wrapper.
 *
 * Generally, code in Drupal should accept its dependencies via either
 * constructor injection or setter method injection. However, there are cases,
 * particularly in legacy procedural code, where that is infeasible. This
 * class acts as a unified global accessor to arbitrary services within the
 * system in order to ease the transition from procedural code to injected OO
 * code.
 *
 * The container is built by the kernel and passed in to this class which stores
 * it statically. The container always contains the services from
25 26
 * \Drupal\Core\CoreServiceProvider, the service providers of enabled modules and any other
 * service providers defined in $GLOBALS['conf']['container_service_providers'].
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *
 * This class exists only to support legacy code that cannot be dependency
 * injected. If your code needs it, consider refactoring it to be object
 * oriented, if possible. When this is not possible, for instance in the case of
 * hook implementations, and your code is more than a few non-reusable lines, it
 * is recommended to instantiate an object implementing the actual logic.
 *
 * @code
 *   // Legacy procedural code.
 *   function hook_do_stuff() {
 *     $lock = lock()->acquire('stuff_lock');
 *     // ...
 *   }
 *
 *   // Correct procedural code.
 *   function hook_do_stuff() {
43
 *     $lock = \Drupal::lock()->acquire('stuff_lock');
44 45 46 47 48 49
 *     // ...
 *   }
 *
 *   // The preferred way: dependency injected code.
 *   function hook_do_stuff() {
 *     // Move the actual implementation to a class and instantiate it.
50
 *     $instance = new StuffDoingClass(\Drupal::lock());
51 52 53 54 55 56
 *     $instance->doStuff();
 *
 *     // Or, even better, rely on the service container to avoid hard coding a
 *     // specific interface implementation, so that the actual logic can be
 *     // swapped. This might not always make sense, but in general it is a good
 *     // practice.
57
 *     \Drupal::service('stuff.doing')->doStuff();
58 59 60 61 62 63 64 65 66
 *   }
 *
 *   interface StuffDoingInterface {
 *     public function doStuff();
 *   }
 *
 *   class StuffDoingClass implements StuffDoingInterface {
 *     protected $lockBackend;
 *
67 68
 *     public function __construct(LockBackendInterface $lock_backend) {
 *       $this->lockBackend = $lock_backend;
69 70 71 72 73 74 75 76 77 78 79 80 81
 *     }
 *
 *     public function doStuff() {
 *       $lock = $this->lockBackend->acquire('stuff_lock');
 *       // ...
 *     }
 *   }
 * @endcode
 *
 * @see \Drupal\Core\DrupalKernel
 */
class Drupal {

82 83 84
  /**
   * The current system version.
   */
catch's avatar
catch committed
85
  const VERSION = '8.7.2';
86 87 88 89 90 91

  /**
   * Core API compatibility.
   */
  const CORE_COMPATIBILITY = '8.x';

92 93 94 95 96
  /**
   * Core minimum schema version.
   */
  const CORE_MINIMUM_SCHEMA_VERSION = 8000;

97
  /**
98
   * The currently active container object, or NULL if not initialized yet.
99
   *
100
   * @var \Symfony\Component\DependencyInjection\ContainerInterface|null
101 102 103 104 105 106 107
   */
  protected static $container;

  /**
   * Sets a new global container.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
108
   *   A new container instance to replace the current.
109
   */
110
  public static function setContainer(ContainerInterface $container) {
111 112 113
    static::$container = $container;
  }

114 115 116 117 118 119 120
  /**
   * Unsets the global container.
   */
  public static function unsetContainer() {
    static::$container = NULL;
  }

121 122 123
  /**
   * Returns the currently active global container.
   *
124
   * @return \Symfony\Component\DependencyInjection\ContainerInterface|null
125 126
   *
   * @throws \Drupal\Core\DependencyInjection\ContainerNotInitializedException
127 128
   */
  public static function getContainer() {
129 130 131
    if (static::$container === NULL) {
      throw new ContainerNotInitializedException('\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.');
    }
132 133 134
    return static::$container;
  }

135 136 137 138 139 140 141 142 143
  /**
   * Returns TRUE if the container has been initialized, FALSE otherwise.
   *
   * @return bool
   */
  public static function hasContainer() {
    return static::$container !== NULL;
  }

144 145 146 147 148 149 150 151 152
  /**
   * Retrieves a service from the container.
   *
   * Use this method if the desired service is not one of those with a dedicated
   * accessor method below. If it is listed below, those methods are preferred
   * as they can return useful type hints.
   *
   * @param string $id
   *   The ID of the service to retrieve.
153
   *
154 155 156 157
   * @return mixed
   *   The specified service.
   */
  public static function service($id) {
158
    return static::getContainer()->get($id);
159 160
  }

161 162 163 164 165 166 167 168 169 170
  /**
   * Indicates if a service is defined in the container.
   *
   * @param string $id
   *   The ID of the service to check.
   *
   * @return bool
   *   TRUE if the specified service exists, FALSE otherwise.
   */
  public static function hasService($id) {
171 172
    // Check hasContainer() first in order to always return a Boolean.
    return static::hasContainer() && static::getContainer()->has($id);
173 174
  }

175 176 177 178 179 180
  /**
   * Gets the app root.
   *
   * @return string
   */
  public static function root() {
181
    return static::getContainer()->get('app.root');
182 183
  }

184 185 186 187 188 189 190 191 192 193
  /**
   * Gets the active install profile.
   *
   * @return string|null
   *   The name of the active install profile.
   */
  public static function installProfile() {
    return static::getContainer()->getParameter('install_profile');
  }

194 195 196 197 198 199 200
  /**
   * Indicates if there is a currently active request object.
   *
   * @return bool
   *   TRUE if there is a currently active request object, FALSE otherwise.
   */
  public static function hasRequest() {
201 202
    // Check hasContainer() first in order to always return a Boolean.
    return static::hasContainer() && static::getContainer()->has('request_stack') && static::getContainer()->get('request_stack')->getCurrentRequest() !== NULL;
203 204
  }

205 206 207
  /**
   * Retrieves the currently active request object.
   *
208
   * Note: The use of this wrapper in particular is especially discouraged. Most
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
   * code should not need to access the request directly.  Doing so means it
   * will only function when handling an HTTP request, and will require special
   * modification or wrapping when run from a command line tool, from certain
   * queue processors, or from automated tests.
   *
   * If code must access the request, it is considerably better to register
   * an object with the Service Container and give it a setRequest() method
   * that is configured to run when the service is created.  That way, the
   * correct request object can always be provided by the container and the
   * service can still be unit tested.
   *
   * If this method must be used, never save the request object that is
   * returned.  Doing so may lead to inconsistencies as the request object is
   * volatile and may change at various times, such as during a subrequest.
   *
   * @return \Symfony\Component\HttpFoundation\Request
   *   The currently active request object.
   */
  public static function request() {
228
    return static::getContainer()->get('request_stack')->getCurrentRequest();
229 230
  }

231
  /**
232
   * Retrieves the request stack.
233 234 235 236 237
   *
   * @return \Symfony\Component\HttpFoundation\RequestStack
   *   The request stack
   */
  public static function requestStack() {
238
    return static::getContainer()->get('request_stack');
239 240
  }

241 242 243 244 245 246 247
  /**
   * Retrieves the currently active route match object.
   *
   * @return \Drupal\Core\Routing\RouteMatchInterface
   *   The currently active route match object.
   */
  public static function routeMatch() {
248
    return static::getContainer()->get('current_route_match');
249 250
  }

251 252 253
  /**
   * Gets the current active user.
   *
254
   * @return \Drupal\Core\Session\AccountProxyInterface
255 256
   */
  public static function currentUser() {
257
    return static::getContainer()->get('current_user');
258 259
  }

260 261 262
  /**
   * Retrieves the entity manager service.
   *
263
   * @return \Drupal\Core\Entity\EntityManagerInterface
264
   *   The entity manager service.
265 266 267 268 269 270
   *
   * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
   *   Use \Drupal::entityTypeManager() instead in most cases. If the needed
   *   method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the
   *   deprecated \Drupal\Core\Entity\EntityManager to find the
   *   correct interface or service.
271 272
   */
  public static function entityManager() {
273
    return static::getContainer()->get('entity.manager');
274 275
  }

276 277 278 279 280 281 282 283 284 285
  /**
   * Retrieves the entity type manager.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   The entity type manager.
   */
  public static function entityTypeManager() {
    return static::getContainer()->get('entity_type.manager');
  }

286 287 288 289 290 291 292
  /**
   * Returns the current primary database.
   *
   * @return \Drupal\Core\Database\Connection
   *   The current active database's master connection.
   */
  public static function database() {
293
    return static::getContainer()->get('database');
294 295
  }

296 297 298 299 300
  /**
   * Returns the requested cache bin.
   *
   * @param string $bin
   *   (optional) The cache bin for which the cache object should be returned,
301
   *   defaults to 'default'.
302
   *
303
   * @return \Drupal\Core\Cache\CacheBackendInterface
304
   *   The cache object associated with the specified bin.
305 306
   *
   * @ingroup cache
307
   */
308
  public static function cache($bin = 'default') {
309
    return static::getContainer()->get('cache.' . $bin);
310 311
  }

312 313 314 315 316 317 318 319 320 321
  /**
   * Retrieves the class resolver.
   *
   * This is to be used in procedural code such as module files to instantiate
   * an object of a class that implements
   * \Drupal\Core\DependencyInjection\ContainerInjectionInterface.
   *
   * One common usecase is to provide a class which contains the actual code
   * of a hook implementation, without having to create a service.
   *
322 323 324 325 326 327 328 329 330
   * @param string $class
   *   (optional) A class name to instantiate.
   *
   * @return \Drupal\Core\DependencyInjection\ClassResolverInterface|object
   *   The class resolver or if $class is provided, a class instance with a
   *   given class definition.
   *
   * @throws \InvalidArgumentException
   *   If $class does not exist.
331
   */
332 333 334 335
  public static function classResolver($class = NULL) {
    if ($class) {
      return static::getContainer()->get('class_resolver')->getInstanceFromDefinition($class);
    }
336 337 338
    return static::getContainer()->get('class_resolver');
  }

339 340 341 342 343 344 345 346 347 348
  /**
   * Returns an expirable key value store collection.
   *
   * @param string $collection
   *   The name of the collection holding key and value pairs.
   *
   * @return \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   *   An expirable key value store collection.
   */
  public static function keyValueExpirable($collection) {
349
    return static::getContainer()->get('keyvalue.expirable')->get($collection);
350 351
  }

352 353 354 355
  /**
   * Returns the locking layer instance.
   *
   * @return \Drupal\Core\Lock\LockBackendInterface
356 357
   *
   * @ingroup lock
358 359
   */
  public static function lock() {
360
    return static::getContainer()->get('lock');
361 362
  }

363 364 365 366
  /**
   * Retrieves a configuration object.
   *
   * This is the main entry point to the configuration API. Calling
367
   * @code \Drupal::config('book.admin') @endcode will return a configuration
368 369 370 371
   * object in which the book module can store its administrative settings.
   *
   * @param string $name
   *   The name of the configuration object to retrieve. The name corresponds to
372
   *   a configuration file. For @code \Drupal::config('book.admin') @endcode, the config
373 374
   *   object returned will contain the contents of book.admin configuration file.
   *
375 376
   * @return \Drupal\Core\Config\ImmutableConfig
   *   An immutable configuration object.
377 378
   */
  public static function config($name) {
379
    return static::getContainer()->get('config.factory')->get($name);
380 381
  }

382 383 384 385 386 387 388
  /**
   * Retrieves the configuration factory.
   *
   * This is mostly used to change the override settings on the configuration
   * factory. For example, changing the language, or turning all overrides on
   * or off.
   *
389
   * @return \Drupal\Core\Config\ConfigFactoryInterface
390
   *   The configuration factory service.
391 392
   */
  public static function configFactory() {
393
    return static::getContainer()->get('config.factory');
394 395
  }

396 397 398
  /**
   * Returns a queue for the given queue name.
   *
399 400 401 402 403 404 405 406
   * The following values can be set in your settings.php file's $settings
   * array to define which services are used for queues:
   * - queue_reliable_service_$name: The container service to use for the
   *   reliable queue $name.
   * - queue_service_$name: The container service to use for the
   *   queue $name.
   * - queue_default: The container service to use by default for queues
   *   without overrides. This defaults to 'queue.database'.
407 408 409 410 411 412 413 414 415 416 417 418
   *
   * @param string $name
   *   The name of the queue to work with.
   * @param bool $reliable
   *   (optional) TRUE if the ordering of items and guaranteeing every item
   *   executes at least once is important, FALSE if scalability is the main
   *   concern. Defaults to FALSE.
   *
   * @return \Drupal\Core\Queue\QueueInterface
   *   The queue object for a given name.
   */
  public static function queue($name, $reliable = FALSE) {
419
    return static::getContainer()->get('queue')->get($name, $reliable);
420 421 422 423 424
  }

  /**
   * Returns a key/value storage collection.
   *
425
   * @param string $collection
426 427 428 429 430
   *   Name of the key/value collection to return.
   *
   * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  public static function keyValue($collection) {
431
    return static::getContainer()->get('keyvalue')->get($collection);
432 433 434 435 436 437 438 439 440
  }

  /**
   * Returns the state storage service.
   *
   * Use this to store machine-generated data, local to a specific environment
   * that does not need deploying and does not need human editing; for example,
   * the last time cron was run. Data which needs to be edited by humans and
   * needs to be the same across development, production, etc. environments
441
   * (for example, the system maintenance message) should use \Drupal::config() instead.
442
   *
443
   * @return \Drupal\Core\State\StateInterface
444 445
   */
  public static function state() {
446
    return static::getContainer()->get('state');
447 448 449 450 451
  }

  /**
   * Returns the default http client.
   *
452
   * @return \GuzzleHttp\Client
453 454 455
   *   A guzzle http client instance.
   */
  public static function httpClient() {
456
    return static::getContainer()->get('http_client');
457 458 459 460 461 462
  }

  /**
   * Returns the entity query object for this entity type.
   *
   * @param string $entity_type
463
   *   The entity type (for example, node) for which the query object should be
464 465
   *   returned.
   * @param string $conjunction
466 467
   *   (optional) Either 'AND' if all conditions in the query need to apply, or
   *   'OR' if any of them is sufficient. Defaults to 'AND'.
468 469 470 471 472
   *
   * @return \Drupal\Core\Entity\Query\QueryInterface
   *   The query object that can query the given entity type.
   */
  public static function entityQuery($entity_type, $conjunction = 'AND') {
473
    return static::entityTypeManager()->getStorage($entity_type)->getQuery($conjunction);
474 475
  }

476 477 478
  /**
   * Returns the entity query aggregate object for this entity type.
   *
479
   * @param string $entity_type
480
   *   The entity type (for example, node) for which the query object should be
481
   *   returned.
482
   * @param string $conjunction
483 484
   *   (optional) Either 'AND' if all conditions in the query need to apply, or
   *   'OR' if any of them is sufficient. Defaults to 'AND'.
485
   *
486
   * @return \Drupal\Core\Entity\Query\QueryAggregateInterface
487 488 489
   *   The query object that can query the given entity type.
   */
  public static function entityQueryAggregate($entity_type, $conjunction = 'AND') {
490
    return static::entityTypeManager()->getStorage($entity_type)->getAggregateQuery($conjunction);
491 492
  }

493 494 495 496 497 498
  /**
   * Returns the flood instance.
   *
   * @return \Drupal\Core\Flood\FloodInterface
   */
  public static function flood() {
499
    return static::getContainer()->get('flood');
500 501 502 503 504
  }

  /**
   * Returns the module handler.
   *
505
   * @return \Drupal\Core\Extension\ModuleHandlerInterface
506 507
   */
  public static function moduleHandler() {
508
    return static::getContainer()->get('module_handler');
509 510 511 512 513 514 515
  }

  /**
   * Returns the typed data manager service.
   *
   * Use the typed data manager service for creating typed data objects.
   *
516
   * @return \Drupal\Core\TypedData\TypedDataManagerInterface
517 518 519 520
   *   The typed data manager.
   *
   * @see \Drupal\Core\TypedData\TypedDataManager::create()
   */
521
  public static function typedDataManager() {
522
    return static::getContainer()->get('typed_data_manager');
523 524
  }

525 526 527 528 529 530 531
  /**
   * Returns the token service.
   *
   * @return \Drupal\Core\Utility\Token
   *   The token service.
   */
  public static function token() {
532
    return static::getContainer()->get('token');
533 534
  }

535 536 537
  /**
   * Returns the url generator service.
   *
538
   * @return \Drupal\Core\Routing\UrlGeneratorInterface
539 540 541
   *   The url generator service.
   */
  public static function urlGenerator() {
542
    return static::getContainer()->get('url_generator');
543 544
  }

545
  /**
546 547 548 549 550 551 552
   * Generates a URL string for a specific route based on the given parameters.
   *
   * This method is a convenience wrapper for generating URL strings for URLs
   * that have Drupal routes (that is, most pages generated by Drupal) using
   * the \Drupal\Core\Url object. See \Drupal\Core\Url::fromRoute() for
   * detailed documentation. For non-routed local URIs relative to
   * the base path (like robots.txt) use Url::fromUri()->toString() with the
553
   * base: scheme.
554
   *
555 556 557 558 559 560
   * @param string $route_name
   *   The name of the route.
   * @param array $route_parameters
   *   (optional) An associative array of parameter names and values.
   * @param array $options
   *   (optional) An associative array of additional options.
561
   * @param bool $collect_bubbleable_metadata
562
   *   (optional) Defaults to FALSE. When TRUE, both the generated URL and its
563
   *   associated bubbleable metadata are returned.
564
   *
565 566
   * @return string|\Drupal\Core\GeneratedUrl
   *   A string containing a URL to the given path.
567 568
   *   When $collect_bubbleable_metadata is TRUE, a GeneratedUrl object is
   *   returned, containing the generated URL plus bubbleable metadata.
569 570
   *
   * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
571 572 573
   * @see \Drupal\Core\Url
   * @see \Drupal\Core\Url::fromRoute()
   * @see \Drupal\Core\Url::fromUri()
574 575 576 577
   *
   * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
   *   Instead create a \Drupal\Core\Url object directly, for example using
   *   Url::fromRoute().
578
   */
579
  public static function url($route_name, $route_parameters = [], $options = [], $collect_bubbleable_metadata = FALSE) {
580
    @trigger_error('Drupal::url() is deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Instead create a \Drupal\Core\Url object directly, for example using Url::fromRoute()', E_USER_DEPRECATED);
581
    return static::getContainer()->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options, $collect_bubbleable_metadata);
582 583
  }

584 585 586 587 588 589
  /**
   * Returns the link generator service.
   *
   * @return \Drupal\Core\Utility\LinkGeneratorInterface
   */
  public static function linkGenerator() {
590
    return static::getContainer()->get('link_generator');
591 592 593
  }

  /**
594 595 596
   * Renders a link with a given link text and Url object.
   *
   * This method is a convenience wrapper for the link generator service's
597
   * generate() method.
598
   *
599 600 601 602 603
   * @param string $text
   *   The link text for the anchor tag.
   * @param \Drupal\Core\Url $url
   *   The URL object used for the link.
   *
604 605 606
   * @return \Drupal\Core\GeneratedLink
   *   A GeneratedLink object containing a link to the given route and
   *   parameters and bubbleable metadata.
607
   *
608
   * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
609
   * @see \Drupal\Core\Url
610 611 612 613 614 615 616
   *
   * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
   *   Use \Drupal\Core\Link instead.
   *   Example:
   *   @code
   *     $link = Link::fromTextAndUrl($text, $url);
   *   @endcode
617
   */
618 619
  public static function l($text, Url $url) {
    return static::getContainer()->get('link_generator')->generate($text, $url);
620 621
  }

622 623 624
  /**
   * Returns the string translation service.
   *
625
   * @return \Drupal\Core\StringTranslation\TranslationManager
626 627 628
   *   The string translation manager.
   */
  public static function translation() {
629
    return static::getContainer()->get('string_translation');
630 631
  }

632 633 634
  /**
   * Returns the language manager service.
   *
635
   * @return \Drupal\Core\Language\LanguageManagerInterface
636 637 638
   *   The language manager.
   */
  public static function languageManager() {
639
    return static::getContainer()->get('language_manager');
640 641
  }

642 643 644
  /**
   * Returns the CSRF token manager service.
   *
645 646 647 648 649
   * The generated token is based on the session ID of the current user. Normally,
   * anonymous users do not have a session, so the generated token will be
   * different on every page request. To generate a token for users without a
   * session, manually start a session prior to calling this function.
   *
650 651
   * @return \Drupal\Core\Access\CsrfTokenGenerator
   *   The CSRF token manager.
652
   *
653
   * @see \Drupal\Core\Session\SessionManager::start()
654 655
   */
  public static function csrfToken() {
656
    return static::getContainer()->get('csrf_token');
657 658
  }

659 660 661
  /**
   * Returns the transliteration service.
   *
662
   * @return \Drupal\Core\Transliteration\PhpTransliteration
663 664 665
   *   The transliteration manager.
   */
  public static function transliteration() {
666
    return static::getContainer()->get('transliteration');
667 668
  }

669 670 671 672 673 674 675
  /**
   * Returns the form builder service.
   *
   * @return \Drupal\Core\Form\FormBuilderInterface
   *   The form builder.
   */
  public static function formBuilder() {
676
    return static::getContainer()->get('form_builder');
677 678
  }

679 680 681 682 683 684
  /**
   * Gets the theme service.
   *
   * @return \Drupal\Core\Theme\ThemeManagerInterface
   */
  public static function theme() {
685
    return static::getContainer()->get('theme.manager');
686 687
  }

688 689 690 691 692 693
  /**
   * Gets the syncing state.
   *
   * @return bool
   *   Returns TRUE is syncing flag set.
   */
694
  public static function isConfigSyncing() {
695
    return static::getContainer()->get('config.installer')->isSyncing();
696 697
  }

698 699 700 701 702 703 704
  /**
   * Returns a channel logger object.
   *
   * @param string $channel
   *   The name of the channel. Can be any string, but the general practice is
   *   to use the name of the subsystem calling this.
   *
705
   * @return \Psr\Log\LoggerInterface
706 707 708
   *   The logger for this channel.
   */
  public static function logger($channel) {
709
    return static::getContainer()->get('logger.factory')->get($channel);
710 711
  }

712 713 714 715 716 717 718
  /**
   * Returns the menu tree.
   *
   * @return \Drupal\Core\Menu\MenuLinkTreeInterface
   *   The menu tree.
   */
  public static function menuTree() {
719
    return static::getContainer()->get('menu.link_tree');
720 721
  }

722 723 724 725 726 727
  /**
   * Returns the path validator.
   *
   * @return \Drupal\Core\Path\PathValidatorInterface
   */
  public static function pathValidator() {
728
    return static::getContainer()->get('path.validator');
729 730
  }

731 732 733 734 735 736 737
  /**
   * Returns the access manager service.
   *
   * @return \Drupal\Core\Access\AccessManagerInterface
   *   The access manager service.
   */
  public static function accessManager() {
738
    return static::getContainer()->get('access_manager');
739 740
  }

741 742 743 744 745 746 747 748 749 750
  /**
   * Returns the redirect destination helper.
   *
   * @return \Drupal\Core\Routing\RedirectDestinationInterface
   *   The redirect destination helper.
   */
  public static function destination() {
    return static::getContainer()->get('redirect.destination');
  }

751 752 753 754 755 756 757 758 759 760
  /**
   * Returns the entity definition update manager.
   *
   * @return \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
   *   The entity definition update manager.
   */
  public static function entityDefinitionUpdateManager() {
    return static::getContainer()->get('entity.definition_update_manager');
  }

761 762 763 764 765 766 767 768 769 770
  /**
   * Returns the time service.
   *
   * @return \Drupal\Component\Datetime\TimeInterface
   *   The time service.
   */
  public static function time() {
    return static::getContainer()->get('datetime.time');
  }

771 772 773 774 775 776 777 778 779
  /**
   * Returns the messenger.
   *
   * @return \Drupal\Core\Messenger\MessengerInterface
   *   The messenger.
   */
  public static function messenger() {
    // @todo Replace with service once LegacyMessenger is removed in 9.0.0.
    // @see https://www.drupal.org/node/2928994
780
    return new LegacyMessenger();
781 782
  }

783
}