|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace A2Workspace\DatabasePatcher\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Illuminate\Filesystem\Filesystem; |
| 9 | +use Symfony\Component\Finder\Finder; |
| 10 | +use Symfony\Component\Finder\SplFileInfo; |
| 11 | + |
| 12 | +class DbPatchCommand extends Command |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The name and signature of the console command. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'db:patch {filter? : 指定或搜尋檔案} |
| 20 | + {--r|revert : Revert the patch file}'; |
| 21 | + |
| 22 | + /** |
| 23 | + * The console command description. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $description = '執行資料庫補丁檔'; |
| 28 | + |
| 29 | + /** |
| 30 | + * The filesystem instance. |
| 31 | + * |
| 32 | + * @var \Illuminate\Filesystem\Filesystem |
| 33 | + */ |
| 34 | + protected $files; |
| 35 | + |
| 36 | + /** |
| 37 | + * 要排除的檔案 |
| 38 | + * |
| 39 | + * @var string[] |
| 40 | + */ |
| 41 | + protected array $excludedNames = [ |
| 42 | + 'README.md', |
| 43 | + ]; |
| 44 | + |
| 45 | + /** |
| 46 | + * Execute the console command. |
| 47 | + * |
| 48 | + * @return int |
| 49 | + */ |
| 50 | + public function handle() |
| 51 | + { |
| 52 | + $file = $this->determinePatchFile(); |
| 53 | + if (!$file) { |
| 54 | + return 1; |
| 55 | + } |
| 56 | + |
| 57 | + $path = $file->getRealPath(); |
| 58 | + $path = Str::after($path, base_path()); |
| 59 | + |
| 60 | + // 這邊我們判斷 |
| 61 | + // 若為回復模式則呼叫滾回命令並傳入補丁檔案路徑 |
| 62 | + if ($this->option('revert')) { |
| 63 | + $this->info("Running: php artisan migrate:rollback --path={$path}"); |
| 64 | + |
| 65 | + $this->call('migrate:rollback', [ |
| 66 | + '--path' => $path, |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + // 呼叫遷移命令並傳入補丁檔案路徑 |
| 71 | + else { |
| 72 | + $this->info("Running: php artisan migrate --path={$path}"); |
| 73 | + |
| 74 | + $this->call('migrate', [ |
| 75 | + '--path' => $path, |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 決定要被使用的檔案 |
| 84 | + * |
| 85 | + * @return \Symfony\Component\Finder\SplFileInfo|null |
| 86 | + */ |
| 87 | + protected function determinePatchFile() |
| 88 | + { |
| 89 | + // 取得 patches 目錄的檔案列表,若結果為空則提前終止 |
| 90 | + $files = $this->getFileList(); |
| 91 | + if ($files->isEmpty()) { |
| 92 | + $this->warn('找不到任何補丁檔案'); |
| 93 | + |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + // 這邊處理有輸入 filter 參數的場合。 |
| 98 | + if ($inputFilter = $this->getFilterInput()) { |
| 99 | + $filtered = $files->filter(function (SplFileInfo $file) use ($inputFilter) { |
| 100 | + return Str::contains($file->getRelativePathname(), $inputFilter); |
| 101 | + }); |
| 102 | + |
| 103 | + if ($filtered->isEmpty()) { |
| 104 | + $this->error('找不到符合的補丁檔案'); |
| 105 | + |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + $files = $filtered; |
| 110 | + } |
| 111 | + |
| 112 | + return $this->choiceFromFileList('選擇補丁檔案', $files); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return string |
| 117 | + */ |
| 118 | + protected function getFilterInput() |
| 119 | + { |
| 120 | + return trim($this->argument('filter')); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @return \Illuminate\Support\Collection<int, \Symfony\Component\Finder\SplFileInfo> |
| 125 | + */ |
| 126 | + protected function getFileList(): Collection |
| 127 | + { |
| 128 | + $paths = [database_path('patches')]; |
| 129 | + |
| 130 | + return collect($paths) |
| 131 | + ->map(fn ($path) => $this->getFileListInDirectory($path)) |
| 132 | + ->collapse(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * 回傳指令目錄下的檔案。排除目錄的與 $excludedNames 指定的檔案。 |
| 137 | + * |
| 138 | + * @param string $path |
| 139 | + * @return \Symfony\Component\Finder\SplFileInfo[] |
| 140 | + */ |
| 141 | + protected function getFileListInDirectory(string $path): array |
| 142 | + { |
| 143 | + $finder = Finder::create() |
| 144 | + ->filter(function (SplFileInfo $file) { |
| 145 | + return !in_array($file->getRelativePathname(), $this->excludedNames); |
| 146 | + }) |
| 147 | + ->files() |
| 148 | + ->in($path) |
| 149 | + ->depth(0) |
| 150 | + ->sortByName(); |
| 151 | + |
| 152 | + return iterator_to_array($finder, false); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * 讓使用者自檔案列表中選取一個。 |
| 157 | + * |
| 158 | + * @param string $question |
| 159 | + * @param \Illuminate\Support\Collection $files |
| 160 | + * @return \Symfony\Component\Finder\SplFileInfo |
| 161 | + */ |
| 162 | + protected function choiceFromFileList($question, Collection $files): SplFileInfo |
| 163 | + { |
| 164 | + $formattedFiles = $files->map(function (SplFileInfo $file) { |
| 165 | + $label = $file->getRelativePathname(); |
| 166 | + |
| 167 | + // 加上符號隔開避免 choice 時索引與檔案名稱混淆 (這應該是 Symfony 的 bug 待查證) |
| 168 | + $label = "-> {$label}"; |
| 169 | + |
| 170 | + return [$label, $file]; |
| 171 | + }); |
| 172 | + |
| 173 | + $options = $formattedFiles->pluck(0)->toArray(); |
| 174 | + |
| 175 | + $input = $this->choice($question, $options); |
| 176 | + |
| 177 | + return $formattedFiles->first(function ($value) use ($input) { |
| 178 | + return $value[0] === $input; |
| 179 | + })[1]; |
| 180 | + } |
| 181 | +} |
0 commit comments