WordPress-like actions and filters for Laravel
PHP
Latest commit 9dfa364 Jan 20, 2017 @tormjens committed on GitHub Merge pull request #4 from mrchimp/callables
Allow using array callables
Permalink
Failed to load latest commit information.
src Allow using array callables Jan 20, 2017
README.md initial commit Feb 12, 2016
composer.json fixes composer.json issue Feb 12, 2016

README.md

Eventy

Actions and filters in Laravel. WordPress-style.

Bring the event system you've learned to love to your Laravel project. Eventy (for lack of a better name) is a simple action and filter system.

About

Actions are pieces of code you want to execute at certain points in your code. Actions never return anything, but merely serves as the option to hook in to your existing code without having to mess things up.

Filters are made to modify entities. They always return some kind of value. By default they return their first parameters, and you should too.

Installation

  1. Install using Composer
composer require tormjens/eventy
  1. Add the service provider to the providers array in your config/app.php.
    'TorMorten\Eventy\EventServiceProvider',
  1. Add the facade in config/app.php
    'Eventy' => TorMorten\Eventy\Facades\Events::class,

Usage

Actions

Anywhere in your code you can create a new action by so:

Eventy::action('my.hook', 'awesome');

The first parameter is the name of the hook, you will use this at a later point when you'll be listening to your hook. All subsequent parameters are sent to the action as parameters. These can be anything you'd like. For example you might want to tell the listeners that this is attached to a certain model. Then you would pass this as one of the arguments.

To listen to your models, you attach listeners. These are best added to your AppServiceProvider boot() method.

For example if you wanted to hook in to the above hook, you could do:

Eventy::addAction('my.hook', function($what) {
    echo 'You are '. $what;
}, 20, 1);

Again the first argument must be the name of the hook. The second would be a callback. This could be either a Closure, a string referring to a class in the application container (MyNamespace\Http\Listener@myHookListener) or a globally registered function function_name. The third argument is the priority of the hook. The lower the number, the earlier the execution. The fourth parameter specifies the number of arguments your listener accepts.

Filters

Filters work in much of the same way as actions, and have the exact same build-up as actions. The most significat difference is that filters always return their value.

To add a filter:

$value = Eventy::filter('my.hook', 'awesome');

If no listeners are attached to this hook, the filter would simply return 'awesome'.

This is how you add a listener to this filter (still in the AppServiceProvider):

Eventy::addFilter('my.hook', function($what) {
    $what = 'not '. $what;
    return $what;
}, 20, 1);

The filter would now return 'not awesome'. Neat!

You could use this in conjunction with the previous hook.

Eventy::addAction('my.hook', function($what) {
    $what = Eventy::filter('my.hook', 'awesome');
    echo 'You are '. $what;
});

Using in Blade

There are two directives availiable so you can use this in your Blade templates.

Adding the same action as the one in the action example above:

@action('my.hook', 'awesome')

Adding the same filter as the one in the filter example above:

You are @filter('my.hook', 'awesome')