From d2527ed4a77900bc4829cc5ff45614c41fc4fdd5 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Wed, 2 Jan 2013 12:14:56 +0000
Subject: [PATCH] Issue #1831998 by c4rl, effulgentsia: Reduce index.php for
 easier upgrading.

---
 core/includes/bootstrap.inc         | 47 +++++++++++++++++++++++++++++
 core/modules/system/tests/http.php  | 21 ++-----------
 core/modules/system/tests/https.php | 21 ++-----------
 index.php                           | 27 +----------------
 4 files changed, 52 insertions(+), 64 deletions(-)

diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index 107ca9af1333..4f836982a8fe 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -2140,6 +2140,53 @@ function drupal_bootstrap($phase = NULL, $new_phase = TRUE) {
   return $stored_phase;
 }
 
+/**
+ * Handles an entire PHP request.
+ *
+ * This function may be called by PHP scripts (e.g., Drupal's index.php) that
+ * want Drupal to take over the entire PHP processing of the request. The only
+ * expectation is that PHP's superglobals are initialized as desired (PHP does
+ * this automatically, but some scripts might want to alter them) and that the
+ * DRUPAL_ROOT constant is defined and set to the absolute server directory of
+ * Drupal's codebase.
+ *
+ * Scripts and applications that want to invoke multiple Drupal requests within
+ * a single PHP request, or Drupal request handling within some larger workflow,
+ * should not call this function, but instead instantiate and use
+ * \Drupal\Core\DrupalKernel as needed.
+ *
+ * @param boolean $test_only
+ *   Whether to restrict handling to only requests invoked by SimpleTest.
+ *
+ * @see index.php
+ */
+function drupal_handle_request($test_only = FALSE) {
+  // Initialize the environment, load settings.php, and activate a PSR-0 class
+  // autoloader with required namespaces registered.
+  drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
+
+  // Exit if we should be in a test environment but aren't.
+  if ($test_only && !drupal_valid_test_ua()) {
+    header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
+    exit;
+  }
+
+  // @todo Figure out how best to handle the Kernel constructor parameters.
+  $kernel = new DrupalKernel('prod', FALSE, drupal_classloader(), !$test_only);
+
+  // @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();
+  $response = $kernel->handle($request)->prepare($request)->send();
+
+  $kernel->terminate($request, $response);
+  exit;
+}
+
 /**
  * Returns the time zone of the current user.
  */
diff --git a/core/modules/system/tests/http.php b/core/modules/system/tests/http.php
index f53bea6401ce..84476e3a3ee1 100644
--- a/core/modules/system/tests/http.php
+++ b/core/modules/system/tests/http.php
@@ -5,9 +5,6 @@
  * Fake an HTTP request, for use during testing.
  */
 
-use Drupal\Core\DrupalKernel;
-use Symfony\Component\HttpFoundation\Request;
-
 // Set a global variable to indicate a mock HTTP request.
 $is_http_mock = !empty($_SERVER['HTTPS']);
 
@@ -21,21 +18,7 @@
 
 // Change current directory to the Drupal root.
 chdir('../../../..');
+
 define('DRUPAL_ROOT', getcwd());
 require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
-
-// Make sure this file can only be used by simpletest.
-drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
-if (!drupal_valid_test_ua()) {
-  header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
-  exit;
-}
-
-// Continue with normal request handling.
-$request = Request::createFromGlobals();
-
-drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
-
-$kernel = new DrupalKernel('prod', FALSE, drupal_classloader(), FALSE);
-$response = $kernel->handle($request)->prepare($request)->send();
-$kernel->terminate($request, $response);
+drupal_handle_request(TRUE);
diff --git a/core/modules/system/tests/https.php b/core/modules/system/tests/https.php
index e186f0c5704a..0d96896389f6 100644
--- a/core/modules/system/tests/https.php
+++ b/core/modules/system/tests/https.php
@@ -5,9 +5,6 @@
  * Fake an HTTPS request, for use during testing.
  */
 
-use Drupal\Core\DrupalKernel;
-use Symfony\Component\HttpFoundation\Request;
-
 // Set a global variable to indicate a mock HTTPS request.
 $is_https_mock = empty($_SERVER['HTTPS']);
 
@@ -20,21 +17,7 @@
 
 // Change current directory to the Drupal root.
 chdir('../../../..');
+
 define('DRUPAL_ROOT', getcwd());
 require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
-
-// Make sure this file can only be used by simpletest.
-drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
-if (!drupal_valid_test_ua()) {
-  header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
-  exit;
-}
-
-// Continue with normal request handling.
-$request = Request::createFromGlobals();
-
-drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
-
-$kernel = new DrupalKernel('prod', FALSE, drupal_classloader(), FALSE);
-$response = $kernel->handle($request)->prepare($request)->send();
-$kernel->terminate($request, $response);
+drupal_handle_request(TRUE);
diff --git a/index.php b/index.php
index 98bcddb9d57d..df30e3f9f7ed 100644
--- a/index.php
+++ b/index.php
@@ -4,39 +4,14 @@
  * @file
  * The PHP page that serves all page requests on a Drupal installation.
  *
- * The routines here dispatch control to the appropriate handler, which then
- * prints the appropriate page.
- *
  * All Drupal code is released under the GNU General Public License.
  * See COPYRIGHT.txt and LICENSE.txt files in the "core" directory.
  */
 
-use Drupal\Core\DrupalKernel;
-use Symfony\Component\HttpFoundation\Request;
-
 /**
  * Root directory of Drupal installation.
  */
 define('DRUPAL_ROOT', getcwd());
 
-// 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;
 require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
-drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
-
-// @todo Figure out how best to handle the Kernel constructor parameters.
-$kernel = new DrupalKernel('prod', FALSE, drupal_classloader());
-
-// @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();
-$response = $kernel->handle($request)->prepare($request)->send();
-
-$kernel->terminate($request, $response);
+drupal_handle_request();
-- 
GitLab