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 \Drupal\Core\CoreServiceProvider, the service providers of enabled modules and any other service providers defined in $GLOBALS['conf']['container_service_providers'].
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.
// Legacy procedural code. function hook_do_stuff() { $lock = lock()->acquire('stuff_lock'); // ... } // Correct procedural code. function hook_do_stuff() { $lock = \Drupal::lock()->acquire('stuff_lock'); // ... } // The preferred way: dependency injected code. function hook_do_stuff() { // Move the actual implementation to a class and instantiate it. $instance = new StuffDoingClass(\Drupal::lock()); $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. \Drupal::service('stuff.doing')->doStuff(); } interface StuffDoingInterface { public function doStuff(); } class StuffDoingClass implements StuffDoingInterface { protected $lockBackend; public function __construct(LockBackendInterface $lock_backend) { $this->lockBackend = $lock_backend; } public function doStuff() { $lock = $this->lockBackend->acquire('stuff_lock'); // ... } }
Name | Modifiers | Type | Description |
---|---|---|---|
Drupal::$container | protected static | property | The currently active container object, or NULL if not initialized yet. |
Drupal::accessManager | public static | function | Returns the access manager service. |
Drupal::cache | public static | function | Returns the requested cache bin. |
Drupal::config | public static | function | Retrieves a configuration object. |
Drupal::configFactory | public static | function | Retrieves the configuration factory. |
Drupal::CORE_COMPATIBILITY | constant | Core API compatibility. | |
Drupal::CORE_MINIMUM_SCHEMA_VERSION | constant | Core minimum schema version. | |
Drupal::csrfToken | public static | function | Returns the CSRF token manager service. |
Drupal::currentUser | public static | function | Gets the current active user. |
Drupal::database | public static | function | Returns the current primary database. |
Drupal::destination | public static | function | Returns the redirect destination helper. |
Drupal::entityDefinitionUpdateManager | public static | function | Returns the entity definition update manager. |
Drupal::entityManager Deprecated | public static | function | Retrieves the entity manager service. |
Drupal::entityQuery | public static | function | Returns the entity query object for this entity type. |
Drupal::entityQueryAggregate | public static | function | Returns the entity query aggregate object for this entity type. |
Drupal::entityTypeManager | public static | function | Retrieves the entity type manager. |
Drupal::flood | public static | function | Returns the flood instance. |
Drupal::formBuilder | public static | function | Returns the form builder service. |
Drupal::getContainer | public static | function | Returns the currently active global container. |
Drupal::hasContainer | public static | function | Returns TRUE if the container has been initialized, FALSE otherwise. |
Drupal::hasRequest | public static | function | Indicates if there is a currently active request object. |
Drupal::hasService | public static | function | Indicates if a service is defined in the container. |
Drupal::httpClient | public static | function | Returns the default http client. |
Drupal::isConfigSyncing | public static | function | Gets the syncing state. |
Drupal::keyValue | public static | function | Returns a key/value storage collection. |
Drupal::keyValueExpirable | public static | function | Returns an expirable key value store collection. |
Drupal::l Deprecated | public static | function | Renders a link with a given link text and Url object. |
Drupal::languageManager | public static | function | Returns the language manager service. |
Drupal::linkGenerator | public static | function | Returns the link generator service. |
Drupal::lock | public static | function | Returns the locking layer instance. |
Drupal::logger | public static | function | Returns a channel logger object. |
Drupal::menuTree | public static | function | Returns the menu tree. |
Drupal::moduleHandler | public static | function | Returns the module handler. |
Drupal::pathValidator | public static | function | Returns the path validator. |
Drupal::queue | public static | function | Returns a queue for the given queue name. |
Drupal::request | public static | function | Retrieves the currently active request object. |
Drupal::requestStack | public static | function | Retrives the request stack. |
Drupal::root | public static | function | Gets the app root. |
Drupal::routeMatch | public static | function | Retrieves the currently active route match object. |
Drupal::service | public static | function | Retrieves a service from the container. |
Drupal::setContainer | public static | function | Sets a new global container. |
Drupal::state | public static | function | Returns the state storage service. |
Drupal::theme | public static | function | Gets the theme service. |
Drupal::token | public static | function | Returns the token service. |
Drupal::translation | public static | function | Returns the string translation service. |
Drupal::transliteration | public static | function | Returns the transliteration service. |
Drupal::typedDataManager | public static | function | Returns the typed data manager service. |
Drupal::unsetContainer | public static | function | Unsets the global container. |
Drupal::url Deprecated | public static | function | Generates a URL string for a specific route based on the given parameters. |
Drupal::urlGenerator | public static | function | Returns the url generator service. |
Drupal::VERSION | constant | The current system version. |
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal.php/class/Drupal/8.1.x