From 35ef2c97ea6b7a032e9a073ef697a8a9c9786a9f Mon Sep 17 00:00:00 2001
From: effulgentsia <effulgentsia@78040.no-reply.drupal.org>
Date: Wed, 23 May 2012 23:52:03 -0500
Subject: [PATCH] refs #1536844 Clean up bootstrap shivs.

---
 core/includes/bootstrap.inc                   |  3 +--
 core/includes/common.inc                      | 25 +++++++++++--------
 .../LegacyRequestSubscriber.php               |  7 ++++++
 core/update.php                               |  6 +++--
 index.php                                     |  5 ++++
 5 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index e78b8b8e52ed..aa20e90ff8b9 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -138,8 +138,7 @@
 const DRUPAL_BOOTSTRAP_PAGE_HEADER = 5;
 
 /**
- * Seventh bootstrap phase: load code for subsystems and modules; validate and
- * fix input data.
+ * Seventh bootstrap phase: load code for subsystems and modules.
  */
 const DRUPAL_BOOTSTRAP_CODE = 6;
 
diff --git a/core/includes/common.inc b/core/includes/common.inc
index 375b91bc1d0e..be2278fda637 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -5160,13 +5160,10 @@ function drupal_valid_token($token, $value = '', $skip_anonymous = FALSE) {
   return (($skip_anonymous && $user->uid == 0) || ($token == drupal_get_token($value)));
 }
 
+/**
+ * Loads code for subsystems and modules, and registers stream wrappers.
+ */
 function _drupal_bootstrap_code() {
-  static $called = FALSE;
-
-  if ($called) {
-    return;
-  }
-  $called = TRUE;
   require_once DRUPAL_ROOT . '/' . variable_get('path_inc', 'core/includes/path.inc');
   require_once DRUPAL_ROOT . '/core/includes/theme.inc';
   require_once DRUPAL_ROOT . '/core/includes/pager.inc';
@@ -5183,19 +5180,22 @@ function _drupal_bootstrap_code() {
   require_once DRUPAL_ROOT . '/core/includes/errors.inc';
   require_once DRUPAL_ROOT . '/core/includes/schema.inc';
 
-  // Detect string handling method
+  // @todo Move this to earlier in bootstrap: http://drupal.org/node/1569456.
   unicode_check();
-  // Undo magic quotes
+
+  // @todo Remove this: http://drupal.org/node/1569456.
   fix_gpc_magic();
-  // Load all enabled modules
+
+  // Load all enabled modules.
   module_load_all();
+
   // Make sure all stream wrappers are registered.
   file_get_stream_wrappers();
 
+  // Now that stream wrappers are registered, log fatal errors from a simpletest
+  // child site to a test specific file directory.
   $test_info = &$GLOBALS['drupal_test_info'];
   if (!empty($test_info['in_child_site'])) {
-    // Running inside the simpletest child site, log fatal errors to test
-    // specific file directory.
     ini_set('log_errors', 1);
     ini_set('error_log', 'public://error.log');
   }
@@ -5205,6 +5205,9 @@ function _drupal_bootstrap_code() {
  * Temporary BC function for scripts not using DrupalKernel.
  *
  * DrupalKernel skips this and replicates it via event listeners.
+ *
+ * @see Drupal\Core\EventSubscriber\PathSubscriber;
+ * @see Drupal\Core\EventSubscriber\LegacyRequestSubscriber;
  */
 function _drupal_bootstrap_full($skip = FALSE) {
   static $called = FALSE;
diff --git a/core/lib/Drupal/Core/EventSubscriber/LegacyRequestSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/LegacyRequestSubscriber.php
index 2620c0753a7c..84365a577ad2 100644
--- a/core/lib/Drupal/Core/EventSubscriber/LegacyRequestSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/LegacyRequestSubscriber.php
@@ -28,6 +28,13 @@ class LegacyRequestSubscriber implements EventSubscriberInterface {
    */
   public function onKernelRequestLegacy(GetResponseEvent $event) {
     if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
+      // Prior to invoking hook_init(), initialize the theme (potentially a
+      // custom one for this page), so that:
+      // - Modules with hook_init() implementations that call theme() or
+      //   theme_get_registry() don't initialize the incorrect theme.
+      // - The theme can have hook_*_alter() implementations affect page
+      //   building (e.g., hook_form_alter(), hook_node_view_alter(),
+      //   hook_page_alter()), ahead of when rendering starts.
       menu_set_custom_theme();
       drupal_theme_initialize();
       module_invoke_all('init');
diff --git a/core/update.php b/core/update.php
index ab050a8346ed..b47702ec5811 100644
--- a/core/update.php
+++ b/core/update.php
@@ -439,9 +439,11 @@ function update_check_requirements($skip_warnings = FALSE) {
   install_goto('core/update.php?op=info');
 }
 
-// Bootstrap, fix requirements, and set the maintenance theme.
-drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
+// Allow update_fix_d8_requirements() to run before code that can break on a
+// Drupal 7 database.
+drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
 update_fix_d8_requirements();
+drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 drupal_maintenance_theme();
 
 // Turn error reporting back on. From now on, only fatal errors (which are
diff --git a/index.php b/index.php
index b63ba1f2ffc7..2f05bb394f44 100644
--- a/index.php
+++ b/index.php
@@ -32,6 +32,11 @@
 // container at some point.
 request($request);
 
+// Bootstrap all of Drupal's subsystems, but do not initialize anything that
+// depends on the fully resolved Drupal path, because path resolution happens
+// during the REQUEST event of the kernel.
+// @see Drupal\Core\EventSubscriber\PathSubscriber;
+// @see Drupal\Core\EventSubscriber\LegacyRequestSubscriber;
 drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
 
 $dispatcher = new EventDispatcher();
-- 
GitLab