|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Defines class that powers `drush blt-doctor` command. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Acquia\Blt\Drush\Command; |
| 9 | + |
| 10 | +class BltDoctor { |
| 11 | + |
| 12 | + protected $localSettingsPath; |
| 13 | + protected $statusTable; |
| 14 | + |
| 15 | + /** |
| 16 | + * BoltDoctor constructor. |
| 17 | + */ |
| 18 | + public function __construct() { |
| 19 | + $this->statusTable = drush_core_status(); |
| 20 | + $this->localSettingsPath = $this->statusTable['root'] . '/' . $this->statusTable['site'] . '/settings/' . 'local.settings.php'; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Performs all checks. |
| 25 | + */ |
| 26 | + public function checkAll() { |
| 27 | + $this->checkLocalSettingsFile(); |
| 28 | + $this->checkUriResponse(); |
| 29 | + $this->checkHttps(); |
| 30 | + $this->checkDbConnection(); |
| 31 | + $this->checkCachingConfig(); |
| 32 | + $this->checkNvmExists(); |
| 33 | + //$this->checkDatabaseUpdates(); |
| 34 | + |
| 35 | + // @todo Check if Drupal is installed. |
| 36 | + // @todo Check if files directory exists. |
| 37 | + // @todo Check error_level. |
| 38 | + // @todo Check if theme dependencies have been built. |
| 39 | + // @todo Check if composer dependencies have been built. |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Checks that local settings file exists. |
| 44 | + */ |
| 45 | + protected function checkLocalSettingsFile() { |
| 46 | + if (!file_exists($this->localSettingsPath)) { |
| 47 | + drush_log("Could not find local settings file!", 'error'); |
| 48 | + drush_print("Your local settings file should exist at $this->localSettingsPath", 2); |
| 49 | + } |
| 50 | + else { |
| 51 | + drush_log("Found your local settings file at:", 'success'); |
| 52 | + drush_print($this->localSettingsPath, 2); |
| 53 | + } |
| 54 | + drush_print(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Checks that configured $base_url responds to requests. |
| 59 | + */ |
| 60 | + protected function checkUriResponse() { |
| 61 | + $site_available = drush_shell_exec("curl -I --insecure %s", $this->statusTable['uri']); |
| 62 | + if (!$site_available) { |
| 63 | + drush_log("Did not get response from $this->statusTable['uri']", 'error'); |
| 64 | + drush_print("Is your *AMP stack running?", 2); |
| 65 | + drush_print("Is your \$base_url set correctly in $this->localSettingsPath?", 2); |
| 66 | + } |
| 67 | + else { |
| 68 | + drush_log("Received response from site:", 'success'); |
| 69 | + drush_print($this->statusTable['uri'], 2); |
| 70 | + } |
| 71 | + drush_print(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Checks that SSL cert is valid for $base_url. |
| 76 | + */ |
| 77 | + protected function checkHttps() { |
| 78 | + if (strstr($this->statusTable['uri'], 'https')) { |
| 79 | + if (!drush_shell_exec('curl -cacert %s', $this->statusTable['uri'])) { |
| 80 | + drush_log('The SSL certificate for your local site appears to be invalid:', 'error'); |
| 81 | + drush_print($this->statusTable['uri'], 2); |
| 82 | + drush_print(); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Checks that drush is able to bootstrap and connect to database. |
| 89 | + */ |
| 90 | + protected function checkDbConnection() { |
| 91 | + if (empty($this->statusTable['bootstrap']) || $this->statusTable['bootstrap'] != 'Successful') { |
| 92 | + drush_log('Could not bootstrap Drupal!', 'error'); |
| 93 | + drush_print("Is your *AMP stack running?", 2); |
| 94 | + drush_print('Are your database credentials correct?', 2); |
| 95 | + drush_blt_print_status_rows($this->statusTable, array( |
| 96 | + 'db-driver', |
| 97 | + 'db-hostname', |
| 98 | + 'db-username', |
| 99 | + 'db-password', |
| 100 | + 'db-name', |
| 101 | + 'db-port', |
| 102 | + )); |
| 103 | + |
| 104 | + drush_print('Is the active PHP binary the same one that is associated with your database service?'); |
| 105 | + drush_blt_print_status_rows($this->statusTable, array( |
| 106 | + 'php-os', |
| 107 | + 'php-bin', |
| 108 | + 'php-conf', |
| 109 | + )); |
| 110 | + |
| 111 | + drush_print('Are you using the correct site and settings.php file?'); |
| 112 | + drush_blt_print_status_rows($this->statusTable, array( |
| 113 | + 'site', |
| 114 | + 'drupal-settings-file', |
| 115 | + )); |
| 116 | + } |
| 117 | + else { |
| 118 | + drush_log('Bootstrapped Drupal and connected to database.', 'success'); |
| 119 | + } |
| 120 | + drush_print(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Checks if database updates are pending. |
| 125 | + */ |
| 126 | + protected function checkDatabaseUpdates() { |
| 127 | + drush_include_engine('drupal', 'update'); |
| 128 | + $pending = update_main(); |
| 129 | + |
| 130 | + if ($pending) { |
| 131 | + drush_log("There are pending database updates", 'error'); |
| 132 | + drush_print("Run `drush updb` to execute the updates.", 2); |
| 133 | + } |
| 134 | + else { |
| 135 | + drush_log("There are no pending database updates.", 'success'); |
| 136 | + } |
| 137 | + drush_print(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Checks that nvm exists. |
| 142 | + * |
| 143 | + * Note that this does not check if `nvm use` has been invoked for the correct |
| 144 | + * node version. |
| 145 | + */ |
| 146 | + protected function checkNvmExists() { |
| 147 | + $home = getenv("HOME"); |
| 148 | + if (!file_exists("$home/.nvm")) { |
| 149 | + drush_log('NVM does not exist. Install using the following commands:', 'error'); |
| 150 | + drush_print('curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash', 2); |
| 151 | + drush_print('source ~/.bashrc', 2); |
| 152 | + drush_print('nvm install 0.12.7', 2); |
| 153 | + drush_print('nvm use 0.12.7', 2); |
| 154 | + drush_print(); |
| 155 | + } |
| 156 | + else { |
| 157 | + drush_log("NVM exists.", 'success'); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Checks that caching is configured for local development. |
| 163 | + */ |
| 164 | + protected function checkCachingConfig() { |
| 165 | + if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) { |
| 166 | + global $conf; |
| 167 | + |
| 168 | + if ($conf['cache']) { |
| 169 | + drush_log('Drupal cache is enabled. It is suggested that you disable this for local development.', 'warning'); |
| 170 | + } |
| 171 | + else { |
| 172 | + drush_log('Drupal cache is disabled.', 'success'); |
| 173 | + } |
| 174 | + if ($conf['preprocess_css']) { |
| 175 | + drush_log('CSS preprocessing enabled. It is suggested that you disable this for local development.', 'warning'); |
| 176 | + } |
| 177 | + else { |
| 178 | + drush_log('CSS preprocessing is disabled.', 'success'); |
| 179 | + } |
| 180 | + if ($conf['preprocess_js']) { |
| 181 | + drush_log('JS preprocessing is enabled. It is suggested that you disable this for local development.', 'warning'); |
| 182 | + } |
| 183 | + else { |
| 184 | + drush_log('JS preprocessing is disabled.', 'success'); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + protected function checkContribExists() { |
| 190 | + if (!file_exists($this->statusTable['root'] . '/sites/all/modules/contrib')) { |
| 191 | + drush_log("Contributed module dependencies are missing.", 'error'); |
| 192 | + drush_print("Run `./task.sh setup:build:all to build all contributed dependencies.", 2); |
| 193 | + drush_print(); |
| 194 | + } |
| 195 | + else { |
| 196 | + drush_log("Contributed module dependencies are present."); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments