Skip to content

Commit

Permalink
[DoctrineBundle] feat: doctrine schema subscribers as listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
alli83 authored and ostrolucky committed Mar 12, 2023
1 parent 402d02b commit dcf256e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
3 changes: 3 additions & 0 deletions DependencyInjection/Compiler/CacheSchemaSubscriberPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class CacheSchemaSubscriberPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
// deprecated in Symfony 6.3
$this->injectAdapters($container, 'doctrine.orm.listeners.doctrine_dbal_cache_adapter_schema_subscriber', DoctrineDbalAdapter::class);

$this->injectAdapters($container, 'doctrine.orm.listeners.doctrine_dbal_cache_adapter_schema_listener', DoctrineDbalAdapter::class);

// available in Symfony 5.1 and up to Symfony 5.4 (deprecated)
$this->injectAdapters($container, 'doctrine.orm.listeners.pdo_cache_adapter_doctrine_schema_subscriber', PdoAdapter::class);
}
Expand Down
24 changes: 18 additions & 6 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaSubscriber;
use Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaListener;
use Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaListener;
use Symfony\Bridge\Doctrine\SchemaListener\MessengerTransportDoctrineSchemaListener;
use Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber;
use Symfony\Bridge\Doctrine\SchemaListener\PdoSessionHandlerSchemaSubscriber;
use Symfony\Bridge\Doctrine\SchemaListener\PdoSessionHandlerSchemaListener;
use Symfony\Bridge\Doctrine\SchemaListener\RememberMeTokenProviderDoctrineSchemaListener;
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
Expand Down Expand Up @@ -394,18 +397,24 @@ protected function ormLoad(array $config, ContainerBuilder $container)
$container->removeAlias('doctrine.orm.metadata.annotation_reader');
}

// available in Symfony 6.3
$container->removeDefinition('doctrine.orm.listeners.doctrine_dbal_cache_adapter_schema_' . (class_exists(DoctrineDbalCacheAdapterSchemaListener::class) ? 'subscriber' : 'listener'));

// available in Symfony 6.3
$container->removeDefinition('doctrine.orm.listeners.doctrine_token_provider_schema_' . (class_exists(RememberMeTokenProviderDoctrineSchemaListener::class) ? 'subscriber' : 'listener'));

// available in Symfony 5.1 and up to Symfony 5.4 (deprecated)
if (! class_exists(PdoCacheAdapterDoctrineSchemaSubscriber::class)) {
$container->removeDefinition('doctrine.orm.listeners.pdo_cache_adapter_doctrine_schema_subscriber');
}

if (! class_exists(PdoSessionHandlerSchemaSubscriber::class)) {
$container->removeDefinition('doctrine.orm.listeners.pdo_session_handler_schema_subscriber');
if (! class_exists(PdoSessionHandlerSchemaListener::class)) {
$container->removeDefinition('doctrine.orm.listeners.pdo_session_handler_schema_listener');
}

// available in Symfony 6.3 and higher
if (! class_exists(LockStoreSchemaSubscriber::class)) {
$container->removeDefinition('doctrine.orm.listeners.lock_store_schema_subscriber');
if (! class_exists(LockStoreSchemaListener::class)) {
$container->removeDefinition('doctrine.orm.listeners.lock_store_schema_listener');
}

if (! class_exists(UlidGenerator::class)) {
Expand Down Expand Up @@ -1063,6 +1072,9 @@ private function loadMessengerServices(ContainerBuilder $container): void
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('messenger.xml');

// available in Symfony 6.3
$container->removeDefinition('doctrine.orm.messenger.doctrine_schema_' . (class_exists(MessengerTransportDoctrineSchemaListener::class) ? 'subscriber' : 'listener'));

/**
* The Doctrine transport component (symfony/doctrine-messenger) is optional.
* Remove service definition, if it is not available
Expand Down
2 changes: 1 addition & 1 deletion DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function process(ContainerBuilder $container): void
return;
}

$container->removeDefinition('doctrine.orm.listeners.pdo_session_handler_schema_subscriber');
$container->removeDefinition('doctrine.orm.listeners.pdo_session_handler_schema_listener');
}
}, PassConfig::TYPE_BEFORE_OPTIMIZATION);

Expand Down
6 changes: 6 additions & 0 deletions Resources/config/messenger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@
</service>

<service id="doctrine.orm.messenger.doctrine_schema_subscriber" class="Symfony\Bridge\Doctrine\SchemaListener\MessengerTransportDoctrineSchemaSubscriber">
<deprecated package="doctrine/doctrine-bundle" version="2.9">The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.</deprecated>
<argument type="tagged" tag="messenger.receiver" />
<tag name="doctrine.event_subscriber" />
</service>
<service id="doctrine.orm.messenger.doctrine_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\MessengerTransportDoctrineSchemaListener">
<argument type="tagged" tag="messenger.receiver" />
<tag name="doctrine.event_listener" event="postGenerateSchema" />
<tag name="doctrine.event_listener" event="onSchemaCreateTable" />
</service>
</services>
</container>
18 changes: 14 additions & 4 deletions Resources/config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,34 @@
<!-- listeners -->
<service id="doctrine.orm.listeners.resolve_target_entity" class="%doctrine.orm.listeners.resolve_target_entity.class%" public="false" />
<service id="doctrine.orm.listeners.doctrine_dbal_cache_adapter_schema_subscriber" class="Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaSubscriber">
<deprecated package="doctrine/doctrine-bundle" version="2.9">The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.</deprecated>
<argument type="collection" /> <!-- DoctrineDbalAdapter instances -->
<tag name="doctrine.event_subscriber" />
</service>
<service id="doctrine.orm.listeners.doctrine_dbal_cache_adapter_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaListener">
<argument type="collection" /> <!-- DoctrineDbalAdapter instances -->
<tag name="doctrine.event_listener" event="postGenerateSchema" />
</service>
<service id="doctrine.orm.listeners.pdo_cache_adapter_doctrine_schema_subscriber" class="Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber">
<argument type="collection" /> <!-- PdoAdapter instances -->
<tag name="doctrine.event_subscriber" />
</service>
<service id="doctrine.orm.listeners.doctrine_token_provider_schema_subscriber" class="Symfony\Bridge\Doctrine\SchemaListener\RememberMeTokenProviderDoctrineSchemaSubscriber">
<deprecated package="doctrine/doctrine-bundle" version="2.9">The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.</deprecated>
<argument type="tagged" tag="security.remember_me_handler" />
<tag name="doctrine.event_subscriber" />
</service>
<service id="doctrine.orm.listeners.pdo_session_handler_schema_subscriber" class="Symfony\Bridge\Doctrine\SchemaListener\PdoSessionHandlerSchemaSubscriber">
<service id="doctrine.orm.listeners.doctrine_token_provider_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\RememberMeTokenProviderDoctrineSchemaListener">
<argument type="tagged" tag="security.remember_me_handler" />
<tag name="doctrine.event_listener" event="postGenerateSchema" />
</service>
<service id="doctrine.orm.listeners.pdo_session_handler_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\PdoSessionHandlerSchemaListener">
<argument type="service" id="session.handler" />
<tag name="doctrine.event_subscriber" />
<tag name="doctrine.event_listener" event="postGenerateSchema" />
</service>
<service id="doctrine.orm.listeners.lock_store_schema_subscriber" class="Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaSubscriber">
<service id="doctrine.orm.listeners.lock_store_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaListener">
<argument type="tagged" tag="lock.store" />
<tag name="doctrine.event_subscriber" />
<tag name="doctrine.event_listener" event="postGenerateSchema" />
</service>

<!-- naming strategy -->
Expand Down
11 changes: 8 additions & 3 deletions Tests/CacheSchemaSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\ORM\EntityManagerInterface;
use Generator;
use Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaSubscriber;
use Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaListener;
use Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\DependencyInjection\Alias;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function testSchemaSubscriberWiring(string $adapterId, string $subscriber
], $container);

$container->setAlias('test_subscriber_alias', new Alias($subscriberId, true));
// prevent my_cache_apapter from inlining
// prevent my_cache_adapter from inlining
$container->register('uses_my_cache_adapter', 'stdClass')
->addArgument(new Reference('my_cache_adapter'))
->setPublic(true);
Expand All @@ -90,7 +90,12 @@ public function testSchemaSubscriberWiring(string $adapterId, string $subscriber

public function getSchemaSubscribers(): Generator
{
yield ['cache.adapter.doctrine_dbal', 'doctrine.orm.listeners.doctrine_dbal_cache_adapter_schema_subscriber', DoctrineDbalCacheAdapterSchemaSubscriber::class];
/**
* available in Symfony 6.3
*
* @psalm-suppress UndefinedClass
*/
yield ['cache.adapter.doctrine_dbal', 'doctrine.orm.listeners.doctrine_dbal_cache_adapter_schema_listener', DoctrineDbalCacheAdapterSchemaListener::class];

/**
* available in Symfony 5.1 and up to Symfony 5.4 (deprecated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaSubscriber;
use Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaListener;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -14,7 +14,7 @@
use function interface_exists;
use function sys_get_temp_dir;

class LockStoreSchemaSubscriberTest extends TestCase
class LockStoreSchemaListenerTest extends TestCase
{
/**
* @param array<string, mixed> $config
Expand All @@ -24,7 +24,7 @@ class LockStoreSchemaSubscriberTest extends TestCase
*/
public function testLockStoreSchemaSubscriberWiring(array $config, int $expectedCount): void
{
if (! class_exists(LockStoreSchemaSubscriber::class)) {
if (! class_exists(LockStoreSchemaListener::class)) {
self::markTestSkipped('symfony/doctrine-bridge version not supported');
}

Expand Down Expand Up @@ -59,7 +59,7 @@ public function testLockStoreSchemaSubscriberWiring(array $config, int $expected

$container->setAlias(
'test_subscriber_lock_alias',
new Alias('doctrine.orm.listeners.lock_store_schema_subscriber', true),
new Alias('doctrine.orm.listeners.lock_store_schema_listener', true),
);
$container->compile();

Expand Down

0 comments on commit dcf256e

Please sign in to comment.