rector/bin/rector
Tomas Votruba 4a76cdaa82
[SymfonyPHPConfig] Add monorepo split for value objects function (#4153)
* add monorepo split for symfony-php-config

* [SOLID] Prevent replacing referenced parametes with inlined constnat value

* make Symfon 5.0- compatible

* add support for reference skip in constructor

* [rector] add support for reference skip in constructor

* [cs] add support for reference skip in constructor

* improve misisng rule feedback

* underscore

* Fix ChangeReadOnlyVariableWithDefaultValueToConstantRector for new

* add clear-cache to all commands

* colors

* [rector] colors

* [cs] colors

Co-authored-by: rector-bot <tomas@getrector.org>
2020-10-10 17:42:43 +00:00

186 lines
6.2 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
declare(strict_types=1);
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Bootstrap\ConfigShifter;
use Rector\Core\Bootstrap\NoRectorsLoadedReporter;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Configuration\MinimalVersionChecker;
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonParser;
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonReader;
use Rector\Core\Bootstrap\RectorConfigsResolver;
use Rector\Core\Console\Application;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\DependencyInjection\RectorContainerFactory;
use Rector\Core\Exception\NoRectorsLoadedException;
use Symplify\SetConfigResolver\Bootstrap\InvalidSetReporter;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
use Symplify\SetConfigResolver\Exception\SetNotFoundException;
@ini_set('memory_limit', '-1'); // @ intentionally: continue anyway
// 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->includeCwdVendorAutoloadIfExists();
$autoloadIncluder->autoloadProjectAutoloaderFile('/../../autoload.php');
$autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();
$autoloadIncluder->autoloadFromCommandLine();
$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesCaller());
$symfonyStyle = $symfonyStyleFactory->create();
try {
$composerJsonReader = new ComposerJsonReader(__DIR__ . '/../composer.json');
$versionChecker = new MinimalVersionChecker(
PHP_VERSION,
new ComposerJsonParser($composerJsonReader->read())
);
$versionChecker->check();
$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);
if ($rectorConfigsResolver->getFirstResolvedConfig()) {
/** @var Configuration $configuration */
$configuration = $container->get(Configuration::class);
$configuration->setFirstResolverConfigFileInfo($rectorConfigsResolver->getFirstResolvedConfig());
/** @var ChangedFilesDetector $changedFilesDetector */
$changedFilesDetector = $container->get(ChangedFilesDetector::class);
$changedFilesDetector->setFirstResolvedConfigFileInfo($rectorConfigsResolver->getFirstResolvedConfig());
}
} catch (SetNotFoundException $setNotFoundException) {
(new InvalidSetReporter())->report($setNotFoundException);
exit(ShellCode::ERROR);
} catch (Throwable $throwable) {
$symfonyStyle->error($throwable->getMessage());
exit(ShellCode::ERROR);
}
/** @var Application $application */
$application = $container->get(Application::class);
exit($application->run());
final class AutoloadIncluder
{
/**
* @var string[]
*/
private $alreadyLoadedAutoloadFiles = [];
public function includeCwdVendorAutoloadIfExists(): void
{
$cwdVendorAutoload = getcwd() . '/vendor/autoload.php';
if (!is_file($cwdVendorAutoload)) {
return;
}
$this->loadIfNotLoadedYet($cwdVendorAutoload, __METHOD__ . '()" on line ' . __LINE__);
}
public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void
{
// Rector's vendor is already loaded
if (class_exists('Rector\HttpKernel\RectorKernel')) {
return;
}
$devOrPharVendorAutoload = __DIR__ . '/../vendor/autoload.php';
if (! is_file($devOrPharVendorAutoload)) {
return;
}
$this->loadIfNotLoadedYet($devOrPharVendorAutoload, __METHOD__ . '()" on line ' . __LINE__);
}
/**
* Inspired by https://github.com/phpstan/phpstan-src/blob/e2308ecaf49a9960510c47f5a992ce7b27f6dba2/bin/phpstan#L19
*/
public function autoloadProjectAutoloaderFile(string $file): void
{
$path = dirname(__DIR__) . $file;
if (!extension_loaded('phar')) {
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
return;
}
$pharPath = Phar::running(false);
if ($pharPath === '') {
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
} else {
$path = dirname($pharPath) . $file;
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
}
}
public function autoloadFromCommandLine(): void
{
$cliArgs = $_SERVER['argv'];
$autoloadOptionPosition = array_search('-a', $cliArgs) ?: array_search('--autoload-file', $cliArgs);
if (! $autoloadOptionPosition) {
return;
}
$autoloadFileValuePosition = $autoloadOptionPosition + 1;
$fileToAutoload = $cliArgs[$autoloadFileValuePosition] ?? null;
if ($fileToAutoload=== null) {
return;
}
$this->loadIfNotLoadedYet($fileToAutoload, __METHOD__);
}
private function loadIfNotLoadedYet(string $file, string $location): void
{
if (in_array($file, $this->alreadyLoadedAutoloadFiles, true)) {
return;
}
if ($this->isDebugOption()) {
echo sprintf(sprintf(
'File "%s" is about to be loaded in "%s"' . PHP_EOL,
$file,
$location
));
}
$this->alreadyLoadedAutoloadFiles[] = realpath($file);
require_once $file;
}
private function isDebugOption(): bool
{
return in_array('--debug', $_SERVER['argv'], true);
}
}