Mecha CMS

Deep learning on using the API.

Class Hook

Author: Taufik Nurrohman · 96 Views


Function hooks.


Hook::set($id = null, $fn = null, $stack = null);


Set a head.content hook with named function as the ID:

function fn_meta() {
    echo '<meta name="generator" content="Mecha">';

Hook::set('head.content', 'fn_meta');

Set a head.content hook with anonymous function:

Hook::set('head.content', function() {
    echo '<meta name="generator" content="Mecha">';

Using custom priority:

function fn_meta() {
    echo '<meta name="generator" content="Mecha">';

Hook::set('head.content', 'fn_meta', 1);

Note: 1 is the function execution priority. The smaller the value, the earlier the function will be executed. And functions with the same priority are executed in the order in which they were added to the hook. Default is 10.


Hook::get($id = null, $fail = false);


Get all of the attached hooks:


Get all hooks attached to head.content:



Hook::reset($id = null, $stack = null | $fn = null);


Remove an attached head.content hook with ID fn_meta before execution, or prevent a head.content hook with ID fn_meta to be attached:

Hook::reset('head.content', 'fn_meta');

Note: You cannot remove hooks that are attached using anonymous function because they have no ID.


Hook::fire($id, array $lot = []);


Execute the head.content hook:

Hook::fire('head.content'); // → `<meta name="generator" content="Mecha">`



Hook::NS($id, array $lot = []);

This …

Hook::NS('', [$a, $b]);

… is equal to this:

Hook::fire(['', 'bar.baz', 'baz'], [$a, $b]);

… is equal to these:

Hook::fire('', [$a, $b]);
Hook::fire('bar.baz', [$a, $b]);
Hook::fire('baz', [$a, $b]);


With named function as the ID:

function do_set_header($a, $b) {
    echo '<meta name="author" content="' . $a . ' ' . $b . '">';

Hook::set('head.content', 'do_set_header');

With anonymous function:

Hook::set('head.content', function($a, $b) {
    echo '<meta name="generator" content="' . $a . ' ' . $b . '">';

How to fire:

Hook::fire('head.content', ['Taufik', 'Nurrohman']); // → `<meta name="author" content="Taufik Nurrohman">`

As Filter 

The first value of the available arguments will be returned. So, this test will return Taufik:

__test__(Hook::fire('head.content', ['Taufik', 'Nurrohman']));

And adding a new hook with arguments and returned its first parameter with custom value will return that custom value:

Hook::set('head.content', function($a, $b) {
    return '(' . $a . ')';
}, 2);

Hook::set('head.content', function($a, $b) {
    return '{' . $a . '}';
}, 1);

Hook::set('head.content', function($a, $b) {
    echo '<meta name="author" content="' . $a . ' ' . $b . '">';
}, 3);

Hook::fire('head.content', ['Taufik', 'Nurrohman']); // → `<meta name="author" content="({Taufik}) Nurrohman">`

Returning a null value or nothing at all will return the initial value of the first argument anyway. So, these tests will return Taufik:

Hook::set('head.content', function($a, $b) {
    return null;

Hook::set('head.content', function($a, $b) {
    // Do nothing!


No comments yet.