Skip to content

Latest commit

 

History

History
182 lines (113 loc) · 4.97 KB

README.md

File metadata and controls

182 lines (113 loc) · 4.97 KB

PHP Hook library

Latest Stable Version Total Downloads Latest Unstable Version License

Versión en español

Library for handling hooks.



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 (including tests, vendor folder, exceptions not used, docs...) you can use:

$ composer require Josantonius/Hook --prefer-source

Or you can also clone the complete repository with Git:

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

Requirements

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

To use this library in HHVM (HipHop Virtual Machine) you will have to activate the scalar types. Add the following line "hhvm.php7.scalar_types = true" in your "/etc/hhvm/php.ini".

Quick Start and Examples

To use this class, simply:

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

use Josantonius\Hook\Hook;

Available Methods

Available methods in this library:

Hook::getInstance();
setSingletonName($method);
Atttribute Description Type
$method Set method name for use singleton pattern string

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

$tag → (string) → Action hook name.

$func → (callable) → Function to attach to action hook.

$priority → (int) (Optional) → Order in which the action is executed.

$args → (int) (Optional) → Number of arguments accepted.

Hook::addActions($actions);

$actions → (array) → Number of arguments accepted.

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

$tag → (string) → Action hook name.

$args → (mixed) (Optional) → Optional arguments.

$remove → (boolean) (Optional) → Delete hook after executing actions.

Hook::current();

Usage

Example of use for this library:

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

use Josantonius\Hook\Hook;

/* Add actions */

Hook::addAction('css', ['Namespace\Class\Example', 'css'], 2, 0);

$hooks = [
    ['meta',       ['Namespace\Class\Example', 'meta'],      1, 0],
    ['js',         ['Namespace\Class\Example', 'js'],        3, 0],
    ['after-body', ['Namespace\Class\Example', 'afterBody'], 4, 0],
    ['footer',     ['Namespace\Class\Example', 'footer'],    5, 0],
];

Hook::addActions($hooks);

/* Run actions */

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

Tests

To use the test class, simply:

<?php
$loader = require __DIR__ . '/vendor/autoload.php';

$loader->addPsr4('Josantonius\\Hook\\Tests\\', __DIR__ . '/vendor/josantonius/hook/tests');

use Josantonius\Hook\Tests\HookTest;

Available test methods in this library:

HookTest::testAddHooksMethod();
HookTest::testAddHooksArray();
HookTest::testAddHooksInstance();
HookTest::testSetSingletonName();
HookTest::testCurrentHook();
HookTest::testExecuteHooks();

Exception Handler

This library uses exception handler that you can customize.

Contribute

  1. Check for open issues or open a new issue to start a discussion around a bug or feature.
  2. Fork the repository on GitHub to start making your changes.
  3. Write one or more tests for the new feature or that expose the bug.
  4. Make code changes to implement the feature or fix the bug.
  5. Send a pull request to get your changes merged and published.

This is intended for large and long-lived objects.

Repository

All files in this repository were created and uploaded automatically with Reposgit Creator.

Licensing

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

Copyright

2017 Josantonius, josantonius.com

If you find it useful, let me know 😉

You can contact me on Twitter or through my email.