[TASK] Add template type option for init command (#6233)

This commit is contained in:
Sebastian Schreiber 2021-04-25 21:43:59 +02:00 committed by GitHub
parent 7b1680875c
commit 255b1fcbf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 209 additions and 3 deletions

View File

@ -74,6 +74,7 @@ It supports all versions of PHP from 5.3 and major open-source projects:
- [How to Ignore Rule or Paths](/docs/how_to_ignore_rule_or_paths.md)
- [Static Reflection and Autoload](/docs/static_reflection_and_autoload.md)
- [How to Configure Rule](/docs/how_to_configure_rules.md)
- [How to Generate Configuration file](/docs/init_command.md)
### Contributing

99
docs/init_command.md Normal file
View File

@ -0,0 +1,99 @@
# How to generate a configuration file
To start quickly you can run the init command
```bash
vendor/bin/rector init
```
This will create a `rector.php` if it doesn´t already exist in your root directory with some sensitive defaults to start with.
```php
// rector.php
use Rector\Core\Configuration\Option;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
// here we can define, what sets of rules will be applied
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::SETS, [SetList::CODE_QUALITY]);
// register single rule
$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class);
};
```
The init command takes an option called `--template-type`.
If some other Rector extension like [rector-nette](https://github.com/rectorphp/rector-nette) or [typo3-rector](https://github.com/sabbelasichon/typo3-rector) provides such a custom template type you can specify it here:
```bash
vendor/bin/rector init --template-type=typo3
```
The rector.php file for TYPO3 contains useful framework specific defaults to start from:
```php
use Ssch\TYPO3Rector\Set\Typo3SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\PostRector\Rector\NameImportingPostRector;
use Rector\Core\Configuration\Option;
return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
$parameters = $containerConfigurator->parameters();
$containerConfigurator->import(Typo3SetList::TYPO3_76);
$containerConfigurator->import(Typo3SetList::TYPO3_87);
$containerConfigurator->import(Typo3SetList::TYPO3_95);
$containerConfigurator->import(Typo3SetList::TYPO3_104);
$containerConfigurator->import(Typo3SetList::TYPO3_11);
$parameters->set(Option::SKIP, [
NameImportingPostRector::class => [
'ClassAliasMap.php',
'ext_localconf.php',
'ext_emconf.php',
'ext_tables.php',
__DIR__ . '/**/Configuration/TCA/*',
__DIR__ . '/**/Configuration/RequestMiddlewares.php',
__DIR__ . '/**/Configuration/Commands.php',
__DIR__ . '/**/Configuration/AjaxRoutes.php',
__DIR__ . '/**/Configuration/Extbase/Persistence/Classes.php',
],
]);
};
```
If you just want to use the default template provided by Rector you can omit the --template-type option.
# How to add a template type as a developer
In order to provide a new template type as a developer you should create a custom template class implementing the TemplateResolverInterface:
```php
use Rector\Core\Contract\Template\TemplateResolverInterface;
final class MyCustomTemplate implements TemplateResolverInterface
{
/**
* @var string
*/
private const TYPE = 'custom';
public function provide(): string
{
return __DIR__ . '/path/to/custom/template.php.dist';
}
public function supports(string $type): bool
{
return $type === self::TYPE;
}
public function __toString(): string
{
return self::TYPE;
}
}
```

View File

@ -205,6 +205,7 @@ parameters:
paths:
- src/Configuration/Configuration.php
- src/Console/Command/ProcessCommand.php
- src/Console/Command/InitCommand.php
- '#Method (.*?) should return array<PhpParser\\Node\\(.*?)\> but returns array<PhpParser\\Node\>#'
- '#Parameter \#1 (.*?) expects Symfony\\Component\\DependencyInjection\\ContainerBuilder, Symfony\\Component\\DependencyInjection\\ContainerInterface given#'

View File

@ -157,4 +157,9 @@ final class Option
* @var string
*/
public const OPTION_NO_DIFFS = 'no-diffs';
/**
* @var string
*/
public const TEMPLATE_TYPE = 'template-type';
}

View File

@ -4,8 +4,12 @@ declare(strict_types=1);
namespace Rector\Core\Console\Command;
use Rector\Core\Configuration\Option;
use Rector\Core\Contract\Template\TemplateResolverInterface;
use Rector\Core\Template\TemplateTypeNotFound;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\PackageBuilder\Console\ShellCode;
@ -29,26 +33,45 @@ final class InitCommand extends Command
*/
private $symfonyStyle;
/**
* @var TemplateResolverInterface[]
*/
private $templateResolvers;
/**
* @param TemplateResolverInterface[] $templateResolvers
*/
public function __construct(
FileSystemGuard $fileSystemGuard,
SmartFileSystem $smartFileSystem,
SymfonyStyle $symfonyStyle
SymfonyStyle $symfonyStyle,
array $templateResolvers
) {
$this->fileSystemGuard = $fileSystemGuard;
$this->smartFileSystem = $smartFileSystem;
$this->symfonyStyle = $symfonyStyle;
$this->templateResolvers = $templateResolvers;
parent::__construct();
}
protected function configure(): void
{
$this->setDescription('Generate rector.php configuration file');
$this->addOption(
Option::TEMPLATE_TYPE,
null,
InputOption::VALUE_OPTIONAL,
'A template type like default, nette, doctrine etc.',
'default'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$rectorTemplateFilePath = __DIR__ . '/../../../templates/rector.php.dist';
$templateType = (string) $input->getOption(Option::TEMPLATE_TYPE);
$rectorTemplateFilePath = $this->resolveTemplateFilePathByType($templateType);
$this->fileSystemGuard->ensureFileExists($rectorTemplateFilePath, __METHOD__);
$rectorRootFilePath = getcwd() . '/rector.php';
@ -63,4 +86,23 @@ final class InitCommand extends Command
return ShellCode::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) {
$availableTemplateTypes = implode(', ', $this->templateResolvers);
throw TemplateTypeNotFound::typeNotFound($templateType, $availableTemplateTypes);
}
return $rectorTemplateFilePath;
}
}

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Contract\Template;
use Stringable;
interface TemplateResolverInterface extends Stringable
{
public function provide(): string;
public function supports(string $type): bool;
}

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Template;
use Rector\Core\Contract\Template\TemplateResolverInterface;
final class DefaultResolver implements TemplateResolverInterface
{
/**
* @var string
*/
private const TYPE = 'default';
public function __toString(): string
{
return self::TYPE;
}
public function provide(): string
{
return __DIR__ . '/../../templates/rector.php.dist';
}
public function supports(string $type): bool
{
return $type === self::TYPE || $type === '';
}
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Template;
use RuntimeException;
final class TemplateTypeNotFound extends RuntimeException
{
public static function typeNotFound(string $type, string $availableTypes): self
{
$message = sprintf('No template found for type %s. Possible values are %s', $type, $availableTypes);
return new self($message);
}
}