1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Cs\Console; |
4
|
|
|
|
5
|
|
|
use Funivan\Cs\Configuration\ConfigurationInterface; |
6
|
|
|
use Funivan\Cs\FileFinder\FinderParams; |
7
|
|
|
use Funivan\Cs\FileProcessor\FixerProcessor; |
8
|
|
|
use Funivan\Cs\FileTool\ToolsFilter; |
9
|
|
|
use Funivan\Cs\Report\Report; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Ivan Shcherbak <[email protected]> 2016 |
17
|
|
|
*/ |
18
|
|
|
abstract class BaseCommand extends Command { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritDoc |
22
|
|
|
*/ |
23
|
|
|
protected function configure() { |
24
|
|
|
|
25
|
|
|
$this->addOption('configuration', null, InputOption::VALUE_REQUIRED, 'Path to the configuration file'); |
26
|
|
|
|
27
|
|
|
$this->addOption('directory', null, InputOption::VALUE_REQUIRED, 'Process files inside this directory', null); |
28
|
|
|
$this->addOption('commit', null, InputOption::VALUE_REQUIRED, 'Process files changed in specific commit. By default we check all modified files', null); |
29
|
|
|
$this->addOption('tools', null, InputOption::VALUE_REQUIRED, 'Filter tools by name', ''); |
30
|
|
|
|
31
|
|
|
$this->addOption('list-tools', null, InputOption::VALUE_REQUIRED, 'Show Tools that will be applied to the files'); |
32
|
|
|
|
33
|
|
|
$this->addOption('list-all-tools', null, InputOption::VALUE_REQUIRED, 'Show All Tools that can be used'); |
34
|
|
|
|
35
|
|
|
parent::configure(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
protected final function execute(InputInterface $input, OutputInterface $output) { |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
$configuration = $input->getOption('configuration'); |
46
|
|
|
|
47
|
|
|
if (!empty($configuration)) { |
48
|
|
|
/** @noinspection PhpIncludeInspection */ |
49
|
|
|
/** @var ConfigurationInterface $config */ |
50
|
|
|
$config = include_once $configuration; |
51
|
|
|
} else { |
52
|
|
|
$config = $this->getDefaultConfiguration(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (empty($config)) { |
56
|
|
|
$output->writeln('<error>Provide valid configuration file path</error>'); |
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!($config instanceof ConfigurationInterface)) { |
61
|
|
|
throw new \Exception('Invalid configuration file result. Expect ' . ConfigurationInterface::class); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$tools = $config->getTools(); |
65
|
|
|
|
66
|
|
|
# list all tools |
67
|
|
|
if ($input->getOption('list-all-tools')) { |
68
|
|
|
foreach ($tools as $tool) { |
69
|
|
|
$output->writeln('Tool : ' . $tool->getName()); |
70
|
|
|
} |
71
|
|
|
return 0; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$toolsFilter = ToolsFilter::createFromString($input->getOption('tools')); |
75
|
|
|
|
76
|
|
|
foreach ($tools as $index => $tool) { |
77
|
|
|
if (!$toolsFilter->isValid($tool)) { |
78
|
|
|
unset($tools[$index]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
# list tools |
83
|
|
|
if ($input->getOption('list-tools')) { |
84
|
|
|
foreach ($tools as $tool) { |
85
|
|
|
$output->writeln('Tool : ' . $tool->getName()); |
86
|
|
|
} |
87
|
|
|
return 0; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$fileProcessor = $this->getFileProcessor($input, $output); |
91
|
|
|
foreach ($tools as $tool) { |
92
|
|
|
$fileProcessor->addTool($tool); |
93
|
|
|
$output->writeln('Add tool: ' . $tool->getName(), OutputInterface::VERBOSITY_VERY_VERBOSE); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$fileProcessor->setOutput($output); |
97
|
|
|
|
98
|
|
|
$params = new FinderParams(); |
99
|
|
|
$params->setDirectory($input->getOption('directory')); |
100
|
|
|
$params->setCommit($input->getOption('commit')); |
101
|
|
|
$files = $config->getFileFinderFactory($params)->getFileCollection(); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
if ($files->count() === 0) { |
105
|
|
|
$output->writeln('<comment>✔ Empty files list</comment>'); |
106
|
|
|
return 0; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$report = new Report(); |
110
|
|
|
|
111
|
|
|
$fileProcessor->process($files, $report); |
112
|
|
|
|
113
|
|
|
return $this->getResultState($input, $output, $report); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param InputInterface $input |
119
|
|
|
* @param OutputInterface $output |
120
|
|
|
* @return FixerProcessor |
121
|
|
|
*/ |
122
|
|
|
protected abstract function getFileProcessor(InputInterface $input, OutputInterface $output); |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param InputInterface $input |
127
|
|
|
* @param OutputInterface $output |
128
|
|
|
* @param Report $report |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
protected abstract function getResultState(InputInterface $input, OutputInterface $output, Report $report); |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return ConfigurationInterface |
136
|
|
|
*/ |
137
|
|
|
protected abstract function getDefaultConfiguration(); |
138
|
|
|
|
139
|
|
|
} |