From 0613dbf5ad1b8e79d2d48e295a9ea81958bd6ad4 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Tue, 20 Mar 2018 11:47:25 +0000 Subject: [PATCH] Issue #2949965 by alexpott, Lendude: \Drupal::classResolver() could be more helpful --- core/lib/Drupal.php | 16 +++++++++++++--- core/modules/field_layout/field_layout.module | 6 ++---- .../inline_form_errors.module | 3 +-- .../demo_umami_content.install | 4 ++-- core/tests/Drupal/Tests/Core/DrupalTest.php | 18 ++++++++++++++++-- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index 61982c38851..e0b08670b78 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -320,10 +320,20 @@ class Drupal { * 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. + * @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. */ - public static function classResolver() { + public static function classResolver($class = NULL) { + if ($class) { + return static::getContainer()->get('class_resolver')->getInstanceFromDefinition($class); + } return static::getContainer()->get('class_resolver'); } diff --git a/core/modules/field_layout/field_layout.module b/core/modules/field_layout/field_layout.module index 5a5fa11a49b..713e7e4aac9 100644 --- a/core/modules/field_layout/field_layout.module +++ b/core/modules/field_layout/field_layout.module @@ -50,8 +50,7 @@ function field_layout_entity_type_alter(array &$entity_types) { */ function field_layout_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { if ($display instanceof EntityDisplayWithLayoutInterface) { - \Drupal::classResolver()->getInstanceFromDefinition(FieldLayoutBuilder::class) - ->buildView($build, $display); + \Drupal::classResolver(FieldLayoutBuilder::class)->buildView($build, $display); } } @@ -62,8 +61,7 @@ function field_layout_form_alter(&$form, FormStateInterface $form_state, $form_i $form_object = $form_state->getFormObject(); if ($form_object instanceof ContentEntityFormInterface && $display = $form_object->getFormDisplay($form_state)) { if ($display instanceof EntityDisplayWithLayoutInterface) { - \Drupal::classResolver()->getInstanceFromDefinition(FieldLayoutBuilder::class) - ->buildForm($form, $display); + \Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form, $display); } } } diff --git a/core/modules/inline_form_errors/inline_form_errors.module b/core/modules/inline_form_errors/inline_form_errors.module index dcb140b7a51..3bacd37b93d 100644 --- a/core/modules/inline_form_errors/inline_form_errors.module +++ b/core/modules/inline_form_errors/inline_form_errors.module @@ -59,8 +59,7 @@ function inline_form_errors_preprocess_datetime_wrapper(&$variables) { * Implements hook_element_info_alter(). */ function inline_form_errors_element_info_alter(array &$info) { - \Drupal::classResolver()->getInstanceFromDefinition(RenderElementHelper::class) - ->alterElementInfo($info); + \Drupal::classResolver(RenderElementHelper::class)->alterElementInfo($info); } /** diff --git a/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install b/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install index 99ea0d1be55..8b94cc82f58 100644 --- a/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install +++ b/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install @@ -12,7 +12,7 @@ use Drupal\demo_umami_content\InstallHelper; */ function demo_umami_content_install() { if (!\Drupal::service('config.installer')->isSyncing()) { - \Drupal::classResolver()->getInstanceFromDefinition(InstallHelper::class)->importContent(); + \Drupal::classResolver(InstallHelper::class)->importContent(); } } @@ -21,6 +21,6 @@ function demo_umami_content_install() { */ function demo_umami_content_uninstall() { if (!\Drupal::service('config.installer')->isSyncing()) { - \Drupal::classResolver()->getInstanceFromDefinition(InstallHelper::class)->deleteImportedContent(); + \Drupal::classResolver(InstallHelper::class)->deleteImportedContent(); } } diff --git a/core/tests/Drupal/Tests/Core/DrupalTest.php b/core/tests/Drupal/Tests/Core/DrupalTest.php index 2ff6ebf6666..53f40af8f3f 100644 --- a/core/tests/Drupal/Tests/Core/DrupalTest.php +++ b/core/tests/Drupal/Tests/Core/DrupalTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\Core; +use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\DependencyInjection\ContainerNotInitializedException; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -120,8 +121,21 @@ class DrupalTest extends UnitTestCase { * @covers ::classResolver */ public function testClassResolver() { - $this->setMockContainerService('class_resolver'); - $this->assertNotNull(\Drupal::classResolver()); + $class_resolver = $this->prophesize(ClassResolverInterface::class); + $this->setMockContainerService('class_resolver', $class_resolver->reveal()); + $this->assertInstanceOf(ClassResolverInterface::class, \Drupal::classResolver()); + } + + /** + * Tests the classResolver method when called with a class. + * + * @covers ::classResolver + */ + public function testClassResolverWithClass() { + $class_resolver = $this->prophesize(ClassResolverInterface::class); + $class_resolver->getInstanceFromDefinition(static::class)->willReturn($this); + $this->setMockContainerService('class_resolver', $class_resolver->reveal()); + $this->assertSame($this, \Drupal::classResolver(static::class)); } /**