|
1 | 1 | <?php
|
2 | 2 | namespace Drush\Commands\sql;
|
3 | 3 |
|
| 4 | +use Consolidation\AnnotatedCommand\CommandData; |
4 | 5 | use Drupal\Core\Database\Database;
|
5 | 6 | use Drush\Commands\DrushCommands;
|
6 | 7 | use Drush\Drush;
|
7 | 8 | use Drush\Exceptions\UserAbortException;
|
| 9 | +use Drush\Exec\ExecTrait; |
8 | 10 | use Drush\Sql\SqlBase;
|
9 | 11 | use Consolidation\OutputFormatters\StructuredData\PropertyList;
|
10 | 12 |
|
11 | 13 | class SqlCommands extends DrushCommands
|
12 | 14 | {
|
| 15 | + use ExecTrait; |
13 | 16 |
|
14 | 17 | /**
|
15 |
| - * Print database connection details using print_r(). |
| 18 | + * Print database connection details. |
16 | 19 | *
|
17 | 20 | * @command sql:conf
|
18 | 21 | * @aliases sql-conf
|
@@ -229,4 +232,32 @@ public function dump($options = ['result-file' => self::REQ, 'create-db' => fals
|
229 | 232 | }
|
230 | 233 | return new PropertyList(['path' => $return]);
|
231 | 234 | }
|
| 235 | + |
| 236 | + /** |
| 237 | + * Assert that `mysql` or similar are on the user's PATH. |
| 238 | + * |
| 239 | + * @hook validate |
| 240 | + * @param CommandData $commandData |
| 241 | + * @return bool |
| 242 | + * @throws \Exception |
| 243 | + */ |
| 244 | + public function validate(CommandData $commandData) |
| 245 | + { |
| 246 | + if (in_array($commandData->annotationData()->get('command'), ['sql:connect', 'sql:conf'])) { |
| 247 | + // These commands don't require a program. |
| 248 | + return; |
| 249 | + } |
| 250 | + |
| 251 | + $sql = SqlBase::create($commandData->options()); |
| 252 | + $program = $sql->command(); |
| 253 | + |
| 254 | + // Remove environment variables (eg. PGPASSFILE=) before testing program. |
| 255 | + // @todo Remove once postgres is passing env variables via Process. |
| 256 | + $program = preg_replace('#^([A-Z0-9]+=.+? )+#', '', $program); |
| 257 | + |
| 258 | + if (!$this->programExists($program)) { |
| 259 | + $this->logger->warning(dt('The shell command \'!command\' is required but cannot be found. Please install it and retry.', ['!command' => $program])); |
| 260 | + return false; |
| 261 | + } |
| 262 | + } |
232 | 263 | }
|
0 commit comments