forked from TYPO3/coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupCommand.php
More file actions
132 lines (110 loc) · 3.99 KB
/
Copy pathSetupCommand.php
File metadata and controls
132 lines (110 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 project.
*
* (c) 2019-2026 Benni Mack
* Simon Gilli
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CodingStandards\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CodingStandards\Setup;
/**
* @internal
*/
final class SetupCommand extends Command
{
protected function configure(): void
{
parent::configure();
$this
->setName('setup')
->setDescription('Setting up the TYPO3 rule sets for an extension or a project')
->addArgument('type', InputArgument::OPTIONAL, sprintf(
'Type to setup, valid types are <comment>["%s"]</comment>. If not set, the detection is automatic',
implode('","', Setup::VALID_TYPES)
))
->addOption(
'force',
'f',
InputOption::VALUE_NONE,
'Replace existing files'
)
->addOption(
'rule-set',
'r',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Rule set to set up',
Setup::VALID_RULE_SETS
)
;
}
private function getForce(InputInterface $input): bool
{
return (bool)$input->getOption('force');
}
/**
* @return array<int, string>
*/
private function getRuleSets(InputInterface $input): array
{
/** @var array<int, string> $ruleSets */
$ruleSets = $input->getOption('rule-set');
return $ruleSets;
}
/**
* @throws \RuntimeException
*/
private function getType(InputInterface $input): string
{
$type = $input->getArgument('type');
if (!is_string($type) || $type === '') {
$composerManifestError = 'Cannot auto-detect type, composer.json cannot be %s. Use the type argument instead.';
$composerManifest = $this->getProjectDir() . '/composer.json';
if (!file_exists($composerManifest)) {
throw new \RuntimeException(sprintf($composerManifestError, 'found'));
}
$composerManifest = file_get_contents($composerManifest);
if ($composerManifest === false) {
throw new \RuntimeException(sprintf($composerManifestError, 'read')); // @codeCoverageIgnore
}
$composerManifest = json_decode($composerManifest, true);
if ($composerManifest === false || !is_array($composerManifest)) {
throw new \RuntimeException(sprintf($composerManifestError, 'decoded'));
}
if (
($composerManifest['type'] ?? '') === 'typo3-cms-extension'
|| ($composerManifest['extra']['typo3/cms']['extension-key'] ?? '') !== ''
) {
$type = Setup::EXTENSION;
} else {
$type = Setup::PROJECT;
}
}
return $type;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$force = $this->getForce($input);
$ruleSets = $this->getRuleSets($input);
$type = $this->getType($input);
$setup = new Setup($this->getTargetDir($input), new SymfonyStyle($input, $output));
$result = true;
if (in_array(Setup::RULE_SET_EDITORCONFIG, $ruleSets, true)) {
$result = $setup->copyEditorConfig($force);
}
if (in_array(Setup::RULE_SET_PHP_CS_FIXER, $ruleSets, true)) {
$result = $setup->copyPhpCsFixerConfig($force, $type) && $result;
}
return $result ? 0 : 1;
}
}