Download the PHP package horat1us/yii2-static-behavior without Composer
On this page you can find all versions of the php package horat1us/yii2-static-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-static-behavior
Yii2 Static Behavior
This package implements static behavior to allow you append behavior-like event handlers to external objects.
Usage example: You use package that contains ActiveRecord. If you need to handle some events of this record (beforeInsert, afterInsert etc.), this package definitely for you.
It uses static yii\base\Event::on()
and yii\base\Event::off()
under the hood.
The main advantage of using this static behavior:
- event handler class (ItemInterface) will be instantiated before event will be called, so dependencies will be lazy-loaded too.
Installation
Structure
- StaticBehavior - main class that deals with attaching and detaching handlers.
- Bootstrap - configurable application bootstrap class. It use StaticBehavior to attach handlers before application request and detach after request.
- ItemInterface - describes simple event handler (to be used in StaticBehavior).
- Item - abstract ItemInterface implementation. Allows to configure handlers (for example methods) for different events.
- Bootstrap\Behavior - behavior to attach handlers before action and detach after controller/module action. Should be used if some handlers will be used only when module executed.
Usage
StaticBehavior
First, you need to implement ItemInterface. Then, configure your StaticBehavior with implemented items.
Note: you can use \Closure instead of item classes (not recommended)
Detailed example
Bootstrap
To bootstrap your application you should use Bootstrap. It will attach handlers before application request and detach it after request.
Item
Main purpose of items - lazy dependency injection for event handlers. You can define dependencies in constructor or use yii2-way configuration (prefered).
Item that handles yii\db\ActiveRecord
events:
Example (handle yii\db\ActiveRecord
events)
Bootstrap\Behavior
This behavior should be used when some handlers will be used only in one module. For example:
- sending messages after authentication
- sending message with token for two-factor authentication
It can be used with controller or any another component (you will need to configure events).
Example (authorization tokens)
Contributors
- Alexander
Letnikow
License
MIT