1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Export\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\Export\Exporter; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
class ExportCommand extends Command |
12
|
|
|
{ |
13
|
|
|
protected $name = 'export'; |
14
|
|
|
|
15
|
|
|
protected $description = 'Export the entire app to a static site'; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
parent::__construct(); |
20
|
|
|
|
21
|
|
|
collect() |
22
|
|
|
->merge(config('export.before', [])) |
23
|
|
|
->merge(config('export.after', [])) |
24
|
|
|
->keys() |
25
|
|
|
->unique() |
26
|
|
|
->sort() |
27
|
|
|
->each(function (string $name) { |
28
|
|
|
$this->addOption( |
29
|
|
|
"skip-{$name}", |
30
|
|
|
null, |
31
|
|
|
InputOption::VALUE_NONE, |
32
|
|
|
"Skip the {$name} hook" |
33
|
|
|
); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
$this->addOption("skip-all", null, InputOption::VALUE_NONE, 'Skip all hooks'); |
37
|
|
|
$this->addOption("skip-before", null, InputOption::VALUE_NONE, 'Skip all before hooks'); |
38
|
|
|
$this->addOption("skip-after", null, InputOption::VALUE_NONE, 'Skip all after hooks'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function handle(Exporter $exporter) |
42
|
|
|
{ |
43
|
|
|
$this->runBeforeHooks(); |
44
|
|
|
|
45
|
|
|
$this->info('Exporting site...'); |
46
|
|
|
|
47
|
|
|
$exporter->export(); |
48
|
|
|
|
49
|
|
|
if (config('export.disk')) { |
50
|
|
|
$this->info('Files were saved to disk `'.config('export.disk').'`'); |
51
|
|
|
} else { |
52
|
|
|
$this->info('Files were saved to `dist`'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->runAfterHooks(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
protected function runBeforeHooks() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if ($this->input->getOption('skip-all') || $this->input->getOption('skip-before')) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$beforeHooks = collect(config('export.before', [])) |
65
|
|
|
->reject(function (string $hook, string $name) { |
66
|
|
|
return $this->input->getOption("skip-{$name}"); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
if (! count($beforeHooks)) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->info('Running before hooks...'); |
74
|
|
|
|
75
|
|
|
$this->runHooks($beforeHooks); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
protected function runAfterHooks() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
if ($this->input->getOption('skip-all') || $this->input->getOption('skip-after')) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$afterHooks = collect(config('export.after', [])) |
85
|
|
|
->reject(function (string $hook, string $name) { |
86
|
|
|
return $this->input->getOption("skip-{$name}"); |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
if (! count($afterHooks)) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->info('Running after hooks...'); |
94
|
|
|
|
95
|
|
|
$this->runHooks($afterHooks); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function runHooks(Collection $hooks) |
99
|
|
|
{ |
100
|
|
|
foreach ($hooks as $name => $command) { |
101
|
|
|
$this->comment("[{$name}]", 'v'); |
102
|
|
|
|
103
|
|
|
if (method_exists(Process::class, 'fromShellCommandline')) { |
104
|
|
|
$process = Process::fromShellCommandline($command); |
105
|
|
|
} else { |
106
|
|
|
$process = new Process($command); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$process->mustRun(); |
110
|
|
|
|
111
|
|
|
foreach ($process as $data) { |
112
|
|
|
$this->output->write($data); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.