InstallerRedirectTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Drupal\Core\Installer;
  3. use Drupal\Core\Database\Connection;
  4. use Drupal\Core\Database\Database;
  5. use Drupal\Core\Database\DatabaseException;
  6. use Drupal\Core\Database\DatabaseNotFoundException;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. /**
  9. * Provides methods for checking if Drupal is already installed.
  10. */
  11. trait InstallerRedirectTrait {
  12. /**
  13. * Returns whether the current PHP process runs on CLI.
  14. *
  15. * @return bool
  16. */
  17. protected function isCli() {
  18. return PHP_SAPI === 'cli';
  19. }
  20. /**
  21. * Determines if an exception handler should redirect to the installer.
  22. *
  23. * @param \Exception $exception
  24. * The exception to check.
  25. * @param \Drupal\Core\Database\Connection|null $connection
  26. * (optional) The default database connection. If not provided, a less
  27. * comprehensive check will be performed. This can be the case if the
  28. * exception occurs early enough that a database connection object isn't
  29. * available from the container yet.
  30. *
  31. * @return bool
  32. * TRUE if the exception handler should redirect to the installer because
  33. * Drupal is not installed yet, or FALSE otherwise.
  34. */
  35. protected function shouldRedirectToInstaller(\Exception $exception, Connection $connection = NULL) {
  36. // Never redirect on the command line.
  37. if ($this->isCli()) {
  38. return FALSE;
  39. }
  40. // Never redirect if we're already in the installer.
  41. if (drupal_installation_attempted()) {
  42. return FALSE;
  43. }
  44. // If the database wasn't found, assume the user hasn't entered it properly
  45. // and redirect to the installer. This check needs to come first because a
  46. // DatabaseNotFoundException is also an instance of DatabaseException.
  47. if ($exception instanceof DatabaseNotFoundException) {
  48. return TRUE;
  49. }
  50. // To avoid unnecessary queries, only act if the exception is one that is
  51. // expected to occur when Drupal has not yet been installed. This includes
  52. // NotFoundHttpException because an uninstalled site won't have route
  53. // information available yet and therefore can return 404 errors.
  54. if (!($exception instanceof \PDOException || $exception instanceof DatabaseException || $exception instanceof NotFoundHttpException)) {
  55. return FALSE;
  56. }
  57. // Redirect if there isn't even any database connection information in
  58. // settings.php yet, since that means Drupal is not installed.
  59. if (!Database::getConnectionInfo()) {
  60. return TRUE;
  61. }
  62. // Redirect if the database is empty.
  63. if ($connection) {
  64. try {
  65. return !$connection->schema()->tableExists('sessions');
  66. }
  67. catch (\Exception $e) {
  68. // If we still have an exception at this point, we need to be careful
  69. // since we should not redirect if the exception represents an error on
  70. // an already-installed site (for example, if the database server went
  71. // down). Assume we shouldn't redirect, just in case.
  72. return FALSE;
  73. }
  74. }
  75. // When in doubt, don't redirect.
  76. return FALSE;
  77. }
  78. }