summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxjm2016-09-02 15:25:16 -0500
committerxjm2016-09-02 15:25:16 -0500
commit9d3d930151c6af5fc8d5876355bebaced1dbe640 (patch)
treee6b4cae2def763e923e8d0b7467a311e46dabab7
parentea28d66948b2a75dbea891326586572c6422196d (diff)
Issue #2785355 by timmillwood, dawehner, claudiu.cristea, borisson_: Expose the class resolver on \Drupal
-rw-r--r--core/lib/Drupal.php17
-rw-r--r--core/tests/Drupal/Tests/Core/DrupalTest.php12
2 files changed, 28 insertions, 1 deletions
diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php
index f62e914..bc21709 100644
--- a/core/lib/Drupal.php
+++ b/core/lib/Drupal.php
@@ -300,6 +300,23 @@ class Drupal {
}
/**
+ * 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.
*
* @param string $collection
diff --git a/core/tests/Drupal/Tests/Core/DrupalTest.php b/core/tests/Drupal/Tests/Core/DrupalTest.php
index d4e3565..de94bf8 100644
--- a/core/tests/Drupal/Tests/Core/DrupalTest.php
+++ b/core/tests/Drupal/Tests/Core/DrupalTest.php
@@ -102,7 +102,7 @@ class DrupalTest extends UnitTestCase {
}
/**
- * Tests the service() method.
+ * Tests the cache() method.
*
* @covers ::cache
*/
@@ -112,6 +112,16 @@ class DrupalTest extends UnitTestCase {
}
/**
+ * Tests the classResolver method.
+ *
+ * @covers ::classResolver
+ */
+ public function testClassResolver() {
+ $this->setMockContainerService('class_resolver');
+ $this->assertNotNull(\Drupal::classResolver());
+ }
+
+ /**
* Tests the keyValueExpirable() method.
*
* @covers ::keyValueExpirable