2020-11-26 17:44:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
use Rector\Core\Bootstrap\RectorConfigsResolver;
|
|
|
|
use Rector\Core\Console\ConsoleApplication;
|
|
|
|
use Rector\Core\Console\Style\SymfonyStyleFactory;
|
|
|
|
use Rector\Core\DependencyInjection\RectorContainerFactory;
|
2020-11-27 17:32:38 +01:00
|
|
|
use Rector\Core\HttpKernel\RectorKernel;
|
2020-11-26 17:44:27 +01:00
|
|
|
use Symplify\PackageBuilder\Console\ShellCode;
|
|
|
|
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
|
|
|
|
use Symplify\SetConfigResolver\Bootstrap\InvalidSetReporter;
|
|
|
|
use Symplify\SetConfigResolver\Exception\SetNotFoundException;
|
|
|
|
|
|
|
|
// @ 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();
|
2020-12-26 00:17:31 +01:00
|
|
|
|
|
|
|
$autoloadIncluder->loadIfExistsAndNotLoadedYet(__DIR__ . '/../vendor/scoper-autoload.php');
|
|
|
|
$autoloadIncluder->loadIfExistsAndNotLoadedYet(getcwd() . '/vendor/autoload.php');
|
|
|
|
|
|
|
|
$autoloadIncluder->autoloadProjectAutoloaderFile();
|
2020-11-26 17:44:27 +01:00
|
|
|
$autoloadIncluder->autoloadFromCommandLine();
|
|
|
|
|
|
|
|
$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesCaller());
|
|
|
|
$symfonyStyle = $symfonyStyleFactory->create();
|
|
|
|
|
2021-02-20 20:48:04 +01:00
|
|
|
$rectorConfigsResolver = new RectorConfigsResolver();
|
|
|
|
|
2020-11-26 17:44:27 +01:00
|
|
|
try {
|
2021-02-20 20:48:04 +01:00
|
|
|
$bootstrapConfigs = $rectorConfigsResolver->provide();
|
2020-11-26 17:44:27 +01:00
|
|
|
|
|
|
|
$rectorContainerFactory = new RectorContainerFactory();
|
2021-02-20 20:48:04 +01:00
|
|
|
$container = $rectorContainerFactory->createFromBootstrapConfigs($bootstrapConfigs);
|
2020-11-26 17:44:27 +01:00
|
|
|
} catch (SetNotFoundException $setNotFoundException) {
|
|
|
|
$invalidSetReporter = new InvalidSetReporter();
|
|
|
|
$invalidSetReporter->report($setNotFoundException);
|
|
|
|
exit(ShellCode::ERROR);
|
|
|
|
} catch (Throwable $throwable) {
|
|
|
|
$symfonyStyle->error($throwable->getMessage());
|
|
|
|
exit(ShellCode::ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var ConsoleApplication $application */
|
|
|
|
$application = $container->get(ConsoleApplication::class);
|
|
|
|
exit($application->run());
|
|
|
|
|
|
|
|
final class AutoloadIncluder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $alreadyLoadedAutoloadFiles = [];
|
|
|
|
|
|
|
|
public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void
|
|
|
|
{
|
|
|
|
// Rector's vendor is already loaded
|
2020-11-27 17:32:38 +01:00
|
|
|
if (class_exists(RectorKernel::class)) {
|
2020-11-26 17:44:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-27 21:00:10 +00:00
|
|
|
// in Rector develop repository
|
|
|
|
$this->loadIfExistsAndNotLoadedYet(__DIR__ . '/../vendor/autoload.php');
|
2020-11-26 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-27 21:00:10 +00:00
|
|
|
* In case Rector is installed as vendor dependency,
|
|
|
|
* this autoloads the project vendor/autoload.php, including Rector
|
2020-11-26 17:44:27 +01:00
|
|
|
*/
|
2020-11-27 21:00:10 +00:00
|
|
|
public function autoloadProjectAutoloaderFile(): void
|
2020-11-26 17:44:27 +01:00
|
|
|
{
|
2020-11-27 21:00:10 +00:00
|
|
|
$this->loadIfExistsAndNotLoadedYet(__DIR__ . '/../../autoload.php');
|
2020-11-26 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function autoloadFromCommandLine(): void
|
|
|
|
{
|
|
|
|
$cliArgs = $_SERVER['argv'];
|
|
|
|
|
|
|
|
$autoloadOptionPosition = array_search('-a', $cliArgs, true) ?: array_search('--autoload-file', $cliArgs, true);
|
|
|
|
if (! $autoloadOptionPosition) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$autoloadFileValuePosition = $autoloadOptionPosition + 1;
|
|
|
|
$fileToAutoload = $cliArgs[$autoloadFileValuePosition] ?? null;
|
|
|
|
if ($fileToAutoload === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-27 21:00:10 +00:00
|
|
|
$this->loadIfExistsAndNotLoadedYet($fileToAutoload);
|
2020-11-26 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:17:31 +01:00
|
|
|
public function loadIfExistsAndNotLoadedYet(string $filePath): void
|
2020-11-26 17:44:27 +01:00
|
|
|
{
|
2020-12-24 23:01:30 +01:00
|
|
|
if (! file_exists($filePath)) {
|
2020-11-26 17:44:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-24 23:01:30 +01:00
|
|
|
if (in_array($filePath, $this->alreadyLoadedAutoloadFiles, true)) {
|
2020-11-27 21:00:10 +00:00
|
|
|
return;
|
2020-11-26 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
2020-12-24 23:01:30 +01:00
|
|
|
$this->alreadyLoadedAutoloadFiles[] = realpath($filePath);
|
2020-11-26 17:44:27 +01:00
|
|
|
|
2020-12-24 23:01:30 +01:00
|
|
|
require_once $filePath;
|
2020-11-26 17:44:27 +01:00
|
|
|
}
|
|
|
|
}
|