Mecha CMS

Deep learning on using the API.

Class Hook

Author: Taufik Nurrohman · 79 Views

Tweet

Function hooks.

Set 

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

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

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

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

Set a head.content hook with anonymous function:

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

Using custom priority:

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

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

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

Get all of the attached hooks:

__test__(Hook::get());

Get all of the attached the_header hooks:

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

Reset 

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

Remove an attached the_header hook with ID do_set_header before execution, or prevent a the_header hook with ID do_set_header to be attached:

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

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

Fire 

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

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">`

0 Comments

No comments yet.