ExceptionDetectNeedsInstallSubscriber.php 2.0 KB

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