PHP Classes

PHP Hook Function Call: Register hooks and execute associated actions

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 120 All time: 9,502 This week: 660Up
Version License PHP version Categories
php-hook 1.0.8MIT/X Consortium ...5.6PHP 5, Language
Description 

Author

This package can register hooks and execute associated actions.

It can register action handler hooks associated to a given action name by passing a callback function.

The class can also process action executions by calling the registered hooks for a given action name.

Picture of Josantonius
  Performance   Level  
Name: Josantonius is available for providing paid consulting. Contact Josantonius .
Classes: 31 packages by
Country: Spain Spain
Age: ???
All time rank: 132526 in Spain Spain
Week rank: 180 Up6 in Spain Spain Up
Innovation award
Innovation award
Nominee: 11x

Documentation

PHP Hook library

Latest Stable Version Latest Unstable Version License Codacy Badge Total Downloads Travis PSR2 PSR4 CodeCov

Versión en español

Library for handling hooks.

Requirements

This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.

Installation

The preferred way to install this extension is through Composer.

To install PHP Hook library, simply:

$ composer require Josantonius/Hook

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:

$ composer require Josantonius/Hook --prefer-source

You can also clone the complete repository with Git:

$ git clone https://github.com/Josantonius/PHP-Hook.git

Or install it manually:

Download Hook.php:

$ wget https://raw.githubusercontent.com/Josantonius/PHP-Hook/master/src/Hook.php

Available Methods

Available methods in this library:

- Get Hook instance:

Hook::getInstance();

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $id | Unique ID for multiple instances. | string | No | '0' |

# Return (object) ? Hook instance

- Set method name for use singleton pattern:

Hook::setSingletonName($method);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $method | Set method name for use singleton pattern. | callable | No | |

# Return (void)

- Attach custom function to action hook:

Hook::addAction($tag, $function, $priority, $args);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $tag | Action hook name. | string | Yes | | | $function | Function to attach to action hook. | callable | Yes | | | $priority | Order in which the action is executed. | int | No | 8 | | $args | Number of arguments accepted. | int | No | 0 |

# Return (boolean)

- Add actions hooks from array:

Hook::addActions($actions);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $actions | Actions hooks | array | Yes | |

# Return (boolean)

- Run all hooks attached to the hook:

By default it will look for getInstance method to use singleton pattern and create a single instance of the class. If it does not exist it will create a new object.

Hook::doAction($tag, $args, $remove);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $tag | Action hook name | string | Yes | | | $args | Optional arguments | mixed | No | array() | | $remove | Delete hook after executing actions | boolean | No | true |

# Return (mixed|false) ? output of the last action or false

- Returns the current action hook:

Hook::current();

# Return (string|false) ? current action hook

- Check if there is a certain action hook:

Hook::isAction($tag);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $tag | Action hook name | string | Yes | |

# Return (boolean)

Quick Start

To use this library with Composer:

require __DIR__ . '/vendor/autoload.php';

use Josantonius\Hook\Hook;

Or If you installed it manually, use it:

require_once __DIR__ . '/Hook.php';

use Josantonius\Hook\Hook;

Usage

Example of use for this library:

- Add action hook:

Hook::addAction('css', ['Josantonius\Hook\Test\Example', 'css']);

- Add action hook with priority:

Hook::addAction('js', ['Josantonius\Hook\Test\Example', 'js'], 1);

- Add action hook with priority and arguments number:

$instance = new Example;

Hook::addAction('meta', [$instance, 'meta'], 2, 1);

- Add action hook and set singleton method:

Hook::setSingletonName('singletonMethod');

$instance = call_user_func(
    'Josantonius\Hook\Test\Example::singletonMethod'
);

Hook::addAction('article', [$instance, 'article'], 3, 0);

- Add multiple action hooks:

$instance = new Example;
        
Hook::addActions([
    ['after-body', [$instance, 'afterBody'], 4, 0],
    ['footer', [$instance, 'footer'], 5, 0],
]);

- Add multiple action hooks and set singleton method:

Hook::setSingletonName('singletonMethod');

$instance = call_user_func(
    'Josantonius\Hook\Test\Example::singletonMethod'
);

Hook::addActions([
    ['slide', [$instance, 'slide'], 6, 0],
    ['form', [$instance, 'form'], 7, 2],
]);

- Check if is action:

Hook::setSingletonName('singletonMethod');

Hook::isAction('meta');

- Execute action hooks:

Hook::doAction('css');
Hook::doAction('js');
Hook::doAction('after-body');
Hook::doAction('article');
Hook::doAction('footer');

- Execute action hook with arguments:

Hook::doAction('meta', 'The title');
Hook::doAction('form', ['input', 'select']);

Tests

To run tests you just need composer and to execute the following:

$ git clone https://github.com/Josantonius/PHP-Hook.git

$ cd PHP-Hook

$ composer install

Run unit tests with PHPUnit:

$ composer phpunit

Run PSR2 code standard tests with PHPCS:

$ composer phpcs

Run PHP Mess Detector tests to detect inconsistencies in code style:

$ composer phpmd

Run all previous tests:

$ composer tests

? TODO

  • [ ] Add new feature.
  • [ ] Improve tests.
  • [ ] Improve documentation.
  • [ ] Refactor code for disabled code style rules. See phpmd.xml and .php_cs.dist.

Contribute

If you would like to help, please take a look at the list of issues or the To Do checklist.

Pull requests

  • Fork and clone.
  • Run the command `composer install` to install the dependencies. This will also install the dev dependencies.
  • Run the command `composer fix` to excute code standard fixers.
  • Run the tests.
  • Create a branch, commit, push and send me a pull request.

Repository

The file structure from this repository was created with PHP-Skeleton.

License

This project is licensed under MIT license. See the LICENSE file for more info.

Copyright

2017 - 2018 Josantonius, josantonius.com

If you find it useful, let me know :wink:

You can contact me on Twitter or through my email.


  Files folder image Files (15)  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (2 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php_cs.dist Example Example script
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONDUCT.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpmd.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README-ES.md Doc. Documentation
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (15)  /  src  
File Role Description
  Plain text file Hook.php Class Class source

  Files folder image Files (15)  /  tests  
File Role Description
  Plain text file Example.php Class Class source
  Plain text file HookTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:120
This week:0
All time:9,502
This week:660Up