-
-
Notifications
You must be signed in to change notification settings - Fork 455
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 d7b61d6
Showing
6 changed files
with
138 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\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) { | ||
continue; | ||
} | ||
|
||
$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,71 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Tests; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheSchemaSubscriberPass; | ||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; | ||
use Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension; | ||
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 | ||
{ | ||
if (! class_exists(PdoCacheAdapterDoctrineSchemaSubscriber::class)) { | ||
$this->markTestSkipped('This test requires Symfony 5.1 or higher'); | ||
} | ||
|
||
$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' => [ | ||
'pools' => [ | ||
'my_cache_adapter' => ['adapter' => 'cache.adapter.pdo'], | ||
], | ||
], | ||
], | ||
], $container); | ||
|
||
$extension = new DoctrineExtension(); | ||
$container->registerExtension($extension); | ||
$extension->load([ | ||
[ | ||
'dbal' => [], | ||
'orm' => [], | ||
], | ||
], $container); | ||
|
||
$container->setAlias('test_subscriber_alias', new Alias('doctrine.orm.listeners.pdo_cache_adapter_doctrine_schema_subscriber', true)); | ||
// prevent my_cache_apapter from inlining | ||
$container->register('uses_my_cache_adapter', 'stdClass') | ||
->addArgument(new Reference('my_cache_adapter')) | ||
->setPublic(true); | ||
$container->addCompilerPass(new CacheSchemaSubscriberPass(), PassConfig::TYPE_OPTIMIZE, -10); | ||
$container->compile(); | ||
|
||
// check that PdoAdapter service is injected as an argument | ||
$definition = $container->findDefinition('test_subscriber_alias'); | ||
$this->assertEquals([new Reference('my_cache_adapter')], $definition->getArgument(0)); | ||
} | ||
} |