add RectorProviderInterface + CompilerPass to load it

This commit is contained in:
TomasVotruba 2018-02-23 20:00:31 +01:00
parent 07d3641cbd
commit 38443547ec
8 changed files with 92 additions and 11 deletions

View File

@ -4,15 +4,24 @@ namespace Rector\RectorBuilder;
use PhpParser\Node;
use Rector\Contract\Rector\RectorInterface;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
final class CaseRector implements RectorInterface
{
public function __construct()
/**
* @var MethodCallAnalyzer
*/
private $methodCallAnalyzer;
public function __construct(MethodCallAnalyzer $methodCallAnalyzer)
{
$this->methodCallAnalyzer = $methodCallAnalyzer;
}
public function isCandidate(Node $node): bool
{
dump('EE');
die;
// TODO: Implement isCandidate() method.
}

View File

@ -26,6 +26,16 @@ final class CaseRectorBuilder
*/
private $newArguments = [];
/**
* @var CaseRector
*/
private $caseRector;
public function __construct(CaseRector $caseRector)
{
$this->caseRector = $caseRector;
}
public function matchMethodCallByType(string $type): self
{
$this->type = $type;
@ -56,9 +66,10 @@ final class CaseRectorBuilder
public function create(): RectorInterface
{
// $caseRector = new CaseRector()
$caseRector = clone $this->caseRector;
dump('the work :)');
die;
// @todo: configure by all that has been setup here
return $caseRector;
}
}

View File

@ -0,0 +1,10 @@
<?php declare(strict_types=1);
namespace Rector\RectorBuilder\Contract;
use Rector\Contract\Rector\RectorInterface;
interface RectorProviderInterface
{
public function provide(): RectorInterface;
}

View File

@ -0,0 +1,23 @@
<?php declare(strict_types=1);
namespace Rector\RectorBuilder\DependencyInjection\CompilerPass;
use Rector\Rector\RectorCollector;
use Rector\RectorBuilder\Contract\RectorProviderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symplify\PackageBuilder\DependencyInjection\DefinitionFinder;
final class RectorProvidersCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $containerBuilder): void
{
$rectorCollectorDefinition = DefinitionFinder::getByType($containerBuilder, RectorCollector::class);
$rectorProviderDefinitions = DefinitionFinder::findAllByType($containerBuilder, RectorProviderInterface::class);
foreach ($rectorProviderDefinitions as $rectorProviderDefinition) {
$methodCallArgument = sprintf("@=service('%s').provide()", $rectorProviderDefinition->getClass());
$rectorCollectorDefinition->addMethodCall('addRector', [$methodCallArgument]);
}
}
}

View File

@ -3,5 +3,5 @@ services:
autowire: true
Rector\RectorBuilder\:
resource: '../'
exclude: '../{BuilderRector.php}'
resource: ..
exclude: ../{CaseRector.php}

View File

@ -4,6 +4,7 @@ namespace Rector\DependencyInjection;
use Rector\DependencyInjection\CompilerPass\CollectorCompilerPass;
use Rector\NodeTypeResolver\DependencyInjection\CompilerPass\NodeTypeResolverCollectorCompilerPass;
use Rector\RectorBuilder\DependencyInjection\CompilerPass\RectorProvidersCompilerPass;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
@ -58,6 +59,7 @@ final class RectorKernel extends Kernel
protected function build(ContainerBuilder $containerBuilder): void
{
$containerBuilder->addCompilerPass(new CollectorCompilerPass());
$containerBuilder->addCompilerPass(new RectorProvidersCompilerPass());
$containerBuilder->addCompilerPass(new NodeTypeResolverCollectorCompilerPass());
}
}

View File

@ -0,0 +1,29 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\Nette\Application\ValidateControlRector;
use Rector\Contract\Rector\RectorInterface;
use Rector\RectorBuilder\CaseRectorBuilder;
use Rector\RectorBuilder\Contract\RectorProviderInterface;
final class RectorProvider implements RectorProviderInterface
{
/**
* @var CaseRectorBuilder
*/
private $caseRectorBuilder;
public function __construct(CaseRectorBuilder $caseRectorBuilder)
{
$this->caseRectorBuilder = $caseRectorBuilder;
}
public function provide(): RectorInterface
{
return $this->caseRectorBuilder->matchMethodCallByType('@todo')
->matchMethodName('@todo')
->changeMethodNameTo('@todo')
->addArgument(2, '@todo')
->create();
}
}

View File

@ -1,5 +1,2 @@
rectors:
factory: [
'@RectorBuilder
]
services:
Rector\Tests\Rector\Contrib\Nette\Application\ValidateControlRector\RectorProvider: ~