rector/src/Console/Command/InitCommand.php
Tomas Votruba 0d05a7606d Updated Rector to commit 00dd0eb4ca
00dd0eb4ca [Core] Refactor init command to use LevelSetList::UP_TO_PHP_XY set list (#1328)
2021-11-28 17:12:25 +00:00

106 lines
5.1 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Core\Console\Command;
use RectorPrefix20211128\Nette\Utils\Strings;
use Rector\Core\Configuration\Option;
use Rector\Core\Contract\Template\TemplateResolverInterface;
use Rector\Core\Exception\Template\TemplateTypeNotFoundException;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Template\DefaultResolver;
use Stringable;
use RectorPrefix20211128\Symfony\Component\Console\Command\Command;
use RectorPrefix20211128\Symfony\Component\Console\Input\InputInterface;
use RectorPrefix20211128\Symfony\Component\Console\Input\InputOption;
use RectorPrefix20211128\Symfony\Component\Console\Output\OutputInterface;
use RectorPrefix20211128\Symfony\Component\Console\Style\SymfonyStyle;
use RectorPrefix20211128\Symplify\SmartFileSystem\FileSystemGuard;
use RectorPrefix20211128\Symplify\SmartFileSystem\SmartFileSystem;
final class InitCommand extends \RectorPrefix20211128\Symfony\Component\Console\Command\Command
{
/**
* @var \Symplify\SmartFileSystem\FileSystemGuard
*/
private $fileSystemGuard;
/**
* @var \Symplify\SmartFileSystem\SmartFileSystem
*/
private $smartFileSystem;
/**
* @var \Symfony\Component\Console\Style\SymfonyStyle
*/
private $symfonyStyle;
/**
* @var \Rector\Core\Contract\Template\TemplateResolverInterface[]
*/
private $templateResolvers;
/**
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
/**
* @param TemplateResolverInterface[] $templateResolvers
*/
public function __construct(\RectorPrefix20211128\Symplify\SmartFileSystem\FileSystemGuard $fileSystemGuard, \RectorPrefix20211128\Symplify\SmartFileSystem\SmartFileSystem $smartFileSystem, \RectorPrefix20211128\Symfony\Component\Console\Style\SymfonyStyle $symfonyStyle, array $templateResolvers, \Rector\Core\Php\PhpVersionProvider $phpVersionProvider)
{
$this->fileSystemGuard = $fileSystemGuard;
$this->smartFileSystem = $smartFileSystem;
$this->symfonyStyle = $symfonyStyle;
$this->templateResolvers = $templateResolvers;
$this->phpVersionProvider = $phpVersionProvider;
parent::__construct();
}
protected function configure() : void
{
$this->setDescription('Generate rector.php configuration file');
$this->addOption(\Rector\Core\Configuration\Option::TEMPLATE_TYPE, null, \RectorPrefix20211128\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, 'A template type like default, nette, doctrine etc.', \Rector\Core\Template\DefaultResolver::TYPE);
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
protected function execute($input, $output) : int
{
$templateType = (string) $input->getOption(\Rector\Core\Configuration\Option::TEMPLATE_TYPE);
$rectorTemplateFilePath = $this->resolveTemplateFilePathByType($templateType);
$this->fileSystemGuard->ensureFileExists($rectorTemplateFilePath, __METHOD__);
$rectorRootFilePath = \getcwd() . '/rector.php';
$doesFileExist = $this->smartFileSystem->exists($rectorRootFilePath);
if ($doesFileExist) {
$this->symfonyStyle->warning('Config file "rector.php" already exists');
} else {
$this->smartFileSystem->copy($rectorTemplateFilePath, $rectorRootFilePath);
$fullPHPVersion = (string) $this->phpVersionProvider->provide();
$phpVersion = \RectorPrefix20211128\Nette\Utils\Strings::substring($fullPHPVersion, 0, 1) . \RectorPrefix20211128\Nette\Utils\Strings::substring($fullPHPVersion, 2, 1);
$fileContent = $this->smartFileSystem->readFile($rectorRootFilePath);
$fileContent = \str_replace('LevelSetList::UP_TO_PHP_XY', \sprintf('LevelSetList::UP_TO_PHP_%d', $phpVersion), $fileContent);
$this->smartFileSystem->dumpFile($rectorRootFilePath, $fileContent);
$this->symfonyStyle->success('"rector.php" config file was added');
}
return \RectorPrefix20211128\Symfony\Component\Console\Command\Command::SUCCESS;
}
private function resolveTemplateFilePathByType(string $templateType) : string
{
$rectorTemplateFilePath = null;
foreach ($this->templateResolvers as $templateResolver) {
if ($templateResolver->supports($templateType)) {
$rectorTemplateFilePath = $templateResolver->provide();
break;
}
}
if ($rectorTemplateFilePath === null) {
$templateResolverTypes = [];
foreach ($this->templateResolvers as $templateResolver) {
if (\method_exists($templateResolver, 'getType')) {
$templateResolverTypes[] = $templateResolver->getType();
} elseif ($templateResolver instanceof \Stringable) {
$templateResolverTypes[] = (string) $templateResolver;
}
}
throw new \Rector\Core\Exception\Template\TemplateTypeNotFoundException($templateType, $templateResolverTypes);
}
return $rectorTemplateFilePath;
}
}