3 namespace Drupal\Core\EventSubscriber;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Installer\InstallerRedirectTrait;
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8 use Symfony\Component\HttpFoundation\RedirectResponse;
9 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10 use Symfony\Component\HttpKernel\KernelEvents;
13 * Exception handler to determine if an exception indicates an uninstalled site.
15 class ExceptionDetectNeedsInstallSubscriber implements EventSubscriberInterface {
16 use InstallerRedirectTrait;
19 * The default database connection.
21 * @var \Drupal\Core\Database\Connection
23 protected $connection;
26 * Constructs a new ExceptionDetectNeedsInstallSubscriber.
28 * @param \Drupal\Core\Database\Connection $connection
29 * The default database connection.
31 public function __construct(Connection $connection) {
32 $this->connection = $connection;
36 * Handles errors for this subscriber.
38 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
39 * The event to process.
41 public function onException(GetResponseForExceptionEvent $event) {
42 $exception = $event->getException();
43 if ($this->shouldRedirectToInstaller($exception, $this->connection)) {
44 // Only redirect if this is an HTML response (i.e., a user trying to view
45 // the site in a web browser before installing it).
46 $request = $event->getRequest();
47 $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat());
48 if ($format == 'html') {
49 $event->setResponse(new RedirectResponse($request->getBasePath() . '/core/install.php', 302, ['Cache-Control' => 'no-cache']));
55 * Registers the methods in this class that should be listeners.
58 * An array of event listener definitions.
60 public static function getSubscribedEvents() {
61 $events[KernelEvents::EXCEPTION][] = ['onException', 100];