Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Messenger & Cache schema subscribers in Symfony 5.1
- Loading branch information
1 parent
98e038d
commit 7a6308f
Showing
6 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
DependencyInjection/Compiler/CacheSchemaSubscriberPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Bridge\Doctrine\SchemaListener\MessengerTransportDoctrineSchemaSubscriber; | ||
use Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber; | ||
use Symfony\Component\Cache\Adapter\PdoAdapter; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Injects PdoAdapter into its schema subscriber. | ||
* | ||
* Must be run later after ResolveChildDefinitionsPass. | ||
*/ | ||
class CacheSchemaSubscriberPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$subscriberId = 'doctrine.orm.listeners.pdo_cache_adapter_doctrine_schema_subscriber'; | ||
|
||
if (!$container->hasDefinition($subscriberId)) { | ||
return; | ||
} | ||
|
||
$cacheAdaptersReferences = []; | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if ($definition->isAbstract() || $definition->isSynthetic()) { | ||
continue; | ||
} | ||
|
||
if ($definition->getClass() === PdoAdapter::class) { | ||
$cacheAdaptersReferences[] = new Reference($id); | ||
} | ||
} | ||
|
||
$container->getDefinition($subscriberId) | ||
->replaceArgument(0, $cacheAdaptersReferences); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Tests; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheSchemaSubscriberPass; | ||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass; | ||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; | ||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use Doctrine\ORM\EntityRepository; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomClassRepoEntity; | ||
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomServiceRepoEntity; | ||
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestDefaultRepoEntity; | ||
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository; | ||
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoRepository; | ||
use Fixtures\Bundles\RepositoryServiceBundle\RepositoryServiceBundle; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension; | ||
use Symfony\Component\Cache\Adapter\PdoAdapter; | ||
use Symfony\Component\DependencyInjection\Alias; | ||
use Symfony\Component\DependencyInjection\Compiler\PassConfig; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class CacheSchemaSubscriberTest extends TestCase | ||
{ | ||
public function testSchemaSubscriberWiring() : void | ||
{ | ||
$container = new ContainerBuilder(new ParameterBag([ | ||
'kernel.name' => 'app', | ||
'kernel.debug' => false, | ||
'kernel.bundles' => [], | ||
'kernel.cache_dir' => sys_get_temp_dir(), | ||
'kernel.environment' => 'test', | ||
'kernel.root_dir' => __DIR__ . '/../../../../', // src dir | ||
'kernel.project_dir' => __DIR__ . '/../../../../', // src dir | ||
'kernel.bundles_metadata' => [], | ||
'kernel.charset' => 'UTF-8', | ||
'kernel.container_class' => ContainerBuilder::class, | ||
'kernel.secret' => 'test', | ||
'env(base64:default::SYMFONY_DECRYPTION_SECRET)' => 'foo', | ||
])); | ||
|
||
$extension = new FrameworkExtension(); | ||
$container->registerExtension($extension); | ||
$extension->load(['framework' => ['cache' => [ | ||
'app' => 'cache.adapter.pdo' | ||
]]], $container); | ||
|
||
$extension = new DoctrineExtension(); | ||
$container->registerExtension($extension); | ||
$extension->load([ | ||
[ | ||
'dbal' => [], | ||
'orm' => [], | ||
], | ||
], $container); | ||
|
||
$container->setAlias('test_alias', new Alias('doctrine.orm.listeners.pdo_cache_adapter_doctrine_schema_subscriber', true)); | ||
$container->addCompilerPass(new CacheSchemaSubscriberPass(), PassConfig::TYPE_OPTIMIZE, -10); | ||
$container->compile(); | ||
|
||
// sanity check | ||
$this->assertSame(PdoAdapter::class, $container->getDefinition('cache.app')->getClass()); | ||
// check that PdoAdapter service is injected as an argument | ||
$definition = $container->findDefinition('test_alias'); | ||
$this->assertEquals([new Reference('cache.app')], $definition->getArgument(0)); | ||
} | ||
} |