1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HotRodCli\Commands; |
4
|
|
|
|
5
|
|
|
use HotRodCli\Jobs\Module\ReplaceText; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use HotRodCli\AppContainer; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class BaseCommand extends Command |
12
|
|
|
{ |
13
|
|
|
protected $appContainer; |
14
|
|
|
|
15
|
|
|
protected $jobs = []; |
16
|
|
|
|
17
|
|
|
protected $processors = []; |
18
|
|
|
|
19
|
|
|
protected $configs = [ |
20
|
|
|
'arguments' => [], |
21
|
|
|
'options' => [], |
22
|
|
|
'description' => '', |
23
|
|
|
'name' => '', |
24
|
|
|
'help' => '' |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** @var ReplaceText */ |
28
|
|
|
protected $replaceTextJob; |
29
|
|
|
|
30
|
|
|
public function __construct(AppContainer $appContainer) |
31
|
|
|
{ |
32
|
|
|
$this->appContainer = $appContainer; |
33
|
|
|
$this->replaceTextJob = $this->appContainer->resolve(ReplaceText::class); |
34
|
|
|
|
35
|
|
|
parent::__construct(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setJobs() |
39
|
|
|
{ |
40
|
|
|
foreach ($this->jobs as $key => $job) { |
41
|
|
|
$this->jobs[$key] = $this->appContainer->resolve($key); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setProcessors() |
46
|
|
|
{ |
47
|
|
|
foreach ($this->processors as $key => $processor) { |
48
|
|
|
$this->processors[$key] = $this->appContainer->resolve($key); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function config(BaseCommand $command) |
53
|
|
|
{ |
54
|
|
|
$configs = $command->configs; |
55
|
|
|
$command->setName($configs['name'])->setDescription($configs['description'])->setHelp($configs['help']); |
56
|
|
|
|
57
|
|
|
foreach ($configs['arguments'] as $argument) { |
58
|
|
|
$command->addArgument( |
59
|
|
|
$argument['name'], |
60
|
|
|
$argument['mode'], |
61
|
|
|
$argument['description'] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
foreach ($configs['options'] as $option) { |
66
|
|
|
$command->addOption( |
67
|
|
|
$option['name'], |
68
|
|
|
$option['shortcut'], |
69
|
|
|
$option['mode'], |
70
|
|
|
$option['description'] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function runProcessors(InputInterface $input, OutputInterface $output) |
76
|
|
|
{ |
77
|
|
|
foreach ($this->processors as $processor) { |
78
|
|
|
$processor($input, $output); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function replaceTextsSequence(array $sequence, string $destination) |
85
|
|
|
{ |
86
|
|
|
foreach ($sequence as $needle => $value) { |
87
|
|
|
$this->replaceTextJob->handle($needle, $value, $destination); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|