kellenon/yii2-event
Composer 安装命令:
composer require kellenon/yii2-event
包简介
Package
README 文档
README
Yii2 Event
Convenient event system for Yii2.
This component will help create a system of events, which will be as simple to use as a system of events in Laravel.
Installation
To install, either run
composer require kellenon/yii2-event
or add
"kellenon/yii2-event": "@dev"
to the require section of your composer.json file.
Usage
You can attach a behavior to a component and declare events and listeners in it.
The listener class must implement the interface EventListenerInterface
Behaviors
EventBehavior
<?php use Kellenon\Yii2Event\Behavior\EventBehavior; use yii\base\Model; class Order extends ActiveRecord { public const EVENT_ORDER_CREATED = 'order-created'; public function behaviors(): array { return [ [ 'class' => EventBehavior::class, 'events' => [ static::EVENT_ORDER_CREATED => [ Listener1::class, Listener2::class, Listener3::class, ], ], ], ]; } }
// client code $order = new Order(); $order->load(Yii::$app->request->post()); if ($order->save()) { $order->trigger(Order::EVENT_ORDER_CREATED); }
EventStateBehavior
<?php use Kellenon\Yii2Event\Behavior\EventStateBehavior; class Order extends ActiveRecord { public const EVENT_AFTER_COMPLETE = 'after-complete'; public const STATUS_NEW = 'new'; public const STATUS_CONFIRMATION = 'confirmation'; public const STATUS_PROCESSING = 'processing'; public const STATUS_PAYMENT_COMPLETED = 'payment-completed'; public const STATUS_COMPLETE = 'complete'; public const STATUS_CANCELLED = 'cancelled'; /** * @return array[] * If the transition condition is met, the event will be triggered automatically. */ public function behaviors(): array { return [ [ 'class' => EventBehavior::class, 'events' => [ static::EVENT_AFTER_COMPLETE => [ Listener1::class, Listener2::class, Listener3::class, ], ], ], [ 'class' => EventStateBehavior::class, 'attribute' => 'status', 'transitionEventStates' => [ static::EVENT_AFTER_COMPLETE => [ static::STATUS_NEW => [static::STATUS_COMPLETE, static::STATUS_CANCELLED], static::STATUS_CONFIRMATION => [static::STATUS_COMPLETE, static::STATUS_CANCELLED], static::STATUS_PROCESSING => [static::STATUS_COMPLETE, static::STATUS_CANCELLED], static::STATUS_PAYMENT_COMPLETED => [static::STATUS_COMPLETE, static::STATUS_CANCELLED], ], ], ], ]; } }
// client code $order = new Order(); $order->status = Order::STATUS_COMPLETE; // save() -> Event 'after-complete' will be triggered automatically $order->save();
Bootstrap
<?php use Kellenon\Yii2Event\EventBootstrap; return [ 'bootstrap' => [ [ 'class' => EventBootstrap::class, 'events' => [ 'eventName' => [ Listener1::class, Listener2::class, Listener3::class, ], ], ], ], ];
// client code Yii::$app->trigger('eventName');
统计信息
- 总下载量: 4.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-11