Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAT-3057 Make bundle compatible with Symfony 5 and 6 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BazingaPropelEventDispatcherBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class BazingaPropelEventDispatcherBundle extends Bundle
{
/**
* {@inheritdoc}
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
Expand Down
47 changes: 13 additions & 34 deletions EventDispatcher/LazyEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,13 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @author William Durand <[email protected]>
*/
class LazyEventDispatcher implements EventDispatcherInterface
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;

/**
* @var string
*/
private $serviceId;

/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
private $eventDispatcher = null;
private ContainerInterface $container;
private string $serviceId;
private ?EventDispatcherInterface $eventDispatcher = null;

/**
* @param ContainerInterface $container
* @param string $serviceId
*/
public function __construct($container, $serviceId)
public function __construct(ContainerInterface $container, string $serviceId)
{
$this->container = $container;
$this->serviceId = $serviceId;
Expand All @@ -39,71 +21,68 @@ public function __construct($container, $serviceId)
/**
* {@inheritdoc}
*/
public function dispatch($event, $eventName = null)
public function dispatch($event, $eventName = null): object
{
return $this->getEventDispatcher()->dispatch($event, $eventName);
}

/**
* {@inheritdoc}
*/
public function addListener($eventName, $listener, $priority = 0)
public function addListener($eventName, $listener, $priority = 0): void
{
$this->getEventDispatcher()->addListener($eventName, $listener, $priority);
}

/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
public function addSubscriber(EventSubscriberInterface $subscriber): void
{
$this->getEventDispatcher()->addSubscriber($subscriber);
}

/**
* {@inheritdoc}
*/
public function removeListener($eventName, $listener)
public function removeListener($eventName, $listener): void
{
$this->getEventDispatcher()->removeListener($eventName, $listener);
}

/**
* {@inheritdoc}
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
public function removeSubscriber(EventSubscriberInterface $subscriber): void
{
$this->getEventDispatcher()->removeSubscriber($subscriber);
}

/**
* {@inheritdoc}
*/
public function getListeners($eventName = null)
public function getListeners($eventName = null): array
{
return $this->getEventDispatcher()->getListeners($eventName);
}

/**
* {@inheritdoc}
*/
public function getListenerPriority($eventName, $listener)
public function getListenerPriority($eventName, $listener): ?int
{
return $this->getEventDispatcher()->getListenerPriority($eventName, $listener);
}

/**
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
public function hasListeners($eventName = null): bool
{
return $this->getEventDispatcher()->hasListeners($eventName);
}

/**
* @return EventDispatcherInterface
*/
protected function getEventDispatcher()
protected function getEventDispatcher(): EventDispatcherInterface
{
if (null === $this->eventDispatcher) {
$this->eventDispatcher = $this->container->get($this->serviceId);
Expand Down
24 changes: 10 additions & 14 deletions Injector/DispatcherInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

class DispatcherInjector
{
const MODEL_INTERFACE = 'EventDispatcherAwareModelInterface';
public const MODEL_INTERFACE = 'EventDispatcherAwareModelInterface';

private $classes;

private $container;

private $logger;
private ContainerInterface $container;
private array $classes;
private ?LoggerInterface $logger;

public function __construct(ContainerInterface $container, array $classes, LoggerInterface $logger = null)
{
Expand All @@ -26,10 +24,10 @@ public function __construct(ContainerInterface $container, array $classes, Logge
/**
* Initializes the EventDispatcher-aware models.
*
* This methods has to accept unknown classes as it is triggered during
* This method has to accept unknown classes as it is triggered during
* the boot and so will be called before running the propel:build command.
*/
public function initializeModels()
public function initializeModels(): void
{
foreach ($this->classes as $id => $class) {
$baseClass = sprintf('%s\\Base\\%s',
Expand All @@ -39,15 +37,15 @@ public function initializeModels()

try {
$ref = new \ReflectionClass($baseClass);
} catch (\ReflectionException $e) {
} catch (\ReflectionException) {
$this->log(sprintf('The class "%s" does not exist.', $baseClass));

continue;
}

try {
$ref = new \ReflectionClass($class);
} catch (\ReflectionException $e) {
} catch (\ReflectionException) {
$this->log(sprintf(
'The class "%s" does not exist. Either your model is not generated yet or you have an error in your listener configuration.',
$class
Expand All @@ -70,10 +68,8 @@ public function initializeModels()
}
}

private function log($message)
private function log($message): void
{
if (null !== $this->logger) {
$this->logger->warning($message);
}
$this->logger?->warning($message);
}
}
66 changes: 47 additions & 19 deletions Tests/BazingaPropelEventDispatcherBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,66 @@

use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject;
use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject3;
use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventSubscriber;
use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject2;
use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventListener;
use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\TestKernel;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Kernel;

class BazingaPropelEventDispatcherBundleTest extends WebTestCase
{
public function testGetListener()
protected function setUp(): void
{
$listener = $this->getContainer()->get('listener.my_event_listener');
parent::setUp();
$this->deleteTmpDir();
self::bootKernel();
}

protected static function getKernelClass(): string
{
return TestKernel::class;
}

private function deleteTmpDir(): void
{
$dir = sys_get_temp_dir().'/'.Kernel::VERSION;
if (file_exists($dir)) {
$fs = new Filesystem();
$fs->remove($dir);
}
}

public function testGetListener(): void
{
$listener = self::getContainer()->get('listener.my_event_listener');

$this->assertNotNull($listener);
$this->assertInstanceOf('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventListener', $listener);
$this->assertInstanceOf(MyEventListener::class, $listener);
}

public function testGetListenerWithNonExistentClass()
public function testGetListenerWithNonExistentClass(): void
{
$this->assertFalse(class_exists('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject2', false));
$this->assertFalse(class_exists(MyObject2::class, false));

$listener = $this->getContainer()->get('listener.my_event_listener_2');
$listener = self::getContainer()->get('listener.my_event_listener_2');

$this->assertNotNull($listener);
$this->assertInstanceOf('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventListener', $listener);
$this->assertInstanceOf(MyEventListener::class, $listener);
}

public function testGetListenerWithNonExistentParentClass()
public function testGetListenerWithNonExistentParentClass(): void
{
$this->assertFalse(class_exists('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject2', false));
$this->assertFalse(class_exists(MyObject2::class, false));

$listener = $this->getContainer()->get('listener.my_event_listener_3');
self::getContainer()->get('listener.my_event_listener_3');
}

public function testFireEvent()
public function testFireEvent(): void
{
$object = new MyObject();
$listener = $this->getContainer()->get('listener.my_event_listener');
$listener = self::getContainer()->get('listener.my_event_listener');

$this->assertCount(0, $listener->getEvents());

Expand All @@ -47,9 +75,9 @@ public function testFireEvent()
$this->assertSame($object, $subject);
}

public function testFireEventWithEarlyBoot()
public function testFireEventWithEarlyBoot(): void
{
$listener = $this->getContainer()->get('listener.my_event_listener_4');
$listener = self::getContainer()->get('listener.my_event_listener_4');
$object = new MyObject3();

$this->assertCount(0, $listener->getEvents());
Expand All @@ -62,18 +90,18 @@ public function testFireEventWithEarlyBoot()
$this->assertSame($object, $subject);
}

public function testSubscriber()
public function testSubscriber(): void
{
$subscriber = $this->getContainer()->get('subscriber.my_subscriber_1');
$subscriber = self::getContainer()->get('subscriber.my_subscriber_1');

$this->assertNotNull($subscriber);
$this->assertInstanceOf('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventSubscriber', $subscriber);
$this->assertInstanceOf(MyEventSubscriber::class, $subscriber);
}

public function testFireEventWithSubscriber()
public function testFireEventWithSubscriber(): void
{
$object = new MyObject3();
$subscriber = $this->getContainer()->get('subscriber.my_subscriber_1');
$subscriber = self::getContainer()->get('subscriber.my_subscriber_1');

$this->assertCount(0, $subscriber->getEvents());

Expand Down
8 changes: 4 additions & 4 deletions Tests/Fixtures/EventListener/MyEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;

class MyEventListener
{
private $events = array();
private array $events = [];

public function preSave(Event $event)
public function preSave(Event $event): void
{
$this->events[] = $event;
}

public function getEvents()
public function getEvents(): array
{
return $this->events;
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/Fixtures/EventListener/MyEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class MyEventSubscriber implements EventSubscriberInterface
{
private $events = array();
private array $events = [];

public function preInsert(Event $event)
public function preInsert(GenericEvent $event): void
{
$subject = $event->getSubject();
$subject->source = 'pre_insert';

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

public function getEvents()
public function getEvents(): array
{
return $this->events;
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return array(
'propel.pre_insert' => 'preInsert',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\om;
namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\Base;

abstract class BaseMyObject
abstract class MyObject
{
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\om;
namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\Base;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

abstract class BaseMyObject3 implements \EventDispatcherAwareModelInterface
abstract class MyObject3 implements \EventDispatcherAwareModelInterface
{
private static $eventDispatcher;

Expand Down
5 changes: 3 additions & 2 deletions Tests/Fixtures/Model/MyObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model;

use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\om\BaseMyObject;
use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\Base\MyObject as BaseMyObject;
use EventDispatcherAwareModelInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class MyObject extends BaseMyObject implements \EventDispatcherAwareModelInterface
class MyObject extends BaseMyObject implements EventDispatcherAwareModelInterface
{
private static $eventDispatcher;

Expand Down
Loading