From 9dab3e64ddf6d50f5e3e8cc40f45de963306c04d Mon Sep 17 00:00:00 2001 From: "Nek (Maxime Veber)" Date: Tue, 1 Sep 2020 00:41:42 +0200 Subject: [PATCH 1/2] feat(compat): compatibility with sf 4.3+, 5.0+ And Doctrine 2.7+ ! --- .../DomainEventDataCollectorTest.php | 24 ++++ .../DoctrineConfig/ClassMetadataTest.php | 7 +- .../DomainEventDataCollector.php | 45 +++++-- .../DoctrineConfig/ClassMetadataFactory.php | 124 ++++++++++++------ 4 files changed, 148 insertions(+), 52 deletions(-) create mode 100644 Tests/DataCollector/DomainEventDataCollectorTest.php diff --git a/Tests/DataCollector/DomainEventDataCollectorTest.php b/Tests/DataCollector/DomainEventDataCollectorTest.php new file mode 100644 index 0000000..b5c70cf --- /dev/null +++ b/Tests/DataCollector/DomainEventDataCollectorTest.php @@ -0,0 +1,24 @@ +prophesize(TraceableDomainEventDispatcher::class); + $stack = $this->prophesize(RequestStack::class); + $stack->getMasterRequest()->shouldBeCalled()->willReturn($request = $this->prophesize(Request::class)->reveal()); + + $collector = new DomainEventDataCollector($dispatcher->reveal(), $stack->reveal()); + $collector->collect($request, $this->prophesize(Response::class)->reveal()); + } +} diff --git a/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php b/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php index 26d48a2..9b68dde 100644 --- a/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php +++ b/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php @@ -7,7 +7,6 @@ use Biig\Component\Domain\Event\DomainEventDispatcher; use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadata; use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\Instantiator; -use Doctrine\Common\Persistence\Mapping\ReflectionService; use PHPUnit\Framework\TestCase; class ClassMetadataTest extends TestCase @@ -40,7 +39,11 @@ public function testItsWakable() $this->assertInstanceOf(ClassMetadata::class, $metadata); - $refSer = $this->prophesize(ReflectionService::class); + if (interface_exists(\Doctrine\Persistence\Mapping\ReflectionService::class)) { + $refSer = $this->prophesize(\Doctrine\Persistence\Mapping\ReflectionService::class); + } else { + $refSer = $this->prophesize(\Doctrine\Common\Persistence\Mapping\ReflectionService::class); + } $metadata->wakeupReflectionWithInstantiator($refSer->reveal(), new Instantiator(new DomainEventDispatcher())); $model = $metadata->newInstance(); diff --git a/src/DataCollector/DomainEventDataCollector.php b/src/DataCollector/DomainEventDataCollector.php index 762452c..96536f0 100644 --- a/src/DataCollector/DomainEventDataCollector.php +++ b/src/DataCollector/DomainEventDataCollector.php @@ -10,7 +10,7 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollector; use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; -class DomainEventDataCollector extends DataCollector implements LateDataCollectorInterface +abstract class AbstractDomainEventDataCollector extends DataCollector implements LateDataCollectorInterface { public const NAME = 'biig_domain.domain_event_data_collector'; @@ -22,12 +22,12 @@ class DomainEventDataCollector extends DataCollector implements LateDataCollecto /** * @var RequestStack */ - private $requestStack; + protected $requestStack; /** * @var Request|null */ - private $currentRequest; + protected $currentRequest; /** * DomainEventDataCollector constructor. @@ -43,16 +43,6 @@ public function getName() return self::NAME; } - public function collect(Request $request, Response $response, \Exception $exception = null) - { - $this->currentRequest = $this->requestStack->getMasterRequest() !== $request ? $request : null; - $this->data = [ - 'called_listeners' => [], - 'called_delayed_listeners' => [], - 'not_called_listeners' => [], - ]; - } - public function reset() { $this->data = []; @@ -154,3 +144,32 @@ public function getNotCalledListeners() return $this->data['not_called_listeners'] ?? []; } } + +if (method_exists(TraceableEventDispatcher::class, 'preDispatch')) { + class DomainEventDataCollector extends AbstractDomainEventDataCollector + { + // Compatibility layer with Sf 4.3 & 4.4 + public function collect(Request $request, Response $response /*, \Throwable $exception = null */) + { + $this->currentRequest = $this->requestStack->getMasterRequest() !== $request ? $request : null; + $this->data = [ + 'called_listeners' => [], + 'called_delayed_listeners' => [], + 'not_called_listeners' => [], + ]; + } + } +} else { + class DomainEventDataCollector extends AbstractDomainEventDataCollector + { + public function collect(Request $request, Response $response, \Throwable $exception = null) + { + $this->currentRequest = $this->requestStack->getMasterRequest() !== $request ? $request : null; + $this->data = [ + 'called_listeners' => [], + 'called_delayed_listeners' => [], + 'not_called_listeners' => [], + ]; + } + } +} diff --git a/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php b/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php index 077c3f8..c91f511 100644 --- a/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php +++ b/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php @@ -3,53 +3,103 @@ namespace Biig\Component\Domain\Model\Instantiator\DoctrineConfig; use Biig\Component\Domain\Event\DomainEventDispatcherInterface; -use Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface; -use Doctrine\Common\Persistence\Mapping\ReflectionService; +use Doctrine\Common\Persistence\Mapping\ClassMetadata as OldClassMetadataInterface; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadataFactory as BaseClassMetadataFactory; +use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface; +use Doctrine\Persistence\Mapping\ReflectionService; -final class ClassMetadataFactory extends BaseClassMetadataFactory -{ - /** - * @var DomainEventDispatcherInterface - */ - private $dispatcher; - - /** - * @var EntityManagerInterface - */ - private $entityManager; - - /** - * {@inheritdoc} - */ - public function newClassMetadataInstance($className) +if (interface_exists(\Doctrine\Persistence\Mapping\ClassMetadata::class)) { + final class ClassMetadataFactory extends BaseClassMetadataFactory { - return new ClassMetadata($className, new Instantiator($this->dispatcher), $this->entityManager->getConfiguration()->getNamingStrategy()); - } + /** + * @var DomainEventDispatcherInterface + */ + private $dispatcher; - public function setDispatcher(DomainEventDispatcherInterface $dispatcher) - { - $this->dispatcher = $dispatcher; - } + /** + * @var EntityManagerInterface + */ + private $entityManager; - /** - * {@inheritdoc} - */ - protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) - { - if ($class instanceof ClassMetadata) { - $class->wakeupReflectionWithInstantiator($reflService, new Instantiator($this->dispatcher)); + /** + * {@inheritdoc} + */ + public function newClassMetadataInstance($className) + { + return new ClassMetadata($className, new Instantiator($this->dispatcher), $this->entityManager->getConfiguration()->getNamingStrategy()); + } - return; + public function setDispatcher(DomainEventDispatcherInterface $dispatcher) + { + $this->dispatcher = $dispatcher; } - $class->wakeupReflection($reflService); - } + /** + * {@inheritdoc} + */ + protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) + { + if ($class instanceof ClassMetadata) { + $class->wakeupReflectionWithInstantiator($reflService, new Instantiator($this->dispatcher)); - public function setEntityManager(EntityManagerInterface $em) + return; + } + + $class->wakeupReflection($reflService); + } + + public function setEntityManager(EntityManagerInterface $em) + { + $this->entityManager = $em; + parent::setEntityManager($em); + } + } +} else { + // Compatibility layer for Doctrine ORM <= 2.6 + final class ClassMetadataFactory extends BaseClassMetadataFactory { - $this->entityManager = $em; - parent::setEntityManager($em); + /** + * @var DomainEventDispatcherInterface + */ + private $dispatcher; + + /** + * @var EntityManagerInterface + */ + private $entityManager; + + /** + * {@inheritdoc} + */ + public function newClassMetadataInstance($className) + { + return new ClassMetadata($className, new Instantiator($this->dispatcher), $this->entityManager->getConfiguration()->getNamingStrategy()); + } + + public function setDispatcher(DomainEventDispatcherInterface $dispatcher) + { + $this->dispatcher = $dispatcher; + } + + /** + * {@inheritdoc} + */ + protected function wakeupReflection(OldClassMetadataInterface $class, ReflectionService $reflService) + { + if ($class instanceof ClassMetadata) { + $class->wakeupReflectionWithInstantiator($reflService, new Instantiator($this->dispatcher)); + + return; + } + + $class->wakeupReflection($reflService); + } + + public function setEntityManager(EntityManagerInterface $em) + { + $this->entityManager = $em; + parent::setEntityManager($em); + } } } From 7978c746f137d622b8e7da493b72c67f810b2863 Mon Sep 17 00:00:00 2001 From: "Nek (Maxime Veber)" Date: Tue, 1 Sep 2020 00:42:45 +0200 Subject: [PATCH 2/2] Update release 2.1.4 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 763fdd0..4dfbaff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -## [2.1.4] - 2020-08-20 +## [2.1.4] - 2020-09-01 ### Fixed - Compatibilty with Symfony 4.3 (and 5.x), basically make the library usable again. +- Compatibility with Doctrine 2.7 ## [2.1.2] - 2020-03-09 ### Fixed