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