PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Josantonius   PHP Hook Function Call   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Hook Function Call
Register hooks and execute associated actions
Author: By
Last change:
Date: 6 years ago
Size: 8,397 bytes
 

Contents

Class file image Download

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.