filips123/eventy

0.6 2018-10-29 21:24 UTC

This package is auto-updated.

Last update: 2024-11-16 21:38:48 UTC


README

Latest Stable Version Latest Untable Version Total Downloads License PHP

Linux Build Status Windows Build Status Code Coverage Code Quality

WordPress style actions and filters in classic (pure) PHP.

About

Actions are pieces of code you want to execute at certain points in your code. Actions never return anything but merely serve 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 parameter and you should too.

Read more about filters

Read more about actions

This project is a fork from Eventy for Laravel. The difference is that this project supports PHP 5.4 or later and it doesn't have any dependencies. Also, there are some differences in syntax.

Use Cases

EventyClassic is best used as a way to allow extensibility to your code. Whether you're creating a package or an application, Eventy can bring the extensibility you need.

For example, EventyClassic can lay down the foundation for a plugin based system. You offer an "action" that allows plugins to register themselves. You might offer a "filter" so plugins can change the contents of an array in the core. You could even offer an "action" so plugins can modify the menu of your application.

Installation

Requirements

EventyClassic requires PHP 5.4.0 or higher.

Using Composer

The reccomended way to install EventyClassic is with Composer, dependency manager for PHP.

composer require filips123/eventy

You would only need to include autoloader and namespace in your script.

<?php

use EventyClassic\Events as Eventy;

require 'vendor/autoload.php';

$eventy = new Eventy;

Ideally, class instance should be placed to dependency injection service.

Manually Installation

Alternatively, you could download files from GitHub and then manually include them in your script.

You whould need to include all files and namespace in your script.

<?php

use EventyClassic\Events as Eventy;

require 'src/Events.php';
require 'src/Event.php';
require 'src/Action.php';
require 'src/Filter.php';

$eventy = new Eventy;

Ideally, class instance should be placed to dependency injection service.

Usage

Actions

To listen to your hooks, you attach listeners.

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);

The first parameter is the name of the hook. The second would be a callback. This could be a Closure, an array callback ([$object, 'method']) 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.

You can then run actions:

$eventy->runAction('my.hook', 'awesome');

Again the first argument must be the name of the 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.

Filters

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

This is how you add a listener to this filter:

$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->runFilter('my.hook', 'awesome');
    echo 'You are '. $what;
});

You can then run filters:

$value = $eventy->runFilter('my.hook', 'awesome');

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