diff --git a/core/core.services.yml b/core/core.services.yml index 5877a38..6f6046d 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1169,6 +1169,11 @@ services: tags: - { name: event_subscriber } arguments: ['@config.manager', '@config.storage', '@config.storage.snapshot'] + exception.neds_installer: + class: Drupal\Core\EventSubscriber\ExceptionDetectNeedsInstallSubscriber + arguments: ['@database'] + tags: + - { name: event_subscriber } exception.default_json: class: Drupal\Core\EventSubscriber\ExceptionJsonSubscriber tags: diff --git a/core/lib/Drupal/Core/EventSubscriber/ExceptionDetectNeedsInstallSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ExceptionDetectNeedsInstallSubscriber.php new file mode 100644 index 0000000..0a9f58d --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/ExceptionDetectNeedsInstallSubscriber.php @@ -0,0 +1,88 @@ +connection = $connection; + } + + /** + * {@inheritdoc} + */ + protected function getHandledFormats() { + return ['html']; + } + + /** + * {@inheritdoc} + */ + protected static function getPriority() { + return 100; + } + + /** + * Determines if Drupal still needs to be installed. + * + * @return bool + * TRUE if the system still needs to be installed, FALSE otherwise. + */ + protected function systemNeedsInstall() { + // If we're already in the installer, don't worry about Drupal not being + // installed. + if (PHP_SAPI === 'cli' || drupal_installation_attempted()) { + return FALSE; + } + + // If there's no sessions table, we assume it means Drupal was never + // installed. This may be an incorrect assumption on non-SQL-based + // installations. + return !$this->connection->schema()->tableExists('session'); + } + + /** + * Checks if a 404 error is really a sign of an uninstalled Drupal. + * + * @todo Remove this method in favor of on400() once + * https://www.drupal.org/node/2739617 is committed. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event + * The event to process. + */ + public function on404(GetResponseForExceptionEvent $event) { + return $this->on400($event); + } + + /** + * Checks if a 404 error is really a sign of an uninstalled Drupal. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event + * + */ + public function on400(GetResponseForExceptionEvent $event) { + if ($this->systemNeedsInstall()) { + $event->setResponse(new RedirectResponse($event->getRequest()->getBasePath() . '/core/install.php', 302, ['Cache-Control' => 'no-cache'])); + } + } +}