Mecha CMS

Deep learning on using the API.

Class Hook

Author: Taufik Nurrohman · 96 Views

Tweet

Function hooks.

Set 

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

Example:

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.

Get 

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

Example:

Get all of the attached hooks:

__test__(Hook::get());

Get all hooks attached to head.content:

__test__(Hook::get('head.content'));

Reset 

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

Example:

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.

Fire 

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

Example:

Execute the head.content hook:

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

Advance 

Namespaces 

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

This …

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

… is equal to this:

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

… is equal to these:

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

Arguments 

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

0 Comments

No comments yet.