add --level shortcut option

This commit is contained in:
TomasVotruba 2017-10-30 14:26:44 +01:00
parent c6dc293511
commit a9c7afc1ec
3 changed files with 66 additions and 6 deletions

View File

@ -1,5 +1,6 @@
<?php declare(strict_types=1);
use Rector\Configuration\RectorConfigFilePathHelper;
use Rector\Console\Application;
use Rector\DependencyInjection\ContainerFactory;
use Symfony\Component\Console\Input\ArgvInput;
@ -13,13 +14,17 @@ gc_disable();
require_once __DIR__ . '/bootstrap.php';
try {
// 1. Detect configuration
ConfigFilePathHelper::detectFromInput('rector', new ArgvInput);
// 1. Detect configuration from --level
$configFile = RectorConfigFilePathHelper::resolveLevel(new ArgvInput);
// 2. Or from --config
if ($configFile === null) {
ConfigFilePathHelper::detectFromInput('rector', new ArgvInput);
$configFile = ConfigFilePathHelper::provide('rector', 'rector.yml');
}
// 2. Build DI container
$containerFactory = new ContainerFactory;
$configFile = ConfigFilePathHelper::provide('rector', 'rector.yml');
if ($configFile) {
$container = $containerFactory->createWithConfig($configFile);
} else {

View File

@ -0,0 +1,49 @@
<?php declare(strict_types=1);
namespace Rector\Configuration;
use SplFileInfo;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Finder\Finder;
final class RectorConfigFilePathHelper
{
/**
* @var string
*/
private const LEVEL_OPTION_NAME = '--level';
/**
* @var string
*/
private static $levelDirectory = __DIR__ . '/../config/level';
public static function resolveLevel(InputInterface $input): ?string
{
if (! $input->hasParameterOption(self::LEVEL_OPTION_NAME)) {
return null;
}
$levelConfigName = $input->getParameterOption(self::LEVEL_OPTION_NAME);
$finder = Finder::create()
->files()
->name($levelConfigName . '.yml')
->in(self::$levelDirectory);
$firstFile = self::getFirstFileFromFinder($finder);
if (! $firstFile) {
return null;
}
return $firstFile->getRealPath();
}
private static function getFirstFileFromFinder(Finder $finder): ?SplFileInfo
{
$iterator = $finder->getIterator();
$iterator->rewind();
return $iterator->current();
}
}

View File

@ -20,8 +20,8 @@ final class Application extends SymfonyApplication
}
/**
* This method override adds option to
* load custom config via --config in any command.
* This method override adds option to load custom config via --config in any command.
* And --level option as well.
*/
protected function getDefaultInputDefinition(): InputDefinition
{
@ -34,6 +34,12 @@ final class Application extends SymfonyApplication
'Path to config file.',
getcwd() . '/rector.yml'
),
new InputOption(
'--level',
null,
InputOption::VALUE_REQUIRED,
'Finds config by shortcut name.'
),
new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'),
new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),