Repository navigation
Expand file tree
/
Copy pathrector.php
More file actions
executable file
·207 lines (169 loc) · 6.95 KB
/
Copy pathrector.php
File metadata and controls
executable file
·207 lines (169 loc) · 6.95 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
use Composer\XdebugHandler\XdebugHandler;
use Nette\Utils\Json;
use PHPStan\PharAutoloader;
use PHPStan\Turbo\TurboExtensionEnabler;
use Rector\Bootstrap\AutoloadFileParameterResolver;
use Rector\Bootstrap\RectorConfigsResolver;
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\Configuration\Option;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\DependencyInjection\LazyContainerFactory;
use Rector\DependencyInjection\RectorContainerFactory;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
// @ intentionally: continue anyway
@ini_set('memory_limit', '-1');
// Performance boost
error_reporting(E_ALL);
ini_set('display_errors', 'stderr');
gc_disable();
define('__RECTOR_RUNNING__', true);
// Require Composer autoload.php
$autoloadIncluder = new AutoloadIncluder();
$autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();
// rector-src on split packages: Turbo resolves php-parser classes through the project autoloader,
// as preload-split-package.php declares them only after Turbo is activated
$isSplitPackage = file_exists(__DIR__ . '/../preload-split-package.php') && is_dir(__DIR__ . '/../../../../vendor');
if ($isSplitPackage) {
$autoloadIncluder->autoloadProjectAutoloaderFile();
}
// PHPStan Turbo replaces php-parser classes, so it is activated before preload.php declares them.
// The main process only gets Turbo when php.ini loads it; the workers get it from WorkerCommandLineFactory.
$phpstanBootstrapFile = __DIR__ . '/../../../phpstan/phpstan/bootstrap.php';
if (! class_exists(PharAutoloader::class) && is_file($phpstanBootstrapFile)) {
// Rector installed as a dependency: the project autoloader that loads PHPStan comes after preload.php,
// so load PHPStan's bootstrap now, and mark it as loaded with the identifier Composer gives the file
$GLOBALS['__composer_autoload_files'][md5('phpstan/phpstan:bootstrap.php')] = true;
require_once $phpstanBootstrapFile;
}
if (class_exists(PharAutoloader::class)) {
$phpstanPharPath = dirname((string) new ReflectionClass(PharAutoloader::class)->getFileName()) . '/phpstan.phar';
TurboExtensionEnabler::trustOwnTypesIfSuitable($_SERVER['argv'], $phpstanPharPath);
TurboExtensionEnabler::activateIfCompatible($phpstanPharPath);
unset($phpstanPharPath);
}
unset($phpstanBootstrapFile);
final class AutoloadIncluder
{
/**
* @var string[]
*/
private array $alreadyLoadedAutoloadFiles = [];
public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void
{
// Rector's vendor is already loaded
if (class_exists(LazyContainerFactory::class)) {
return;
}
// in Rector develop repository
$this->loadIfExistsAndNotLoadedYet(__DIR__ . '/../vendor/autoload.php');
}
/**
* In case Rector is installed as vendor dependency,
* this autoloads the project vendor/autoload.php, including Rector
*/
public function autoloadProjectAutoloaderFile(): void
{
$this->loadIfExistsAndNotLoadedYet(__DIR__ . '/../../../autoload.php');
}
/**
* In case Rector is installed as global dependency
*/
public function autoloadRectorInstalledAsGlobalDependency(): void
{
if (dirname(__DIR__) === dirname(getcwd(), 2)) {
return;
}
if (is_dir('vendor/rector/rector')) {
return;
}
$this->loadIfExistsAndNotLoadedYet('vendor/autoload.php');
}
public function autoloadFromCommandLine(): void
{
$cliArgs = $_SERVER['argv'];
$aOptionPosition = array_search('-a', $cliArgs, true);
$autoloadFileOptionPosition = array_search('--autoload-file', $cliArgs, true);
if (is_int($aOptionPosition)) {
$autoloadOptionPosition = $aOptionPosition;
} elseif (is_int($autoloadFileOptionPosition)) {
$autoloadOptionPosition = $autoloadFileOptionPosition;
} else {
return;
}
$autoloadFileValuePosition = $autoloadOptionPosition + 1;
$fileToAutoload = $cliArgs[$autoloadFileValuePosition] ?? null;
if ($fileToAutoload === null) {
return;
}
$this->loadIfExistsAndNotLoadedYet($fileToAutoload);
}
public function loadIfExistsAndNotLoadedYet(string $filePath): void
{
if (! file_exists($filePath)) {
return;
}
if (in_array($filePath, $this->alreadyLoadedAutoloadFiles, true)) {
return;
}
/** @var non-empty-string $realPath always string after file_exists() check */
$realPath = realpath($filePath);
$this->alreadyLoadedAutoloadFiles[] = $realPath;
require_once $filePath;
}
}
if (file_exists(__DIR__ . '/../preload.php') && is_dir(__DIR__ . '/../vendor')) {
require_once __DIR__ . '/../preload.php';
}
// require rector-src on split packages
if ($isSplitPackage) {
require_once __DIR__ . '/../preload-split-package.php';
}
unset($isSplitPackage);
$autoloadIncluder->loadIfExistsAndNotLoadedYet(__DIR__ . '/../vendor/scoper-autoload.php');
$autoloadIncluder->autoloadProjectAutoloaderFile();
$autoloadIncluder->autoloadRectorInstalledAsGlobalDependency();
$autoloadIncluder->autoloadFromCommandLine();
// restart without Xdebug before the container is built, so the restarted process does not repeat that work
if (! new ArgvInput()->hasParameterOption('--xdebug')) {
$xdebugHandler = new XdebugHandler('rector');
$xdebugHandler->setPersistent();
$xdebugHandler->check();
unset($xdebugHandler);
}
AutoloadFileParameterResolver::resolveFromArgv($_SERVER['argv']);
$rectorConfigsResolver = new RectorConfigsResolver();
try {
$bootstrapConfigs = $rectorConfigsResolver->provide();
$rectorContainerFactory = new RectorContainerFactory();
$container = $rectorContainerFactory->createFromBootstrapConfigs($bootstrapConfigs);
} catch (Throwable $throwable) {
// for json output
$argvInput = new ArgvInput();
$outputFormat = $argvInput->getParameterOption('--' . Option::OUTPUT_FORMAT);
// report fatal error in json format
if ($outputFormat === JsonOutputFormatter::NAME) {
$errors = [];
do {
$errors[] = $throwable->getMessage();
} while ($throwable = $throwable->getPrevious());
echo Json::encode([
'fatal_errors' => $errors,
]);
} else {
// report fatal errors in console format
$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesAccessor());
$symfonyStyle = $symfonyStyleFactory->create();
do {
$symfonyStyle->error(str_replace("\r\n", "\n", $throwable->getMessage()));
} while ($throwable = $throwable->getPrevious());
}
exit(Command::FAILURE);
}
/** @var Application $application */
$application = $container->get(Application::class);
exit($application->run());