2017 © Pedro Peláez
 

library local-eventing

In object eventing execution

image

josephlavin/local-eventing

In object eventing execution

  • Monday, August 22, 2016
  • by josephlavin
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Local Eventing

A simple php trait that allows for execution of local methods in an eventing like fashion. Add methods to a class using this trait following a naming convention and they will be executed when the local event is fired., (*1)

use josephlavin\localEventing\LocalEventing;

class dummy
{
    use LocalEventing;

    public function save()
    {
        // do your saving logic...
        $this->fireLocalEvent('saved');
    }

    protected function __onLocalEvent_saved_send_notification()
    {
        // Logic here to send notification...
    }

    protected function __onLocalEvent_saved_log_event()
    {
        // Logic for logging here...
    }
}

Event Listener Method Naming Convention

All event listener methods must be named in this fashion: __onLocalEvent_[event]_[description](), (*2)

  • __onLocalEvent_ : namespace for the local eventing system
  • [event]: the same string given to $this->fireLocalEvent('event')
  • [description]: a simple description of what this method does

Installation

$ composer require josephlavin/local-eventing

Use Case

Lets say we have a basic model type class which fires local events:, (*3)

class BaseModel
{
    use LocalEventing;

    public function __construct()
    {
        $this->fireLocalEvent('created');
    }

    public function insert()
    {
        $this->fireLocalEvent('preInsert');
        // the insert logic...
        $this->fireLocalEvent('postInsert');
    }
}

We can create another trait that relies on the LocalEventing trait and adds __onLocalEvent__ methods. Notice how this trait has an abstract method _require_trait_LocalEventing. This serves as a reminder to the developer that this trait relies on the LocalEventing trait to work properly., (*4)

trait UuidPrimaryKey
{
    // reminder that we must also use LocalEventing Trait
    abstract protected function _require_trait_LocalEventing();

    protected function __onLocalEvent_preInsert_populate_uuid()
    {
        // generate and set a uuid primary key here...
    }
}

Now any models (that extend base model) can use the UuidPrimaryKey trait and gain that functionality., (*5)

```php class MyModel extends BaseModel { use UuidPrimaryKey; } ````, (*6)

The Versions

22/08 2016

dev-master

9999999-dev

In object eventing execution

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Lavin

14/08 2016

1.0.1

1.0.1.0

In object eventing execution

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Lavin

14/08 2016

1.0.0

1.0.0.0

In object evening execution

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Lavin