Skip to content

Commit

Permalink
Merge pull request #76 from biig-io/feature/sf5-doctrine27-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Veber authored Aug 31, 2020
2 parents 06149a1 + 7978c74 commit abd3ff9
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 53 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions Tests/DataCollector/DomainEventDataCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Biig\Component\Domain\Tests\DataCollector;


use Biig\Component\Domain\DataCollector\DomainEventDataCollector;
use Biig\Component\Domain\Debug\TraceableDomainEventDispatcher;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

class DomainEventDataCollectorTest extends TestCase
{
public function testItCollect()
{
$dispatcher = $this->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());
}
}
7 changes: 5 additions & 2 deletions Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
45 changes: 32 additions & 13 deletions src/DataCollector/DomainEventDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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.
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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' => [],
];
}
}
}
124 changes: 87 additions & 37 deletions src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit abd3ff9

Please sign in to comment.