2017 © Pedro Peláez
 

yii2-extension behavior-trait

Allows handling events via inline declared methods, which can be added by traits

image

yii2tech/behavior-trait

Allows handling events via inline declared methods, which can be added by traits

  • Thursday, July 27, 2017
  • by klimov-paul
  • Repository
  • 5 Watchers
  • 17 Stars
  • 1,270 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 28 % Grown

The README.md

, (*1)

Behavior Trait Extension for Yii 2


This extension provides the ability of handling events via inline declared methods, which can be added via traits., (*2)

For license information check the LICENSE-file., (*3)

Latest Stable Version Total Downloads Build Status, (*4)

Installation

The preferred way to install this extension is through composer., (*5)

Either run, (*6)

php composer.phar require --prefer-dist yii2tech/behavior-trait

or add, (*7)

"yii2tech/behavior-trait": "*"

to the require section of your composer.json., (*8)

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]]., (*9)

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:, (*10)

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)!, (*11)

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., (*12)

The Versions

27/07 2017

dev-master

9999999-dev

Allows handling events via inline declared methods, which can be added by traits

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 trait behavior event

26/12 2015

1.0.0

1.0.0.0

Allows handling events via inline declared methods, which can be added by traits

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 trait behavior event