atournayre / entities-events-bundle
This bundle provides a way to dispatch events when entities are created, updated or deleted using Doctrine ORM events and Symfony EventDispatcher.
Installs: 10
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.2
- doctrine/doctrine-bundle: ^2.10
- doctrine/orm: ^2.16
- symfony/config: ^6 || ^7
- symfony/dependency-injection: ^6 || ^7
- symfony/http-kernel: ^6 || ^7
- webmozart/assert: ^1.11
Requires (Dev)
- symfony/console: ^6.3
This package is auto-updated.
Last update: 2024-05-26 21:00:52 UTC
README
This bundle provides a way to dispatch events when entities are created, updated or deleted using Doctrine ORM events and Symfony EventDispatcher.
Requirements
Symfony ^6 || ^7
PHP >=8.2
Install
Use Composer to install the package:
Composer
composer require atournayre/entities-events-bundle
Register bundle
// config/bundles.php return [ // ... Atournayre\Bundle\EntitiesEventsBundle\AtournayreEntitiesEventsBundle::class => ['all' => true], // ... ]
Install listeners
php bin/console atournayre:entities-events:generate-listeners
Usage example
Update your entity
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)); } }
Create an event
use Symfony\Contracts\EventDispatcher\Event; class YourEvent extends Event { public function __construct( public readonly YourEntity $entity ) {} }
Handle an event
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.
Contribute
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.
License
All contents of this package are licensed under the MIT license.