diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index f62e91465aa9479e4a8e91252c63b7c11c31fa87..bc21709b50954ae448aadf3bd51e73865af45459 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -299,6 +299,23 @@ public static function cache($bin = 'default') { return static::getContainer()->get('cache.' . $bin); } + /** + * 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. + * + * @return \Drupal\Core\DependencyInjection\ClassResolverInterface + * The class resolver. + */ + public static function classResolver() { + return static::getContainer()->get('class_resolver'); + } + /** * Returns an expirable key value store collection. * diff --git a/core/tests/Drupal/Tests/Core/DrupalTest.php b/core/tests/Drupal/Tests/Core/DrupalTest.php index d4e3565ae08351281e6c09ae52797d8c086ee64e..de94bf856323d985c4c49c1ce86e9e0f011d699c 100644 --- a/core/tests/Drupal/Tests/Core/DrupalTest.php +++ b/core/tests/Drupal/Tests/Core/DrupalTest.php @@ -102,7 +102,7 @@ public function testDatabase() { } /** - * Tests the service() method. + * Tests the cache() method. * * @covers ::cache */ @@ -111,6 +111,16 @@ public function testCache() { $this->assertNotNull(\Drupal::cache('test')); } + /** + * Tests the classResolver method. + * + * @covers ::classResolver + */ + public function testClassResolver() { + $this->setMockContainerService('class_resolver'); + $this->assertNotNull(\Drupal::classResolver()); + } + /** * Tests the keyValueExpirable() method. *