mhndev/event

1.2.2 2017-10-03 13:58 UTC

This package is auto-updated.

Last update: 2024-11-06 09:50:33 UTC


README

This is a really simple and lightweight php package for developing event based software

Installation

composer require mhndev/event

Sample Usage

bind an closure to an event

Event::bind('order.payed',function($order){
    //do some cool stuf here
});

Another example

class MyClass {
    public function __invoke($order) {
        //do something here
    }
}

$myObject = new MyClass;


Event::bind('order.payed', $myObject($order));

trigger an event

// pass $order object as second argument
Event::trigger('order.payed', $order);

Entity ( Model ) Observer

Binding an observer for a specific event on a model.

User::addObserver('before_update', function($driver){

});

Firing an Event on a model object.

Active Record Pattern

class User
{
    use \mhndev\event\ObservableTrait;
    
    
    function update(array $data)
    {
        $user = $this;
        
        $this->fireEvent('before_update', $this);

        $updatedUser = $this->update($array);

        $driver->fireEvent('after_update', $user, $updatedUser);
    }
}

Data Mapper Pattern

class User
{
    use \mhndev\event\ObservableTrait;
}


class UserRepository
{


    function update($user_identifier, array $data)
    {
        $user = $this->findByIdentifier($user_identifier);

        $user->buildByOptions($data);

        $user->fireEvent('before_update', $user);

        $updatedUser = $this->update($user);

        $driver->fireEvent('after_update', $user, $updatedUser);

        return $updatedUser;
    }
}

Todos

-implement tests