diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index a3ae42d..5961c27 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -34,6 +34,7 @@ use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\HttpKernel\TerminableInterface; use Composer\Autoload\ClassLoader; use Symfony\Component\Routing\Route; @@ -213,37 +214,9 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { * In case the host name in the request is not trusted. */ public static function createFromRequest(Request $request, $class_loader, $environment, $allow_dumping = TRUE) { - // Include our bootstrap file. - $core_root = dirname(dirname(dirname(__DIR__))); - require_once $core_root . '/includes/bootstrap.inc'; - $kernel = new static($environment, $class_loader, $allow_dumping); - - // Ensure sane php environment variables.. static::bootEnvironment(); - - // Get our most basic settings setup. - $site_path = static::findSitePath($request); - $kernel->setSitePath($site_path); - Settings::initialize(dirname($core_root), $site_path, $class_loader); - - // Initialize our list of trusted HTTP Host headers to protect against - // header attacks. - $hostPatterns = Settings::get('trusted_host_patterns', array()); - if (PHP_SAPI !== 'cli' && !empty($hostPatterns)) { - if (static::setupTrustedHosts($request, $hostPatterns) === FALSE) { - throw new BadRequestHttpException('The provided host name is not valid for this server.'); - } - } - - // Redirect the user to the installation script if Drupal has not been - // installed yet (i.e., if no $databases array has been defined in the - // settings.php file) and we are not already installing. - if (!Database::getConnectionInfo() && !drupal_installation_attempted() && PHP_SAPI !== 'cli') { - $response = new RedirectResponse($request->getBasePath() . '/core/install.php'); - $response->prepare($request)->send(); - } - + $kernel->initializeSettings($request); return $kernel; } @@ -363,6 +336,9 @@ public static function findSitePath(Request $request, $require_settings = TRUE) * {@inheritdoc} */ public function setSitePath($path) { + if ($this->booted) { + throw new \Exception('Site path cannot be changed after calling boot()'); + } $this->sitePath = $path; } @@ -561,8 +537,46 @@ public function terminate(Request $request, Response $response) { * {@inheritdoc} */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) { - $this->boot(); - return $this->getHttpKernel()->handle($request, $type, $catch); + // Ensure sane PHP environment variables. + static::bootEnvironment(); + + try { + $this->initializeSettings($request); + + // Redirect the user to the installation script if Drupal has not been + // installed yet (i.e., if no $databases array has been defined in the + // settings.php file) and we are not already installing. + if (!Database::getConnectionInfo() && !drupal_installation_attempted() && PHP_SAPI !== 'cli') { + $response = new RedirectResponse($request->getBasePath() . '/core/install.php'); + } + else { + $this->boot(); + $response = $this->getHttpKernel()->handle($request, $type, $catch); + } + } + catch (HttpExceptionInterface $e) { + if ($catch === FALSE) { + throw $e; + } + $response = new Response($e->getMessage(), $e->getStatusCode()); + } + catch (Exception $e) { + if ($catch === FALSE) { + throw $e; + } + $message = 'If you have just changed code (for example deployed a new module or moved an existing one) read http://drupal.org/documentation/rebuild'; + if (Settings::get('rebuild_access', FALSE)) { + $rebuild_path = $GLOBALS['base_url'] . '/rebuild.php'; + $message .= " or run the rebuild script"; + } + + $response = new Response($message, 500); + } + + // Adapt response headers to the current request. + $response->prepare($request); + + return $response; } /** @@ -739,6 +753,10 @@ public static function bootEnvironment() { return; } + // Include our bootstrap file. + $core_root = dirname(dirname(dirname(__DIR__))); + require_once $core_root . '/includes/bootstrap.inc'; + // Enforce E_STRICT, but allow users to set levels not part of E_STRICT. error_reporting(E_STRICT | E_ALL); @@ -791,6 +809,27 @@ public static function bootEnvironment() { } /** + * Locate site path and initialize settings singleton. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The current request. + */ + protected function initializeSettings(Request $request) { + $site_path = static::findSitePath($request); + $this->setSitePath($site_path); + Settings::initialize($this->root, $site_path, $class_loader); + + // Initialize our list of trusted HTTP Host headers to protect against + // header attacks. + $hostPatterns = Settings::get('trusted_host_patterns', array()); + if (PHP_SAPI !== 'cli' && !empty($hostPatterns)) { + if (static::setupTrustedHosts($request, $hostPatterns) === FALSE) { + throw new BadRequestHttpException('The provided host name is not valid for this server.'); + } + } + } + + /** * Bootstraps the legacy global request variables. * * @param \Symfony\Component\HttpFoundation\Request $request diff --git a/core/lib/Drupal/Core/Test/TestKernel.php b/core/lib/Drupal/Core/Test/TestKernel.php index bff37e6..55294ca 100644 --- a/core/lib/Drupal/Core/Test/TestKernel.php +++ b/core/lib/Drupal/Core/Test/TestKernel.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Test; use Drupal\Core\DrupalKernel; -use Symfony\Component\HttpFoundation\Request; /** * Kernel to mock requests to test simpletest. @@ -18,17 +17,17 @@ class TestKernel extends DrupalKernel { /** * {@inheritdoc} */ - public static function createFromRequest(Request $request, $class_loader, $environment, $allow_dumping = TRUE) { + public function __construct($environment, $class_loader, $allow_dumping = TRUE) { // Include our bootstrap file. require_once __DIR__ . '/../../../../includes/bootstrap.inc'; // Exit if we should be in a test environment but aren't. if (!drupal_valid_test_ua()) { - header($request->server->get('SERVER_PROTOCOL') . ' 403 Forbidden'); + header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); exit; } - return parent::createFromRequest($request, $class_loader, $environment, $allow_dumping); + parent::__construct($environment, $class_loader, $allow_dumping); } } diff --git a/core/lib/Drupal/Core/Test/TestRunnerKernel.php b/core/lib/Drupal/Core/Test/TestRunnerKernel.php index fa9fd51..f19afb3 100644 --- a/core/lib/Drupal/Core/Test/TestRunnerKernel.php +++ b/core/lib/Drupal/Core/Test/TestRunnerKernel.php @@ -42,8 +42,8 @@ public function __construct($environment, $class_loader) { 'simpletest' => 0, ); $this->moduleData = array( - 'system' => new Extension(DRUPAL_ROOT, 'module', 'core/modules/system/system.info.yml', 'system.module'), - 'simpletest' => new Extension(DRUPAL_ROOT, 'module', 'core/modules/simpletest/simpletest.info.yml', 'simpletest.module'), + 'system' => new Extension($this->root, 'module', 'core/modules/system/system.info.yml', 'system.module'), + 'simpletest' => new Extension($this->root, 'module', 'core/modules/simpletest/simpletest.info.yml', 'simpletest.module'), ); } diff --git a/core/modules/system/tests/http.php b/core/modules/system/tests/http.php index 8c7d556..5cdf74a 100644 --- a/core/modules/system/tests/http.php +++ b/core/modules/system/tests/http.php @@ -23,10 +23,10 @@ $value = str_replace('https://', 'http://', $value); } +$kernel = new TestKernel('testing', $autoloader, TRUE); + $request = Request::createFromGlobals(); -$kernel = TestKernel::createFromRequest($request, $autoloader, 'testing', TRUE); -$response = $kernel - ->handle($request) - // Handle the response object. - ->prepare($request)->send(); +$response = $kernel->handle($request); +$response->send(); + $kernel->terminate($request, $response); diff --git a/core/modules/system/tests/https.php b/core/modules/system/tests/https.php index 702184d..fb5cda8 100644 --- a/core/modules/system/tests/https.php +++ b/core/modules/system/tests/https.php @@ -25,10 +25,10 @@ $value = str_replace('http://', 'https://', $value); } +$kernel = new TestKernel('testing', $autoloader, TRUE); + $request = Request::createFromGlobals(); -$kernel = TestKernel::createFromRequest($request, $autoloader, 'testing', TRUE); -$response = $kernel - ->handle($request) - // Handle the response object. - ->prepare($request)->send(); +$response = $kernel->handle($request); +$response->send(); + $kernel->terminate($request, $response); diff --git a/index.php b/index.php index 867f0e0..8d88b1c 100644 --- a/index.php +++ b/index.php @@ -9,37 +9,14 @@ */ use Drupal\Core\DrupalKernel; -use Drupal\Core\Site\Settings; -use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; $autoloader = require_once __DIR__ . '/core/vendor/autoload.php'; -try { +$kernel = new DrupalKernel('prod', $autoloader); - $request = Request::createFromGlobals(); - $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); - $response = $kernel - ->handle($request) - // Handle the response object. - ->prepare($request)->send(); - $kernel->terminate($request, $response); -} -catch (HttpExceptionInterface $e) { - $response = new Response($e->getMessage(), $e->getStatusCode()); - $response->prepare($request)->send(); -} -catch (Exception $e) { - $message = 'If you have just changed code (for example deployed a new module or moved an existing one) read http://drupal.org/documentation/rebuild'; - if (Settings::get('rebuild_access', FALSE)) { - $rebuild_path = $GLOBALS['base_url'] . '/rebuild.php'; - $message .= " or run the rebuild script"; - } +$request = Request::createFromGlobals(); +$response = $kernel->handle($request); +$response->send(); - // Set the response code manually. Otherwise, this response will default to a - // 200. - http_response_code(500); - print $message; - throw $e; -} +$kernel->terminate($request, $response);