1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Export\Console; |
4
|
|
|
|
5
|
|
|
use Spatie\Export\Exporter; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class ExportCommand extends Command |
11
|
|
|
{ |
12
|
|
|
protected $name = 'export'; |
13
|
|
|
|
14
|
|
|
protected $description = 'Export the entire app to a static site'; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
parent::__construct(); |
19
|
|
|
|
20
|
|
|
collect() |
21
|
|
|
->merge(config('export.before', [])) |
22
|
|
|
->merge(config('export.after', [])) |
23
|
|
|
->keys() |
24
|
|
|
->unique() |
25
|
|
|
->sort() |
26
|
|
|
->each(function (string $name) { |
27
|
|
|
$this->addOption( |
28
|
|
|
"skip-{$name}", |
29
|
|
|
null, |
30
|
|
|
InputOption::VALUE_NONE, |
31
|
|
|
"Skip the {$name} hook" |
32
|
|
|
); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function handle(Exporter $exporter) |
37
|
|
|
{ |
38
|
|
|
$exporter->onMessage(function (string $message) { |
39
|
|
|
$this->comment($message, 'v'); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$this->runBeforeHooks(); |
43
|
|
|
|
44
|
|
|
$this->info('Starting export...'); |
45
|
|
|
|
46
|
|
|
$exporter->export(); |
47
|
|
|
|
48
|
|
|
if (config('export.disk')) { |
49
|
|
|
$this->info('Files were saved to disk `'.config('export.disk').'`'); |
50
|
|
|
} else { |
51
|
|
|
$this->info('Files were saved to `dist`'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->runAfterHooks(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
protected function runBeforeHooks() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$beforeHooks = collect(config('export.before', [])) |
60
|
|
|
->reject(function (string $hook, string $name) { |
61
|
|
|
return $this->input->getOption("skip-{$name}"); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
if (! count($beforeHooks)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->info('Running before hooks...'); |
69
|
|
|
|
70
|
|
|
$this->runHooks($beforeHooks); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
protected function runAfterHooks() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$afterHooks = collect(config('export.after', [])) |
76
|
|
|
->reject(function (string $hook, string $name) { |
77
|
|
|
return $this->input->getOption("skip-{$name}"); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
if (! count($afterHooks)) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->info('Running after hooks...'); |
85
|
|
|
|
86
|
|
|
$this->runHooks($afterHooks); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function runHooks(array $hooks) |
90
|
|
|
{ |
91
|
|
|
foreach ($hooks as $name => $command) { |
92
|
|
|
$this->comment("[{$name}]", 'v'); |
93
|
|
|
|
94
|
|
|
$process = new Process($command); |
95
|
|
|
|
96
|
|
|
$process->mustRun(); |
97
|
|
|
|
98
|
|
|
foreach ($process as $data) { |
99
|
|
|
$this->output->write($data); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.