|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Commands; |
| 4 | + |
| 5 | +use App\Traits\SharedTrait; |
| 6 | +use Artisan; |
| 7 | +use DB; |
| 8 | +use HnhDigital\CliHelper\CommandInternalsTrait; |
| 9 | +use HnhDigital\CliHelper\FileSystemTrait; |
| 10 | +use LaravelZero\Framework\Commands\Command; |
| 11 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 12 | + |
| 13 | +class DisplayCommand extends Command |
| 14 | +{ |
| 15 | + use CommandInternalsTrait, FileSystemTrait, SharedTrait; |
| 16 | + |
| 17 | + /** |
| 18 | + * The signature of the command. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $signature = 'display |
| 23 | + {source : profiles|connections|databases} |
| 24 | + {--profile=0 : Profile} |
| 25 | + {--connection=0 : Connection} |
| 26 | + {--json : Return as JSON encoded}'; |
| 27 | + |
| 28 | + /** |
| 29 | + * The description of the command. |
| 30 | + * |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $description = 'List databases available to this connection.'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Execute the console command. |
| 37 | + * |
| 38 | + * @return int |
| 39 | + * |
| 40 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 41 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
| 42 | + */ |
| 43 | + public function handle() |
| 44 | + { |
| 45 | + $this->loadExistingProfiles(); |
| 46 | + |
| 47 | + switch ($this->argument('source')) { |
| 48 | + case 'profiles': |
| 49 | + return $this->listProfiles(); |
| 50 | + case 'connections': |
| 51 | + return $this->listConnections(); |
| 52 | + case 'databases': |
| 53 | + return $this->listDatabases(); |
| 54 | + } |
| 55 | + |
| 56 | + return 1; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * List profiles. |
| 61 | + * |
| 62 | + * @return int |
| 63 | + */ |
| 64 | + private function listProfiles() |
| 65 | + { |
| 66 | + return $this->outputResult(array_keys($this->profiles)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * List profiles. |
| 71 | + * |
| 72 | + * @return int |
| 73 | + */ |
| 74 | + private function listConnections() |
| 75 | + { |
| 76 | + // Select profile. |
| 77 | + if (empty($profile = $this->option('profile'))) { |
| 78 | + $profile = $this->selectProfile(); |
| 79 | + } elseif (!array_has($this->profiles, $profile)) { |
| 80 | + $this->error('Invalid profile supplied.'); |
| 81 | + |
| 82 | + return 1; |
| 83 | + } |
| 84 | + |
| 85 | + $connections = []; |
| 86 | + foreach (array_keys(array_get($this->profiles, $profile.'.local', [])) as $name) { |
| 87 | + $connections[] = $name; |
| 88 | + } |
| 89 | + |
| 90 | + return $this->outputResult($connections); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Output JSON. |
| 95 | + * |
| 96 | + * @param array $output |
| 97 | + * |
| 98 | + * @return int |
| 99 | + */ |
| 100 | + private function outputResult($output) |
| 101 | + { |
| 102 | + if ($this->option('json')) { |
| 103 | + echo json_encode($output); |
| 104 | + |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + foreach ($output as $value) { |
| 109 | + $this->line($value); |
| 110 | + } |
| 111 | + |
| 112 | + return 0; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Output databases. |
| 117 | + * |
| 118 | + * @return int |
| 119 | + */ |
| 120 | + private function listDatabases() |
| 121 | + { |
| 122 | + // Select profile. |
| 123 | + if (empty($profile = $this->option('profile'))) { |
| 124 | + $profile = $this->selectProfile(); |
| 125 | + } elseif (!array_has($this->profiles, $profile)) { |
| 126 | + $this->error('Invalid profile supplied.'); |
| 127 | + |
| 128 | + return 1; |
| 129 | + } |
| 130 | + |
| 131 | + // Select connection profile. |
| 132 | + if (empty($connection = $this->option('connection'))) { |
| 133 | + $connection = $this->selectConnection($profile); |
| 134 | + } elseif (!array_has($this->profiles, $profile.'.'.$connection)) { |
| 135 | + $this->error('Invalid connection profile supplied.'); |
| 136 | + |
| 137 | + return 1; |
| 138 | + } elseif (!empty($connection)) { |
| 139 | + $this->loadDatabaseConnections($profile); |
| 140 | + } |
| 141 | + |
| 142 | + $available_databases = DB::connection($connection) |
| 143 | + ->select('SHOW DATABASES'); |
| 144 | + |
| 145 | + $databases = []; |
| 146 | + foreach ($available_databases as $database) { |
| 147 | + $databases[] = $database->Database; |
| 148 | + } |
| 149 | + |
| 150 | + return $this->outputResult($databases); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Select profile. |
| 155 | + * |
| 156 | + * @return string |
| 157 | + */ |
| 158 | + private function selectProfile() |
| 159 | + { |
| 160 | + $menu_options = []; |
| 161 | + |
| 162 | + foreach (array_keys($this->profiles) as $name) { |
| 163 | + $menu_options[$name] = strtoupper($name); |
| 164 | + } |
| 165 | + |
| 166 | + $option = $this->menu('Select profile', $menu_options)->open(); |
| 167 | + |
| 168 | + return $option; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Select local connection. |
| 173 | + * |
| 174 | + * @return string |
| 175 | + */ |
| 176 | + private function selectConnection($profile) |
| 177 | + { |
| 178 | + $menu_options = []; |
| 179 | + |
| 180 | + foreach (array_keys(array_get($this->profiles, $profile.'.local', [])) as $name) { |
| 181 | + $menu_options[$name] = strtoupper($name); |
| 182 | + } |
| 183 | + |
| 184 | + $option = $this->menu('Select local connection', $menu_options)->open(); |
| 185 | + |
| 186 | + $this->loadDatabaseConnections($profile, $option); |
| 187 | + |
| 188 | + return $option; |
| 189 | + } |
| 190 | +} |
0 commit comments