Commit 8ad648f1 authored by catch's avatar catch

Issue #2182439 by tim.plunkett: Provide a \Drupal::hasService() method.

parent bcc57baf
......@@ -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();
......
......@@ -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.
......
......@@ -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.
*
......
......@@ -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 {
......
......@@ -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.');
......
......@@ -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.');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment