diff --git a/core/authorize.php b/core/authorize.php index fe39394..a18d2d8 100644 --- a/core/authorize.php +++ b/core/authorize.php @@ -20,6 +20,8 @@ * @link authorize Authorized operation helper functions @endlink */ +use Symfony\Component\HttpFoundation\Request; + // Change the directory to the Drupal root. chdir('..'); @@ -68,7 +70,9 @@ function authorize_access_allowed() { // We prepare only a minimal bootstrap. This includes the database and // variables, however, so we have access to the class autoloader. drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES); - +// A request object from the HTTPFoundation to tell us about the request. +$request = Request::createFromGlobals(); +Drupal::getContainer()->set('request', $request); // This must go after drupal_bootstrap(), which unsets globals! global $conf; diff --git a/core/core.services.yml b/core/core.services.yml index 640440b..3095e5b 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -177,11 +177,7 @@ services: arguments: ['@container.namespaces', '@controller_resolver', '@request', '@router.route_provider', '@module_handler'] request: class: Symfony\Component\HttpFoundation\Request - # @TODO the synthetic setting must be uncommented whenever drupal_session_initialize() - # is run after there is a request and the following two lines should be removed. - factory_class: Symfony\Component\HttpFoundation\Request - factory_method: createFromGlobals - #synthetic: true + synthetic: true event_dispatcher: class: Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher arguments: ['@service_container'] diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index f7ab2d7..d96be2b 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -1574,7 +1574,7 @@ function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG $log_entry['referer'] = $request->headers->get('Referer', ''); $log_entry['ip'] = $request->getClientIP(); } - catch (\InvalidArgumentException $e) { + catch (DependencyInjectionRuntimeException $e) { // We are not in a request context. } @@ -1882,10 +1882,13 @@ function drupal_handle_request($test_only = FALSE) { // @todo Remove this once everything in the bootstrap has been // converted to services in the DIC. $kernel->boot(); - drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE); // Create a request object from the HttpFoundation. $request = Request::createFromGlobals(); + Drupal::getContainer()->set('request', $request); + + drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE); + $response = $kernel->handle($request)->prepare($request)->send(); $kernel->terminate($request, $response); @@ -2011,10 +2014,12 @@ function _drupal_bootstrap_configuration() { */ function _drupal_bootstrap_kernel() { // Normally, index.php puts a container in drupal_container() by creating a - // kernel. If there is no container yet, create one. - if (!drupal_container()) { + // kernel. If there is no container yet, create one and set the request. + if (!Drupal::getContainer()) { $kernel = new DrupalKernel('prod', drupal_classloader()); $kernel->boot(); + $request = Request::createFromGlobals(); + Drupal::getContainer()->set('request', $request); } } diff --git a/core/includes/install.inc b/core/includes/install.inc index e0e86cc..dc99134 100644 --- a/core/includes/install.inc +++ b/core/includes/install.inc @@ -617,10 +617,19 @@ function drupal_install_system() { // Create tables. drupal_install_schema('system'); - if (!drupal_container()->has('kernel')) { - // Immediately boot a kernel to have real services ready. + if (!Drupal::getContainer()->has('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. + if (Drupal::getContainer()->initialized('request')) { + $request = Drupal::request(); + } $kernel = new DrupalKernel('install', drupal_classloader(), FALSE); $kernel->boot(); + if (isset($request)) { + Drupal::getContainer()->set('request', $request); + } + } $system_path = drupal_get_path('module', 'system'); diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index af1fe52..f0d2566 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -382,10 +382,17 @@ protected function getKernelParameters() { */ protected function initializeContainer() { $persist = $this->getServicesToPersist(); - // If we are rebuilding the kernel and we are in a request scope, store - // request info so we can add them back after the rebuild. - if (isset($this->container) && $this->container->hasScope('request')) { - $request = $this->container->get('request'); + // The request service requires custom persisting logic, since it is also + // potentially scoped. During Drupal installation, there is a request + // service without a request scope. + $request_scope = FALSE; + if (isset($this->container)) { + if ($this->container->hasScope('request') && $this->container->isScopeActive('request')) { + $request_scope = TRUE; + } + if ($this->container->initialized('request')) { + $request = $this->container->get('request'); + } } $this->container = NULL; $class = $this->getClassName(); @@ -458,8 +465,10 @@ protected function initializeContainer() { // Set the class loader which was registered as a synthetic service. $this->container->set('class_loader', $this->classLoader); // If we have a request set it back to the new container. - if (isset($request)) { + if ($request_scope) { $this->container->enterScope('request'); + } + if (isset($request)) { $this->container->set('request', $request); } \Drupal::setContainer($this->container); diff --git a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php index 54305c0..c0ce1b2 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php @@ -13,6 +13,7 @@ use Symfony\Component\DependencyInjection\Reference; use Drupal\Core\Database\Database; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; /** * Base test case class for Drupal unit tests. @@ -183,7 +184,8 @@ public function containerBuild(ContainerBuilder $container) { $definition = $container->getDefinition('path_processor_alias'); $definition->clearTag('path_processor_inbound')->clearTag('path_processor_outbound'); } - + $request = Request::createFromGlobals(); + $this->container->set('request', $request); } /** diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index 4780e85..700b4e5 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -19,6 +19,7 @@ use Drupal\Core\Language\Language; use ReflectionMethod; use ReflectionObject; +use Symfony\Component\HttpFoundation\Request; /** * Base class for Drupal tests. @@ -922,7 +923,9 @@ protected function prepareEnvironment() { $this->container = new ContainerBuilder(); // @todo Remove this once this class has no calls to t() and format_plural() $this->container->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager'); - + $request = Request::createFromGlobals(); + $request->attributes->set('account', $GLOBALS['user']); + $this->container->set('request', $request); \Drupal::setContainer($this->container); // Unset globals. @@ -989,7 +992,10 @@ protected function rebuildContainer() { $this->kernel->boot(); // DrupalKernel replaces the container in drupal_container() with a // different object, so we need to replace the instance on this test class. - $this->container = drupal_container(); + $this->container = \Drupal::getContainer(); + $request = Request::createFromGlobals(); + $request->attributes->set('account', $GLOBALS['user']); + $this->container->set('request', $request); } /** diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index 49b1a44..15b3325 100755 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -30,6 +30,10 @@ // Bootstrap to perform initial validation or other operations. drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE); simpletest_classloader_register(); +// We have to add a Request. +$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); +$container = Drupal::getContainer(); +$container->set('request', $request); if (!module_exists('simpletest')) { simpletest_script_print_error("The simpletest module must be enabled before this script can run."); @@ -472,8 +476,11 @@ function simpletest_script_run_one_test($test_id, $test_class) { try { // Bootstrap Drupal. drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE); - simpletest_classloader_register(); + // We have to add a Request. + $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); + $container = Drupal::getContainer(); + $container->set('request', $request); // Override configuration according to command line parameters. $conf['simpletest.settings']['verbose'] = $args['verbose']; diff --git a/core/update.php b/core/update.php index fba6b86..b30e23e 100644 --- a/core/update.php +++ b/core/update.php @@ -428,15 +428,12 @@ function update_check_requirements($skip_warnings = FALSE) { // Determine if the current user has access to run update.php. drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES); -require_once DRUPAL_ROOT . '/' . settings()->get('session_inc', 'core/includes/session.inc'); -drupal_session_initialize(); - // A request object from the HTTPFoundation to tell us about the request. -// @todo These two lines were copied from index.php which has its own todo about -// a change required here. Revisit this when that change has been made. $request = Request::createFromGlobals(); -drupal_container() - ->set('request', $request); +Drupal::getContainer()->set('request', $request); + +require_once DRUPAL_ROOT . '/' . settings()->get('session_inc', 'core/includes/session.inc'); +drupal_session_initialize(); // There can be conflicting 'op' parameters because both update and batch use // this parameter name. We need the 'op' coming from a POST request to trump