forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApplication.php
More file actions
164 lines (134 loc) · 5 KB
/
ConsoleApplication.php
File metadata and controls
164 lines (134 loc) · 5 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Rector\Console;
use Composer\XdebugHandler\XdebugHandler;
use Rector\Application\VersionResolver;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Configuration\Option;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Webmozart\Assert\Assert;
final class ConsoleApplication extends Application
{
/**
* @var string
*/
private const NAME = 'Rector';
/**
* @param Command[] $commands
*/
public function __construct(array $commands)
{
parent::__construct(self::NAME, VersionResolver::PACKAGE_VERSION);
Assert::notEmpty($commands);
Assert::allIsInstanceOf($commands, Command::class);
$this->addCommands($commands);
// run this command, if no command name is provided
$this->setDefaultCommand('process');
}
public function doRun(InputInterface $input, OutputInterface $output): int
{
$this->enableXdebug($input);
$shouldFollowByNewline = false;
// skip in this case, since generate content must be clear from meta-info
if ($this->shouldPrintMetaInformation($input)) {
$output->writeln($this->getLongVersion());
$shouldFollowByNewline = true;
}
if ($shouldFollowByNewline) {
$output->write(PHP_EOL);
}
$commandName = $input->getFirstArgument();
// if paths exist or if the command name is not the first argument but with --option, eg:
// bin/rector src
// bin/rector --only "RemovePhpVersionIdCheckRector"
// file_exists() can check directory and file
if (is_string($commandName) && (
file_exists($commandName) || isset($_SERVER['argv'][1])
&& $commandName !== $_SERVER['argv'][1]
// ensure verify has parameter option, eg: --only
&& $input->hasParameterOption($_SERVER['argv'][1])
)
) {
// prepend command name if implicit
$privatesAccessor = new PrivatesAccessor();
$tokens = $privatesAccessor->getPrivateProperty($input, 'tokens');
$tokens = array_merge(['process'], $tokens);
$privatesAccessor->setPrivateProperty($input, 'tokens', $tokens);
}
return parent::doRun($input, $output);
}
protected function getDefaultInputDefinition(): InputDefinition
{
$defaultInputDefinition = parent::getDefaultInputDefinition();
$this->removeUnusedOptions($defaultInputDefinition);
$this->addCustomOptions($defaultInputDefinition);
return $defaultInputDefinition;
}
private function shouldPrintMetaInformation(InputInterface $input): bool
{
$hasNoArguments = $input->getFirstArgument() === null;
if ($hasNoArguments) {
return false;
}
$hasVersionOption = $input->hasParameterOption('--version');
if ($hasVersionOption) {
return false;
}
$outputFormat = $input->getParameterOption(['-o', '--output-format']);
return $outputFormat === ConsoleOutputFormatter::NAME;
}
private function removeUnusedOptions(InputDefinition $inputDefinition): void
{
$options = $inputDefinition->getOptions();
unset($options['quiet'], $options['verbose'], $options['no-interaction']);
$inputDefinition->setOptions($options);
}
private function addCustomOptions(InputDefinition $inputDefinition): void
{
$inputDefinition->addOption(new InputOption(
Option::CONFIG,
'c',
InputOption::VALUE_REQUIRED,
'Path to config file',
$this->getDefaultConfigPath()
));
$inputDefinition->addOption(new InputOption(
Option::DEBUG,
null,
InputOption::VALUE_NONE,
'Enable debug verbosity'
));
$inputDefinition->addOption(new InputOption(
Option::XDEBUG,
null,
InputOption::VALUE_NONE,
'Allow running xdebug'
));
$inputDefinition->addOption(new InputOption(
Option::CLEAR_CACHE,
null,
InputOption::VALUE_NONE,
'Clear cache before starting the execution of the command'
));
}
private function getDefaultConfigPath(): string
{
return getcwd() . '/rector.php';
}
private function enableXdebug(InputInterface $input): void
{
$isXdebugAllowed = $input->hasParameterOption('--xdebug');
if (! $isXdebugAllowed) {
$xdebugHandler = new XdebugHandler('rector');
$xdebugHandler->setPersistent();
$xdebugHandler->check();
unset($xdebugHandler);
}
}
}