atournayre/entities-events-bundle

此包已 弃用 且不再维护。未建议替代包。
此包的最新版本(dev-master)没有可用的许可信息。

此包提供了一种方式,使用Doctrine ORM事件和Symfony事件分发器来在实体创建、更新或删除时分发事件。

dev-master 2023-11-26 20:01 UTC

This package is auto-updated.

Last update: 2024-05-26 21:00:52 UTC


README

此包提供了一种方式,使用Doctrine ORM事件和Symfony事件分发器来在实体创建、更新或删除时分发事件。

需求

Symfony ^6 || ^7

PHP >=8.2

安装

使用 Composer 安装此包

Composer

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
  }
}

这就完了,包会处理剩下的。

贡献

欢迎为包做出贡献!

许可

此包的所有内容均受MIT许可许可。