diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 6954c4d..1a35928 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -902,6 +902,23 @@ function variable_del($name) { } /** + * Retrieve an object from the Drupal Container. + * + * Example: + * @code + * $language_instance = drupal_container('language_instance'); + * @endcode + * + * @param string $name + * The name of the service to retrieve. + * + * @see Drupal\Core\Container + */ +function drupal_container($name) { + return Drupal\Core\Container::getInstance()->get($name); +} + +/** * Retrieves the current page from the cache. * * Note: we do not serve cached pages to authenticated users, or to anonymous diff --git a/core/lib/Drupal/Core/Container.php b/core/lib/Drupal/Core/Container.php new file mode 100644 index 0000000..a794f97 --- /dev/null +++ b/core/lib/Drupal/Core/Container.php @@ -0,0 +1,55 @@ +get('language_instance'); + * @endcode + */ +class Container implements ContainerBuilder { + + /** + * The singleton object kept for the Container. + */ + private static $instance; + + /** + * Create the Drupal Container, making sure to register any initial mappings. + * + * @todo Once the Kernel patch is in, turn this into an event dispatcher to + * have objects register themselves. + */ + function __construct() { + parent::__construct(); + + // Register the Language mappings. + $languageDefinition = new Definition('Drupal\\Core\\Locale\\Language'); + $this->setDefinition('language_interface', $languageDefinition); + } + + /** + * Retrieves the singleton Drupal Container. + * + * @todo Once the Kernel patch is in, we may move the Container instance + * itself into the Kernel class. + */ + public static function getInstance() { + if (!isset(self::$instance)) { + self::$instance = new Container(); + } + return self::$instance; + } +} diff --git a/core/lib/Drupal/Core/Locale/Language.php b/core/lib/Drupal/Core/Locale/Language.php new file mode 100644 index 0000000..ee2834f --- /dev/null +++ b/core/lib/Drupal/Core/Locale/Language.php @@ -0,0 +1,34 @@ +name = $name; + $this->langcode = $langcode; + $this->direction = $direction; + $this->enabled = $direction; + $this->weight = $weight; + $this->method_id = $method_id; + } +} diff --git a/core/modules/simpletest/tests/bootstrap.test b/core/modules/simpletest/tests/bootstrap.test index 371031a..f64fdfe 100644 --- a/core/modules/simpletest/tests/bootstrap.test +++ b/core/modules/simpletest/tests/bootstrap.test @@ -520,3 +520,43 @@ class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase { } } +/** + * Tests for overriding server variables via the API. + */ +class BootstrapDependencyInjectionTestCase extends DrupalUnitTestCase { + public static function getInfo() { + return array( + 'name' => 'Dependency Injection system', + 'description' => 'Test that the dependency injection system is functional.', + 'group' => 'Bootstrap', + ); + } + + function setUp() { + parent::setUp(); + // We're a unit test using classes... don't let the db registry blow up + // PSR-0 classes. + spl_autoload_unregister('drupal_autoload_class'); + spl_autoload_unregister('drupal_autoload_interface'); + } + + function tearDown() { + parent::tearDown(); + // We removed the db autoloader to allow us to unit test but re-register it + // so we don't break other tests. + spl_autoload_register('drupal_autoload_class'); + spl_autoload_register('drupal_autoload_interface'); + } + + /** + * Test providing a direct URL to to drupal_override_server_variables(). + */ + function testRetrieveLanguageInterface() { + // Since the default language_interface service is the default Language, we + // will if they are the same via the Drupal Container. + $expected = new Drupal\Core\Locale\Language(); + $result = drupal_container('language_interface'); + $this->assertIdentical($expected, $result); + } +} +