From 8ad648f14ad860f7ba59449e13f112ffb6b278bb Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Wed, 5 Feb 2014 21:14:01 +0000 Subject: [PATCH] Issue #2182439 by tim.plunkett: Provide a \Drupal::hasService() method. --- core/includes/bootstrap.inc | 2 +- core/includes/install.inc | 2 +- core/lib/Drupal.php | 13 +++++++++++++ core/lib/Drupal/Core/Form/FormBuilder.php | 2 +- .../system/Tests/Bootstrap/GetFilenameUnitTest.php | 2 +- .../Tests/ServiceProvider/ServiceProviderTest.php | 9 ++++----- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index f754b4268166..c40244f86595 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -709,7 +709,7 @@ function drupal_get_filename($type, $name, $filename = NULL) { // Verify that we have an keyvalue service before using it. This is required // because this function is called during installation. // @todo Inject database connection into KeyValueStore\DatabaseStorage. - if (($container = \Drupal::getContainer()) && $container->has('keyvalue') && function_exists('db_query')) { + if (\Drupal::hasService('keyvalue') && function_exists('db_query')) { if ($type == 'module') { if (empty($files[$type])) { $files[$type] = \Drupal::moduleHandler()->getModuleList(); diff --git a/core/includes/install.inc b/core/includes/install.inc index f3fbdf0bf8f2..895f06859c6b 100644 --- a/core/includes/install.inc +++ b/core/includes/install.inc @@ -613,7 +613,7 @@ function drupal_install_system($install_state) { // Create tables. drupal_install_schema('system'); - if (!\Drupal::getContainer()->has('kernel')) { + if (!\Drupal::hasService('kernel')) { // Immediately boot a kernel to have real services ready. If there's already // an initialized request object in the pre-kernel container, persist it in // the post-kernel container. diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index cc689374b433..4106c5b52be5 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -136,6 +136,19 @@ public static function service($id) { return static::$container->get($id); } + /** + * 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) { + return static::$container && static::$container->has($id); + } + /** * Retrieves the currently active request object. * diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 43d15a65bea4..cd82931df185 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -1810,7 +1810,7 @@ protected function drupalStaticReset($name = NULL) { */ protected function currentUser() { if (!$this->currentUser) { - if (\Drupal::getContainer()->has('current_user')) { + if (\Drupal::hasService('current_user')) { $this->currentUser = \Drupal::currentUser(); } else { diff --git a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/GetFilenameUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/GetFilenameUnitTest.php index 702ee3b653c4..4701ce86dd4b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/GetFilenameUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/GetFilenameUnitTest.php @@ -33,7 +33,7 @@ function testDrupalGetFilename() { // Assert that the test is meaningful by making sure the keyvalue service // does not exist. - $this->assertFalse(\Drupal::getContainer()->has('keyvalue'), 'The container has no keyvalue service.'); + $this->assertFalse(\Drupal::hasService('keyvalue'), 'The container has no keyvalue service.'); // Retrieving the location of a module. $this->assertIdentical(drupal_get_filename('module', 'xmlrpc'), 'core/modules/xmlrpc/xmlrpc.module', 'Retrieve module location.'); diff --git a/core/modules/system/lib/Drupal/system/Tests/ServiceProvider/ServiceProviderTest.php b/core/modules/system/lib/Drupal/system/Tests/ServiceProvider/ServiceProviderTest.php index 527b57e9a53b..32111ba40244 100644 --- a/core/modules/system/lib/Drupal/system/Tests/ServiceProvider/ServiceProviderTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/ServiceProvider/ServiceProviderTest.php @@ -33,9 +33,8 @@ public static function getInfo() { * Tests that services provided by module service providers get registered to the DIC. */ function testServiceProviderRegistration() { - $container = \Drupal::getContainer(); - $this->assertTrue($container->getDefinition('file.usage')->getClass() == 'Drupal\\service_provider_test\\TestFileUsage', 'Class has been changed'); - $this->assertTrue($container->has('service_provider_test_class'), 'The service_provider_test_class service has been registered to the DIC'); + $this->assertTrue(\Drupal::getContainer()->getDefinition('file.usage')->getClass() == 'Drupal\\service_provider_test\\TestFileUsage', 'Class has been changed'); + $this->assertTrue(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service has been registered to the DIC'); // The event subscriber method in the test class calls drupal_set_message with // a message saying it has fired. This will fire on every page request so it // should show up on the front page. @@ -49,11 +48,11 @@ function testServiceProviderRegistration() { function testServiceProviderRegistrationDynamic() { // Uninstall the module and ensure the service provider's service is not registered. \Drupal::moduleHandler()->uninstall(array('service_provider_test')); - $this->assertFalse(\Drupal::getContainer()->has('service_provider_test_class'), 'The service_provider_test_class service does not exist in the DIC.'); + $this->assertFalse(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service does not exist in the DIC.'); // Install the module and ensure the service provider's service is registered. \Drupal::moduleHandler()->install(array('service_provider_test')); - $this->assertTrue(\Drupal::getContainer()->has('service_provider_test_class'), 'The service_provider_test_class service exists in the DIC.'); + $this->assertTrue(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service exists in the DIC.'); } } -- GitLab