Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yii2tech/behavior-trait
Allows handling events via inline declared methods, which can be added by traits
https://github.com/yii2tech/behavior-trait
behavior trait yii yii2 yii2-extension
Last synced: 10 days ago
JSON representation
Allows handling events via inline declared methods, which can be added by traits
- Host: GitHub
- URL: https://github.com/yii2tech/behavior-trait
- Owner: yii2tech
- License: other
- Archived: true
- Created: 2015-07-02T10:32:46.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-07-03T11:39:31.000Z (over 5 years ago)
- Last Synced: 2024-03-26T00:01:16.453Z (11 months ago)
- Topics: behavior, trait, yii, yii2, yii2-extension
- Language: PHP
- Homepage:
- Size: 8.79 KB
- Stars: 20
- Watchers: 6
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
Behavior Trait Extension for Yii 2
This extension provides the ability of handling events via inline declared methods, which can be
added via traits.For license information check the [LICENSE](LICENSE.md)-file.
[![Latest Stable Version](https://poser.pugx.org/yii2tech/behavior-trait/v/stable.png)](https://packagist.org/packages/yii2tech/behavior-trait)
[![Total Downloads](https://poser.pugx.org/yii2tech/behavior-trait/downloads.png)](https://packagist.org/packages/yii2tech/behavior-trait)
[![Build Status](https://travis-ci.org/yii2tech/behavior-trait.svg?branch=master)](https://travis-ci.org/yii2tech/behavior-trait)Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist yii2tech/behavior-trait
```or add
```json
"yii2tech/behavior-trait": "*"
```to the require section of your composer.json.
Usage
-----This extension introduces special trait [[\yii2tech\behaviortrait\BehaviorTrait]], which if used provides
the ability of handling events via inline declared methods, which can be added via other traits.
This trait can be added to any descendant of [[\yii\base\Component]].Each event handler method should be named by pattern: '{eventName}Handler{UniqueSuffix}', where 'eventName' is a
name of the event the method should handle, 'UniqueSuffix' any suffix, which separate particular event handler
method from the others.
For example: if the class has an event 'beforeInsert' it can introduce method named `beforeInsertHandlerEncryptPassword`,
which will be automatically triggered when event 'beforeInsert' is triggered:```php
use yii\db\ActiveRecord;
use yii2tech\behaviortrait\BehaviorTrait;class User extends ActiveRecord
{
use BehaviorTrait; // add `BehaviorTrait` allowing to use inline event handlers
use EncryptPasswordTrait; // add trait, which introduce inline event handler// ...
}trait EncryptPasswordTrait
{
public function beforeInsertHandlerEncryptPassword(Event $event)
{
if (!empty($this->newPassword)) {
$this->password = sha1($this->newPassword);
}
}
}
```> Attention: watch for the naming collisions, ensure any inline handler declared either in class or via trait has
a unique name (with unique suffix)!> Note: using traits instead behaviors improves performance but is less flexible. Keep in mind that such approach
has been rejected at Yii2 core at [yiisoft/yii2#1041](https://github.com/yiisoft/yii2/pull/1041).