diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index cd65323..8a6a365 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -2654,57 +2654,13 @@ function drupal_language_initialize() { /** * Returns the language object for a given language type. * - * The 'language_manager' service is only available within the scope of a kernel - * request. When it's not available, we return a default language object, - * regardless of the type passed in. - * * @see Drupal\Core\Language\LanguageManager * * @param string $type * The type of language object needed, e.g. LANGUAGE_TYPE_INTERFACE. - * @param bool $reset - * TRUE to reset the statically cached language object for the type, or for - * all types if $type is NULL. - */ -function language($type, $reset = FALSE) { - // We don't use drupal_static() here because resetting is not a simple case of - // drupal_static_reset(). - static $languages = array(); - - // Reset the language manager's cache and our own. - if ($reset) { - if (drupal_container()->isScopeActive('request')) { - drupal_container()->get('language_manager')->reset($type); - } - if (!isset($type)) { - $languages = array(); - } - elseif (isset($languages[$type])) { - unset($languages[$type]); - } - } - - // If no type is passed (most likely when resetting all types), return. - if (!isset($type)) { - return; - } - - // When the language_manager service exists (is both defined and the 'request' - // scope is active in the container), use it to get the language. Otherwise - // return the default language. - if (drupal_container()->isScopeActive('request')) { - $language_manager = drupal_container()->get('language_manager', Container::NULL_ON_INVALID_REFERENCE); - } - - if (isset($language_manager)) { - return $language_manager->getLanguage($type); - } - else { - if (!isset($languages[$type])) { - $languages[$type] = language_default(); - } - return $languages[$type]; - } + */ +function language($type) { + return drupal_container()->get('language_manager')->getLanguage($type); } /** diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 7f4b26b..05ea4df 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -338,6 +338,8 @@ function install_begin_request(&$install_state) { $container->register('config.factory', 'Drupal\Core\Config\ConfigFactory') ->addArgument(new Reference('config.storage')) ->addArgument(new Reference('event_dispatcher')); + $container->register('language_manager', 'Drupal\Core\Language\LanguageManager'); + // The install process cannot use the database lock backend since the database // is not fully up, so we use a null backend implementation during the // installation process. This will also speed up the installation process. diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index e9d6d2f..8d5ddce 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -118,9 +118,8 @@ public function build(ContainerBuilder $container) { ->addArgument(new Reference('event_dispatcher')) ->addArgument(new Reference('service_container')) ->addArgument(new Reference('controller_resolver')); - $container->register('language_manager', 'Drupal\Core\Language\LanguageManager') - ->addArgument(new Reference('request')) - ->setScope('request'); + $container->register('language_manager', 'Drupal\Core\Language\LanguageManager'); + $container->register('database.slave', 'Drupal\Core\Database\Connection') ->setFactoryClass('Drupal\Core\Database\Database') ->setFactoryMethod('getConnection') @@ -229,6 +228,9 @@ public function build(ContainerBuilder $container) { ->addTag('event_subscriber'); $container->register('config_global_override_subscriber', 'Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber') ->addTag('event_subscriber'); + $container->register('language_request_subscriber', 'Drupal\Core\EventSubscriber\LanguageRequestSubscriber') + ->addArgument(new Reference('language_manager')) + ->addTag('event_subscriber'); $container->register('exception_controller', 'Drupal\Core\ExceptionController') ->addArgument(new Reference('content_negotiation')) diff --git a/core/lib/Drupal/Core/EventSubscriber/LanguageRequestSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/LanguageRequestSubscriber.php new file mode 100644 index 0000000..ace2976 --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/LanguageRequestSubscriber.php @@ -0,0 +1,50 @@ +languageManager = $language_manager; + } + + /** + * Sets the request on the language manager. + * + * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event + * The Event to process. + */ + public function onKernelRequestLanguage(GetResponseEvent $event) { + if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { + $this->languageManager->setRequest($event->getRequest()); + } + } + + /** + * Registers the methods in this class that should be listeners. + * + * @return array + * An array of event listener definitions. + */ + static function getSubscribedEvents() { + $events[KernelEvents::REQUEST][] = array('onKernelRequestLanguage', 300); + + return $events; + } +} diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index ff0177d..ce78a00 100644 --- a/core/lib/Drupal/Core/Language/LanguageManager.php +++ b/core/lib/Drupal/Core/Language/LanguageManager.php @@ -26,13 +26,18 @@ public function __construct(Request $request = NULL) { $this->request = $request; } + public function setRequest(Request $request) { + $this->request = $request; + $this->reset(); + } + public function getLanguage($type) { if (isset($this->languages[$type])) { return $this->languages[$type]; } // @todo Objectify the language system so that we don't have to do this. - if (language_multilingual()) { + if (language_multilingual() && $this->request) { include_once DRUPAL_ROOT . '/core/includes/language.inc'; $this->languages[$type] = language_types_initialize($type, $this->request); } diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php index f72c51a..92a419b 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php @@ -35,7 +35,7 @@ function setUp() { parent::setUp(); // Ensure we are building a new Language object for each test. - language(NULL, TRUE); + $this->container->get('language_manager')->reset(); } diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php index e22e65d..fee58c6 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php @@ -56,8 +56,8 @@ function testUninstallProcess() { 'default' => $this->langcode == 'fr', )); language_save($language); - // Reset statically cached language objects. - language(NULL, TRUE); + // Reset the language manager. + $this->container->get('language_manager')->reset(); // Check the UI language. drupal_language_initialize(); $this->assertEqual(language(LANGUAGE_TYPE_INTERFACE)->langcode, $this->langcode, t('Current language: %lang', array('%lang' => language(LANGUAGE_TYPE_INTERFACE)->langcode))); @@ -104,8 +104,8 @@ function testUninstallProcess() { // Visit the front page. $this->drupalGet(''); - // Reset statically cached language objects. - language(NULL, TRUE); + // Reset the language manager. + $this->container->get('language_manager')->reset(); // Check the init language logic. drupal_language_initialize(); $this->assertEqual(language(LANGUAGE_TYPE_INTERFACE)->langcode, 'en', t('Language after uninstall: %lang', array('%lang' => language(LANGUAGE_TYPE_INTERFACE)->langcode))); diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index e8068fa..fdcf84a 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -1015,13 +1015,6 @@ protected function tearDown() { // this second reset is guranteed to reset everything to nothing. drupal_static_reset(); - // Reset static in language(). - // Restoring drupal_container() makes language() return the proper languages - // already, but it contains an additional static that needs to be reset. The - // reset can happen before the container is restored, as it is unnecessary - // to reset the language_manager service. - language(NULL, TRUE); - // Restore original in-memory configuration. $conf = $this->originalConf; new Settings($this->originalSettings); diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php index 106d726..5e54e46 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php @@ -289,6 +289,7 @@ protected function performUpgrade($register_errors = TRUE) { * @see WebTestBase::drupalGet() */ protected function getUpdatePhp() { + $this->rebuildContainer(); $path = $GLOBALS['base_url'] . '/core/update.php'; $out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_URL => $path, CURLOPT_NOBODY => FALSE)); // Ensure that any changes to variables in the other thread are picked up.