|
17 | 17 | use Spatie\DbDumper\Databases\PostgreSql; |
18 | 18 | use Spatie\DbDumper\Databases\Sqlite; |
19 | 19 | use Spatie\DbDumper\Exceptions\CannotSetParameter; |
| 20 | +use Symfony\Component\Console\Output\BufferedOutput; |
20 | 21 | use Symfony\Component\Process\Process; |
21 | 22 | use VanOns\LaravelEnvironmentImporter\Exceptions\ImportEnvironmentException; |
22 | 23 | use VanOns\LaravelEnvironmentImporter\Notifications\ImportFailed; |
@@ -81,6 +82,7 @@ public function handle(): int |
81 | 82 | $this->importDatabase(); |
82 | 83 | $this->importFiles(); |
83 | 84 | $this->flushCache(); |
| 85 | + $this->runPostImportCommands(); |
84 | 86 | $this->finish(); |
85 | 87 | } catch (Exception $e) { |
86 | 88 | $this->error($e->getMessage()); |
@@ -890,6 +892,57 @@ protected function flushCache(): void |
890 | 892 | $this->info('[Cache] Cache flushed.'); |
891 | 893 | } |
892 | 894 |
|
| 895 | + /** |
| 896 | + * Run post import commands. |
| 897 | + */ |
| 898 | + protected function runPostImportCommands(): void |
| 899 | + { |
| 900 | + $commands = $this->getConfigValue('post_import_commands', []); |
| 901 | + |
| 902 | + if (empty($commands)) { |
| 903 | + return; |
| 904 | + } |
| 905 | + |
| 906 | + $this->line('[CMD] Running post import commands...'); |
| 907 | + |
| 908 | + foreach ($commands as $key => $value) { |
| 909 | + if (is_int($key)) { |
| 910 | + $command = $value; |
| 911 | + $options = []; |
| 912 | + } else { |
| 913 | + $command = $key; |
| 914 | + $options = $value; |
| 915 | + } |
| 916 | + |
| 917 | + $label = is_a($command, Command::class, true) |
| 918 | + ? class_basename($command) |
| 919 | + : $command; |
| 920 | + |
| 921 | + $this->line("[CMD] Running post-import command: \"{$label}\""); |
| 922 | + |
| 923 | + if (is_a($command, Command::class, true)) { |
| 924 | + $buffer = new BufferedOutput(); |
| 925 | + |
| 926 | + Artisan::call($command, $options, $buffer); |
| 927 | + |
| 928 | + foreach (explode("\n", trim($buffer->fetch())) as $outputLine) { |
| 929 | + $this->line(" | {$outputLine}"); |
| 930 | + } |
| 931 | + } elseif (is_string($command)) { |
| 932 | + Process::fromShellCommandline($command) |
| 933 | + ->run(function ($type, $buffer) { |
| 934 | + foreach (explode("\n", trim($buffer)) as $outputLine) { |
| 935 | + $this->line(" | {$outputLine}"); |
| 936 | + } |
| 937 | + }); |
| 938 | + } else { |
| 939 | + $this->warn("[CMD] Invalid post-import command: \"{$label}\""); |
| 940 | + } |
| 941 | + } |
| 942 | + |
| 943 | + $this->info('[CMD] Ran post import commands.'); |
| 944 | + } |
| 945 | + |
893 | 946 | /** |
894 | 947 | * Clean up and finish the import. |
895 | 948 | */ |
|
0 commit comments