2020-07-18 18:57:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-01-29 00:32:20 +01:00
|
|
|
use Rector\DeadDocBlock\Rector\ClassLike\RemoveAnnotationRector;
|
2020-07-18 18:57:24 +02:00
|
|
|
use Rector\Doctrine\Rector\Class_\RemoveRepositoryFromEntityAnnotationRector;
|
2020-11-12 22:53:11 +07:00
|
|
|
use Rector\Doctrine\Rector\ClassMethod\ServiceEntityRepositoryParentCallToDIRector;
|
2021-01-30 09:57:35 +01:00
|
|
|
use Rector\Doctrine\Rector\MethodCall\ReplaceParentRepositoryCallsByRepositoryPropertyRector;
|
2020-09-08 12:00:38 +02:00
|
|
|
use Rector\DoctrineCodeQuality\Rector\Class_\MoveRepositoryFromParentToConstructorRector;
|
2021-01-29 09:08:55 +01:00
|
|
|
use Rector\Removing\Rector\Class_\RemoveParentRector;
|
2021-02-14 20:51:04 +01:00
|
|
|
use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector;
|
|
|
|
use Rector\Renaming\ValueObject\RenameProperty;
|
2020-08-30 15:47:36 +02:00
|
|
|
use Rector\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector;
|
2020-10-27 01:42:04 +01:00
|
|
|
use Rector\Transform\Rector\MethodCall\ReplaceParentCallByPropertyCallRector;
|
2020-09-12 23:19:08 +02:00
|
|
|
use Rector\Transform\ValueObject\ReplaceParentCallByPropertyCall;
|
2020-07-18 18:57:24 +02:00
|
|
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
2020-11-26 17:44:27 +01:00
|
|
|
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
|
2020-07-18 18:57:24 +02:00
|
|
|
|
2020-08-11 12:59:04 +02:00
|
|
|
/**
|
|
|
|
* @see https://tomasvotruba.com/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/
|
|
|
|
* @see https://tomasvotruba.com/blog/2018/04/02/rectify-turn-repositories-to-services-in-symfony/
|
2021-02-14 00:36:53 +01:00
|
|
|
* @see https://getrector.org/blog/2021/02/08/how-to-instantly-decouple-symfony-doctrine-repository-inheritance-to-clean-composition
|
2020-08-11 12:59:04 +02:00
|
|
|
*/
|
2020-07-18 18:57:24 +02:00
|
|
|
return static function (ContainerConfigurator $containerConfigurator): void {
|
|
|
|
$services = $containerConfigurator->services();
|
|
|
|
|
|
|
|
# order matters, this needs to be first to correctly detect parent repository
|
|
|
|
|
2020-08-12 11:44:34 +02:00
|
|
|
// covers "extends EntityRepository"
|
|
|
|
$services->set(MoveRepositoryFromParentToConstructorRector::class);
|
2020-07-18 18:57:24 +02:00
|
|
|
$services->set(ReplaceParentRepositoryCallsByRepositoryPropertyRector::class);
|
|
|
|
$services->set(RemoveRepositoryFromEntityAnnotationRector::class);
|
2020-08-11 12:59:04 +02:00
|
|
|
|
2020-08-12 11:44:34 +02:00
|
|
|
// covers "extends ServiceEntityRepository"
|
|
|
|
// @see https://github.com/doctrine/DoctrineBundle/pull/727/files
|
2020-11-12 22:53:11 +07:00
|
|
|
$services->set(ServiceEntityRepositoryParentCallToDIRector::class);
|
2020-08-13 01:04:23 +02:00
|
|
|
|
2021-02-14 20:51:04 +01:00
|
|
|
$services->set(RenamePropertyRector::class)
|
|
|
|
->call('configure', [[
|
|
|
|
RenamePropertyRector::RENAMED_PROPERTIES => ValueObjectInliner::inline([
|
|
|
|
new RenameProperty(
|
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'_em',
|
|
|
|
'entityManager'
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
]]);
|
|
|
|
|
2020-08-12 11:44:34 +02:00
|
|
|
$services->set(RemoveAnnotationRector::class)
|
|
|
|
->call('configure', [[
|
|
|
|
RemoveAnnotationRector::ANNOTATIONS_TO_REMOVE => ['method'],
|
|
|
|
]]);
|
|
|
|
|
|
|
|
$services->set(ReplaceParentCallByPropertyCallRector::class)
|
|
|
|
->call('configure', [[
|
2020-11-26 17:44:27 +01:00
|
|
|
ReplaceParentCallByPropertyCallRector::PARENT_CALLS_TO_PROPERTIES => ValueObjectInliner::inline([
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'createQueryBuilder',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'createResultSetMappingBuilder',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'clear',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'find',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'findBy',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'findAll',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'count',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'getClassName',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
2020-09-12 23:19:08 +02:00
|
|
|
new ReplaceParentCallByPropertyCall(
|
2020-08-26 12:54:53 +02:00
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
'matching',
|
|
|
|
'entityRepository'
|
|
|
|
),
|
|
|
|
]),
|
2020-08-12 11:44:34 +02:00
|
|
|
]]
|
|
|
|
);
|
|
|
|
|
|
|
|
$services->set(MethodCallToPropertyFetchRector::class)
|
|
|
|
->call('configure', [[
|
|
|
|
MethodCallToPropertyFetchRector::METHOD_CALL_TO_PROPERTY_FETCHES => [
|
|
|
|
'getEntityManager' => 'entityManager',
|
|
|
|
],
|
|
|
|
]]);
|
|
|
|
|
|
|
|
$services->set(RemoveParentRector::class)
|
|
|
|
->call('configure', [[
|
|
|
|
RemoveParentRector::PARENT_TYPES_TO_REMOVE => [
|
|
|
|
'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository',
|
|
|
|
],
|
|
|
|
]]);
|
2020-07-18 18:57:24 +02:00
|
|
|
};
|