annotate core/lib/Drupal/Core/EventSubscriber/ExceptionDetectNeedsInstallSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\EventSubscriber;
Chris@0 4
Chris@0 5 use Drupal\Core\Database\Connection;
Chris@0 6 use Drupal\Core\Installer\InstallerRedirectTrait;
Chris@0 7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 8 use Symfony\Component\HttpFoundation\RedirectResponse;
Chris@0 9 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Chris@0 10 use Symfony\Component\HttpKernel\KernelEvents;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Exception handler to determine if an exception indicates an uninstalled site.
Chris@0 14 */
Chris@0 15 class ExceptionDetectNeedsInstallSubscriber implements EventSubscriberInterface {
Chris@0 16 use InstallerRedirectTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The default database connection.
Chris@0 20 *
Chris@0 21 * @var \Drupal\Core\Database\Connection
Chris@0 22 */
Chris@0 23 protected $connection;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Constructs a new ExceptionDetectNeedsInstallSubscriber.
Chris@0 27 *
Chris@0 28 * @param \Drupal\Core\Database\Connection $connection
Chris@0 29 * The default database connection.
Chris@0 30 */
Chris@0 31 public function __construct(Connection $connection) {
Chris@0 32 $this->connection = $connection;
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Handles errors for this subscriber.
Chris@0 37 *
Chris@0 38 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 39 * The event to process.
Chris@0 40 */
Chris@0 41 public function onException(GetResponseForExceptionEvent $event) {
Chris@0 42 $exception = $event->getException();
Chris@0 43 if ($this->shouldRedirectToInstaller($exception, $this->connection)) {
Chris@0 44 // Only redirect if this is an HTML response (i.e., a user trying to view
Chris@0 45 // the site in a web browser before installing it).
Chris@0 46 $request = $event->getRequest();
Chris@0 47 $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat());
Chris@0 48 if ($format == 'html') {
Chris@0 49 $event->setResponse(new RedirectResponse($request->getBasePath() . '/core/install.php', 302, ['Cache-Control' => 'no-cache']));
Chris@0 50 }
Chris@0 51 }
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Registers the methods in this class that should be listeners.
Chris@0 56 *
Chris@0 57 * @return array
Chris@0 58 * An array of event listener definitions.
Chris@0 59 */
Chris@0 60 public static function getSubscribedEvents() {
Chris@0 61 $events[KernelEvents::EXCEPTION][] = ['onException', 100];
Chris@0 62 return $events;
Chris@0 63 }
Chris@0 64
Chris@0 65 }