1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Pluswerk\TypoScriptAutoFixer\Command; |
5
|
|
|
|
6
|
|
|
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Configuration; |
7
|
|
|
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Reader\GrumphpConfigurationReader; |
8
|
|
|
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Reader\YamlConfigurationReader; |
9
|
|
|
use Pluswerk\TypoScriptAutoFixer\Fixer\IssueFixer; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
final class FixCommand extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var IssueFixer |
20
|
|
|
*/ |
21
|
|
|
private $issueFixer; |
22
|
|
|
|
23
|
|
|
public function __construct(string $name = null) |
24
|
|
|
{ |
25
|
|
|
$name = $name ?? 'fix'; |
26
|
|
|
parent::__construct($name); |
27
|
|
|
$this->issueFixer = new IssueFixer(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this->addArgument('files', InputArgument::IS_ARRAY, 'files to fix', []); |
33
|
|
|
$this->addOption( |
34
|
|
|
'typoscript-linter-configuration', |
35
|
|
|
't', |
36
|
|
|
InputOption::VALUE_NONE, |
37
|
|
|
'if set the configuration file style is the typoscript-lint.yml file style' |
38
|
|
|
); |
39
|
|
|
$this->addOption( |
40
|
|
|
'grumphp-configuration', |
41
|
|
|
'g', |
42
|
|
|
InputOption::VALUE_NONE, |
43
|
|
|
'if set the configuration file style is the grumphp.yml file style' |
44
|
|
|
); |
45
|
|
|
$this->addOption( |
46
|
|
|
'configuration-file', |
47
|
|
|
'c', |
48
|
|
|
InputOption::VALUE_REQUIRED, |
49
|
|
|
'if set the configuration file of given path is used!', |
50
|
|
|
'' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param InputInterface $input |
56
|
|
|
* @param OutputInterface $output |
57
|
|
|
* |
58
|
|
|
* @return int|null |
59
|
|
|
*/ |
60
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
61
|
|
|
{ |
62
|
|
|
$this->initConfiguration($input); |
63
|
|
|
$files = $input->getArgument('files'); |
64
|
|
|
|
65
|
|
|
if (count($files) > 0) { |
|
|
|
|
66
|
|
|
foreach ($files as $file) { |
67
|
|
|
if (is_file($file)) { |
68
|
|
|
$this->issueFixer->fixIssuesForFile($file); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param InputInterface $input |
77
|
|
|
*/ |
78
|
|
|
private function initConfiguration(InputInterface $input): void |
79
|
|
|
{ |
80
|
|
|
$configuration = Configuration::getInstance(); |
81
|
|
|
$configReader = null; |
82
|
|
|
|
83
|
|
|
if ($input->getOption('typoscript-linter-configuration')) { |
84
|
|
|
$configReader = new YamlConfigurationReader(); |
85
|
|
|
} elseif ($input->getOption('grumphp-configuration')) { |
86
|
|
|
$configReader = new GrumphpConfigurationReader(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$configurationFile = $input->getOption('configuration-file'); |
90
|
|
|
|
91
|
|
|
if ($configurationFile !== '' && $input->getOption('typoscript-linter-configuration')) { |
92
|
|
|
$configReader = new YamlConfigurationReader($configurationFile); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($configurationFile !== '' && $input->getOption('grumphp-configuration')) { |
96
|
|
|
$configReader = new GrumphpConfigurationReader($configurationFile); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$configuration->init($configReader); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|