Skip to content

Commit

Permalink
Merge pull request #75 from biig-io/feature/fix-sf5-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Veber authored Aug 20, 2020
2 parents 9da4dd8 + 585d70d commit d842222
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 130 deletions.
8 changes: 4 additions & 4 deletions Tests/Symfony/Serializer/DomainDenormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public function testItDoesntSupportsDenormalization()
$this->decorated->supportsDenormalization(Argument::cetera())->willReturn(false);
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher);

$this->assertFalse($denormalizer->supportsDenormalization([], \stdClass::class, []));
$this->assertFalse($denormalizer->supportsDenormalization([], \FakeModel::class, []));
$this->assertFalse($denormalizer->supportsDenormalization([], \stdClass::class, ''));
$this->assertFalse($denormalizer->supportsDenormalization([], \FakeModel::class, ''));
}

public function testItSupportsDenormalization()
{
$this->decorated->supportsDenormalization(Argument::cetera())->willReturn(true);
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher);

$this->assertTrue($denormalizer->supportsDenormalization([], \stdClass::class, []));
$this->assertTrue($denormalizer->supportsDenormalization([], \FakeModel::class, []));
$this->assertTrue($denormalizer->supportsDenormalization([], \stdClass::class, ''));
$this->assertTrue($denormalizer->supportsDenormalization([], \FakeModel::class, ''));
}

public function testDenormalize()
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"phpunit/phpunit": "^6.4",
"doctrine/orm": "^2.6.3",
"friendsofphp/php-cs-fixer": "^2.14",
"symfony/symfony": "^4.3"
"symfony/symfony": "^4.3 || ^5.0"
},
"conflict": {
"doctrine/orm": "<2.6.3"
Expand Down
12 changes: 0 additions & 12 deletions src/DataCollector/DomainEventDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Contracts\Service\ResetInterface;

class DomainEventDataCollector extends DataCollector implements LateDataCollectorInterface
{
Expand All @@ -32,9 +31,6 @@ class DomainEventDataCollector extends DataCollector implements LateDataCollecto

/**
* DomainEventDataCollector constructor.
*
* @param TraceableDomainEventDispatcher $dispatcher
* @param RequestStack $requestStack
*/
public function __construct(TraceableDomainEventDispatcher $dispatcher, RequestStack $requestStack)
{
Expand Down Expand Up @@ -89,12 +85,6 @@ public function lateCollect()
$this->data = $this->cloneVar($this->data);
}

/**
* @param array $listeners
* @param string $firedEvent
*
* @return array
*/
private function extractListeners(array $listeners, string $firedEvent): array
{
$filteredListeners = [];
Expand Down Expand Up @@ -145,8 +135,6 @@ public function getCalledDelayedListeners()
/**
* Sets the not called listeners.
*
* @param array $listeners
*
* @see TraceableEventDispatcher
*/
public function setNotCalledListeners(array $listeners)
Expand Down
92 changes: 63 additions & 29 deletions src/Debug/TraceableDomainEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface;
use Psr\Log\NullLogger;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Stopwatch\Stopwatch;

class TraceableDomainEventDispatcher extends TraceableEventDispatcher implements DomainEventDispatcherInterface
abstract class AbstractTraceableDomainEventDispatcher extends TraceableEventDispatcher implements DomainEventDispatcherInterface
{
/**
* @var array
*/
private $eventsFired;
protected $eventsFired;

/**
* @var DomainEventDispatcherInterface
*/
private $decorated;
protected $decorated;

/**
* @var DelayedListener[]
*/
private $delayedListenersCalled;
protected $delayedListenersCalled;

/**
* TraceableDomainEventDispatcher constructor.
*
* @param DomainEventDispatcherInterface $dispatcher
*/
public function __construct(DomainEventDispatcherInterface $dispatcher)
{
Expand Down Expand Up @@ -69,29 +69,6 @@ public function persistModel(ModelInterface $model)
return $this->decorated->persistModel($model);
}

/**
* Dispatches an event to all registered listeners.
*
* @param object $event The event to pass to the event handlers/listeners
* @param string|null $eventName The name of the event to dispatch. If not supplied,
* the class of $event should be used instead.
*
*
* @return object The passed $event MUST be returned
*/
public function dispatch($event, string $eventName = null)
{
if ($eventName === null) {
$eventName = get_class($event);
}
$this->eventsFired[] = $eventName;

return parent::dispatch($event, $eventName);
}

/**
* @return array
*/
public function getEventsFired(): array
{
return $this->eventsFired;
Expand All @@ -113,3 +90,60 @@ public function getDelayedListenersCalled(): array
return $this->delayedListenersCalled;
}
}
if (method_exists(TraceableEventDispatcher::class, 'preDispatch')) {
// BC Layer for Sf 4.3 & 4.4
class TraceableDomainEventDispatcher extends AbstractTraceableDomainEventDispatcher
{
/**
* Dispatches an event to all registered listeners.
*
* @param object $event The event to pass to the event handlers/listeners
* @param string|null $eventName The name of the event to dispatch. If not supplied,
* the class of $event should be used instead.
*
* @return object The passed $event MUST be returned
*/
public function dispatch($event /* , string $eventName = null */) // Compatibility layer with Sf 4.3 & 4.4
{
$eventName = 1 < \func_num_args() ? func_get_arg(1) : null;

if (\is_object($event)) {
$eventName = $eventName ?? \get_class($event);
} else {
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
$swap = $event;
$event = $eventName ?? new Event();
$eventName = $swap;

if (!$event instanceof Event) {
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of "%s", "%s" given.', EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
}
}

$eventName = $eventName ?? get_class($event);
$this->eventsFired[] = $eventName;

return parent::dispatch($event, $eventName);
}
}
} else {
class TraceableDomainEventDispatcher extends AbstractTraceableDomainEventDispatcher
{
/**
* Dispatches an event to all registered listeners.
*
* @param object $event The event to pass to the event handlers/listeners
* @param string|null $eventName The name of the event to dispatch. If not supplied,
* the class of $event should be used instead.
*
* @return object The passed $event MUST be returned
*/
public function dispatch(object $event, string $eventName = null): object
{
$eventName = $eventName ?? get_class($event);
$this->eventsFired[] = $eventName;

return parent::dispatch($event, $eventName);
}
}
}
4 changes: 1 addition & 3 deletions src/Debug/WrappedDelayedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ class WrappedDelayedListener

/**
* Does ClassStub class exists ?
*
* @var bool|null
*/
private static $hasClassStub;

/**
* @param DelayedListener $listener
*/
public function __construct(DelayedListener $listener)
{
$this->listener = $this->getListener($listener);
Expand Down
22 changes: 1 addition & 21 deletions src/Event/DelayedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ class DelayedListener

/**
* DelayedEvent constructor.
*
* @param string $eventName
* @param callable $listener
*/
public function __construct(string $eventName, callable $listener)
{
Expand All @@ -36,8 +33,6 @@ public function __construct(string $eventName, callable $listener)
}

/**
* @param DomainEvent $event
*
* @throws InvalidDomainEvent
*/
public function occur(DomainEvent $event)
Expand All @@ -46,21 +41,14 @@ public function occur(DomainEvent $event)
$subject = $event->getSubject();

if (!is_object($subject) || !$subject instanceof ModelInterface) {
throw new InvalidDomainEvent(
sprintf(
'The event "%s" is invalid because no domain model subject is specified while the event must be dispatched after persist.',
get_class($event)
)
);
throw new InvalidDomainEvent(sprintf('The event "%s" is invalid because no domain model subject is specified while the event must be dispatched after persist.', get_class($event)));
}

$this->eventStack[] = $event;
}

/**
* Execute the listener on the events that already occurred.
*
* @param ModelInterface $model
*/
public function process(ModelInterface $model)
{
Expand All @@ -81,19 +69,11 @@ public function process(ModelInterface $model)
}
}

/**
* @return string
*/
public function getEventName(): string
{
return $this->eventName;
}

/**
* @param ModelInterface $model
*
* @return bool
*/
public function shouldOccur(ModelInterface $model): bool
{
if (empty($this->eventStack)) {
Expand Down
16 changes: 2 additions & 14 deletions src/Event/DomainEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public function __construct()
}

/**
* @param RuleInterface $rule
*
* @throws InvalidArgumentException
*/
public function addRule(RuleInterface $rule)
Expand All @@ -43,9 +41,6 @@ public function addRule(RuleInterface $rule)
}
}

/**
* @param DomainRuleInterface $rule
*/
public function addDomainRule(DomainRuleInterface $rule)
{
$events = $rule->on();
Expand All @@ -59,9 +54,6 @@ public function addDomainRule(DomainRuleInterface $rule)
}
}

/**
* @param PostPersistDomainRuleInterface $rule
*/
public function addPostPersistDomainRuleInterface(PostPersistDomainRuleInterface $rule)
{
$events = $rule->after();
Expand All @@ -80,14 +72,13 @@ public function addPostPersistDomainRuleInterface(PostPersistDomainRuleInterface
}

/**
* @param Event|null $event
* @param string|null $eventName
* @param Event|null $event
*
* @throws \Biig\Component\Domain\Exception\InvalidDomainEvent
*
* @return Event
*/
public function dispatch($event, string $eventName = null)
public function dispatch($event, string $eventName = null): object
{
$event = parent::dispatch($event, $eventName);

Expand All @@ -102,9 +93,6 @@ public function dispatch($event, string $eventName = null)
return $event;
}

/**
* @param ModelInterface $model
*/
public function persistModel(ModelInterface $model)
{
foreach ($this->delayedListeners as $listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public function process(ContainerBuilder $container)
}

/**
* @param string $id
* @param string $class
* @param array $attribute
* @param Definition $definition
*
* @throws InvalidArgumentException
*/
private function addListenerForEventsInDefinition(string $id, string $class, array $attribute, Definition $definition)
Expand All @@ -50,16 +45,11 @@ private function addListenerForEventsInDefinition(string $id, string $class, arr
$priority = $attribute['priority'] ?? 0;

if (!class_exists($class, false)) {
throw new InvalidArgumentException(
sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)
);
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
}

if (null === $method || null === $event) {
throw new InvalidArgumentException(sprintf(
'Impossible to register class "%s" as domain rule: the service configuration is wrong. You need to specify either method, event or none. See documentation for more information.',
$class
));
throw new InvalidArgumentException(sprintf('Impossible to register class "%s" as domain rule: the service configuration is wrong. You need to specify either method, event or none. See documentation for more information.', $class));
}

$definition->addMethodCall('addListener', [
Expand All @@ -72,8 +62,6 @@ private function addListenerForEventsInDefinition(string $id, string $class, arr
/**
* `!empty()` is not enough to check multidimensional arrays emptiness.
*
* @param array $attributes
*
* @return bool
*/
private function notEmpty(array $attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,14 @@ public function process(ContainerBuilder $container)
}

/**
* @param array $calls
*
* @throws InvalidConfigurationException
*/
private function verifyCalls(array $calls)
{
foreach ($calls as $call) {
if ('setClassMetadataFactoryName' === $call[0]) {
if (ClassMetadataFactory::class !== $call[1][0]) {
throw new InvalidConfigurationException(
'The option "override_doctrine_instantiator", so this bundles tried to change the'
. ' ClassMetadataFactory of doctrine by changing the DoctrineBundle configuration.'
. ' The final configuration of the doctrine bundle doesn\'t looks like the one expected:'
. ' Something probably altered the configuration. You may disable this feature by changing the default'
. ' configuration or find what came override this. (It may be your manual configuration)'
);
throw new InvalidConfigurationException('The option "override_doctrine_instantiator", so this bundles tried to change the' . ' ClassMetadataFactory of doctrine by changing the DoctrineBundle configuration.' . ' The final configuration of the doctrine bundle doesn\'t looks like the one expected:' . ' Something probably altered the configuration. You may disable this feature by changing the default' . ' configuration or find what came override this. (It may be your manual configuration)');
}
}
}
Expand Down
Loading

0 comments on commit d842222

Please sign in to comment.