Hook
, (*1)
If you have ever used an event manager, you will find the Hooks component very similar. With the HookManager you can create hooks, on which you can hook callbacks to be executed when the hooks are triggered., (*2)
, (*3)
Installation
If you are working on an Asgard project you don't need to install this library as it is already part of the standard libraries., (*4)
composer require asgard/hook 0.*
, (*5)
Usage in the Asgard Framework
$hm = $container['hooks'];
The container is often accessible as a method parameter or through a ContainerAware object. You can also use the singleton but it is not recommended., (*6)
, (*7)
Usage outside the Asgard Framework
$hm = new \Asgard\Hook\HookManager;
, (*8)
Create a hook
$hm->hook('name_of_hook', function($chain, $param1) {
// ...
});
The first parameter is always a \Asgard\Hook\HooksChain object. The next ones are passed when the hook is triggered., (*9)
, (*10)
Trigger a hook
$hm->trigger('name_of_hook', [$param]);
If you want to execute your own function when calling trigger, use the last argument:, (*11)
$hm->trigger('name_of_hook', [$param], function($chain, $param) {
// ...
});
, (*12)
Executing callbacks before and after hooks
To execute functions before a hook:, (*13)
$hm->preHook('name_of_hook', function($chain, $param) {
// ...
});
And after:, (*14)
$hm->postHook('name_of_hook', function($chain, $param) {
// ...
});
, (*15)
Filters
Hooks can be used as filters when parameters are passed by reference, (*16)
$hm->hook('name_of_hook', function($chain, &$param) {
$param = 123;
});
$hm->trigger('name_of_hook', [&$param]);
, (*17)
The HooksChain object
The chain contains all the callbacks to be executed in a hook. If a function returns a value, the chain stops and the value is returned by the trigger method., (*18)
The chain call also be stopped by calling:, (*19)
$chain->stop();
The function calling the stop method will be the last one to be executed., (*20)
To know how many functions have been executed in a hook:, (*21)
$hm->trigger('name_of_hook', [$param], null, $chain);
$count = $chain->executed;
Here we provide a reference to retrieve the chain object and its executed property., (*22)
, (*23)
HooksContainer
A HooksContainer is a class containing hooks. It extends Asgard\Hook\HooksContainer and contains methods that are matched to hooks with annotations., (*24)
Example:, (*25)
<?php
namespace Bundle\Hooks;
class SomeHooks extends \Asgard\Hook\HooksContainer {
/**
* @Hook("Asgard.Http.Start")
*/
public static function start($chain, $request) {
//do something when HTTP starts processing a request
}
}
Here the method start is executed when the hook "Asgard.Http.Start" is triggered., (*26)
If you are working with a Asgard project, all HooksContainer in the Hooks/ folder of a bundle are automatically loaded., (*27)
If not, you can register the hooks with:, (*28)
$annotationsReader = new \Asgard\Hook\AnnotationReader;
$hooks = $annotationsReader->fetchHooks('Bundle\Hooks\SomeHooks');
$hookManager->hooks($hooks);
Contributing
Please submit all issues and pull requests to the asgardphp/asgard repository., (*29)
License
The Asgard framework is open-sourced software licensed under the MIT license, (*30)