This bundle provides a way to dispatch events when entities are created, updated or deleted using Doctrine ORM events and Symfony EventDispatcher.
Symfony ^6 || ^7
PHP >=8.2
Use Composer to install the package:
composer require atournayre/entities-events-bundle
// config/bundles.php
return [
// ...
Atournayre\Bundle\EntitiesEventsBundle\AtournayreEntitiesEventsBundle::class => ['all' => true],
// ...
]
php bin/console atournayre:entities-events:generate-listeners
use Atournayre\Bundle\EntitiesEventsBundle\Collection\EventCollection;
use Atournayre\Bundle\EntitiesEventsBundle\Contracts\HasEventsInterface;
// Implements HasEventsInterface
class YourEntity implements HasEventsInterface
{
// Use EventsTrait to implement HasEventsInterface
use EventsTrait;
public function __construct()
{
// Initialize the collection of events
$this->eventCollection = new EventCollection();
}
public function doSomething(): void
{
// Do something here
// Then dispatch an event
$this->addEvent(new YourEvent($this));
}
}
use Symfony\Contracts\EventDispatcher\Event;
class YourEvent extends Event
{
public function __construct(
public readonly YourEntity $entity
) {}
}
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener]
class YourEventListener
{
public function __invoke(YourEvent $event): void
{
// Do something here
}
}
That's all, the bundle will do the rest.
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
All contents of this package are licensed under the MIT license.