includeCwdVendorAutoloadIfExists(); $autoloadIncluder->autoloadProjectAutoloaderFile(); $autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists(); $autoloadIncluder->autoloadFromCommandLine(); $symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesCaller()); $symfonyStyle = $symfonyStyleFactory->create(); try { $rectorConfigsResolver = new RectorConfigsResolver(); $configFileInfos = $rectorConfigsResolver->provide(); // Build DI container $rectorContainerFactory = new RectorContainerFactory(); // shift configs as last so parameters with main config have higher priority $configShifter = new ConfigShifter(); $firstResolvedConfig = $rectorConfigsResolver->getFirstResolvedConfig(); if ($firstResolvedConfig !== null) { $configFileInfos = $configShifter->shiftInputConfigAsLast($configFileInfos, $firstResolvedConfig); } $container = $rectorContainerFactory->createFromConfigs($configFileInfos); $firstResolvedConfig = $rectorConfigsResolver->getFirstResolvedConfig(); if ($firstResolvedConfig) { /** @var Configuration $configuration */ $configuration = $container->get(Configuration::class); $configuration->setFirstResolverConfigFileInfo($firstResolvedConfig); /** @var ChangedFilesDetector $changedFilesDetector */ $changedFilesDetector = $container->get(ChangedFilesDetector::class); $changedFilesDetector->setFirstResolvedConfigFileInfo($firstResolvedConfig); } } 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 includeCwdVendorAutoloadIfExists(): void { $this->loadIfExistsAndNotLoadedYet(getcwd() . '/vendor/autoload.php'); } public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void { // Rector's vendor is already loaded if (class_exists(RectorKernel::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'); } 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; } $this->loadIfExistsAndNotLoadedYet($fileToAutoload); } private function loadIfExistsAndNotLoadedYet(string $file): void { if (! file_exists($file)) { return; } if (in_array($file, $this->alreadyLoadedAutoloadFiles, true)) { return; } $this->alreadyLoadedAutoloadFiles[] = realpath($file); require_once $file; } }