Skip to content

Commit

Permalink
Rework Doctrine subscriber registration to fix deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-93 authored and bobvandevijver committed May 7, 2024
1 parent 8931927 commit 07ebbf7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
8 changes: 6 additions & 2 deletions DependencyInjection/BobvEntityHistoryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Bobv\EntityHistoryBundle\DependencyInjection;

use Doctrine\ORM\Events;
use Doctrine\ORM\Tools\ToolEvents;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
Expand Down Expand Up @@ -41,9 +43,11 @@ public function load(array $configs, ContainerBuilder $container): void {
// Create the service tags
foreach ($config['connections'] as $connection) {
$container->findDefinition('bobv.entityhistory.create_schema_subscriber')
->addTag('doctrine.event_subscriber', array('connection' => $connection));
->addTag('doctrine.event_listener', ['event' => ToolEvents::postGenerateSchemaTable, 'connection' => $connection]);
$container->findDefinition('bobv.entityhistory.log_history_subscriber')
->addTag('doctrine.event_subscriber', array('connection' => $connection));
->addTag('doctrine.event_listener', ['event' => Events::onFlush, 'connection' => $connection])
->addTag('doctrine.event_listener', ['event' => Events::postPersist, 'connection' => $connection])
->addTag('doctrine.event_listener', ['event' => Events::postUpdate, 'connection' => $connection]);
}

}
Expand Down
11 changes: 1 addition & 10 deletions EventSubscriber/CreateSchemaSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace Bobv\EntityHistoryBundle\EventSubscriber;

use Bobv\EntityHistoryBundle\Configuration\HistoryConfiguration;
use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Schema\Column;
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
use Doctrine\ORM\Tools\ToolEvents;

/**
* Based on the work of
Expand All @@ -16,19 +14,12 @@
*
* @author BobV
*/
class CreateSchemaSubscriber implements EventSubscriber
class CreateSchemaSubscriber
{
public function __construct(private readonly HistoryConfiguration $configuration)
{
}

public function getSubscribedEvents(): array
{
return [
ToolEvents::postGenerateSchemaTable
];
}

public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs): void
{
$cm = $eventArgs->getClassMetadata();
Expand Down
8 changes: 1 addition & 7 deletions EventSubscriber/LogHistorySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
namespace Bobv\EntityHistoryBundle\EventSubscriber;

use Bobv\EntityHistoryBundle\Configuration\HistoryConfiguration;
use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\UnitOfWork;

Expand All @@ -23,7 +21,7 @@
*
* @author BobV
*/
class LogHistorySubscriber implements EventSubscriber
class LogHistorySubscriber
{
/**
* @var Connection
Expand All @@ -49,10 +47,6 @@ class LogHistorySubscriber implements EventSubscriber
public function __construct(private readonly HistoryConfiguration $config) {
}

public function getSubscribedEvents(): array {
return array(Events::onFlush, Events::postPersist, Events::postUpdate);
}

public function onFlush(OnFlushEventArgs $eventArgs): void {
$this->em = $eventArgs->getObjectManager();
$this->conn = $this->em->getConnection();
Expand Down

0 comments on commit 07ebbf7

Please sign in to comment.