Place these in your code. When a Hook and an Event share the key, the Event will run.
Full example:
<?php
$result = Hook::forge('the_hook_key')
->setObject($this)
->setParam('the_param_key', $value)
->setParams(array('another_key' => $another_value))
->execute();
Instantiates a new hook.
- string $key - The key on which Events will activate.
See: new Hook($key)
Chainable
Disables a hook key.
- string $key - The key to disable.
Enabled a disabled hook key.
- string $key - The key to enable.
Binds the object to the events. It's meant to be used with $this
, but you can use it with any object.
- mixed $object - The object to bind.
Chainable
Set a parameter for the Event.
- string $key - The key for the value.
- mixed $key - The value
Chainable
Array version of ->setParam()
.
- array $array - Associative array as $key => $value
Chainable
Runs the Events on the hook.
Returns: \Foolz\Plugin\Result - A Result object filtered by the events.
When set, once a Hook with the same key is found, this will be run.
Full examples:
<?php
Event::forge('the_hook_key')
->setCall(function($result){
})
->setPriority(3);
Event::forge('another_hook_key')
->setCall('Foolz\Theme\Plugin::rainbows')
->setPriority(8);
The higher the number, the lower the priority. You can go negative to achieve a higher priority. The hooks with lower priority will run later than the ones with higher priority. This also means that events with lower priority will edit the result after the high priority ones, thus modifying their results.
The default priority is 5.
Instantiates a new hook.
- string $key - The Hook key on which the Event will activate.
See: new Event($key)
Chainable
Removes all the Events bound to a key up to now
- string $key - The Hook key to search for
Returns an array of the Events with the $key
- string $key - The Hook key to search for
Returns: array - An array of events in decreasing order of priority
Returns the current priority for the Event.
Returns: int - The priority number
Sets the new priority.
- int $key - The new priority number
Chainable
Returns the closure or string to the static method.
Returns: string|Callable - The closure or string to static method
Sets the Closure or string to static method to call
- string|Callable $callable - Closure or string to static method to call
PHP 5.4 Enhancement: If on the Hook you used ->setObject($object)
, Closures will have the object set to $this
and be in the object's scope.
The result is an object to modify during the Events. The Hook will return the Result filtered by the Events.
Full example:
<?php
Event::forge('yet_another_hook')
->setCall(function($result){
$this_param = $result->getParam('this_param');
$result->setParam('another_param', 'You are not here anymore.')
$result->set($this_param);
});
$result = Hook::forge('yet_another_hook')
->setParam('this_param', 'Nothing to see here.')
->setParam('another_param', 'I am still here.')
->execute();
// echoes "Nothing to see here."
echo $result->get('Your fallback');
// echoes "You are not here anymore."
echo $result->getParam('another_param');
// uses the original parameter, and echoes "I am still here."
echo $result->getParam('another_param', true);
Don't get fooled by the order. Hook creates $result
first, passes it to the Event. The Event modifies the $result
. It then goes back to the Hook, that returns it to you.
Returns the result.
Returns: mixed - The result that has been set. The fallback if no result has been set. \Foolz\Plugin\Void if no result or fallback has been set.
Notice: Void is useful to recognize null
as a valid result from an Event.
Sets the result.
- mixed $val - Any value to be set as result
Chainable
Gets the parameter.
- int|string $key - The key that was given to the parameter
- bool $orig - If true returns the first assigned value of the parameter
Sets a parameter.
- int|string $key - The key to give to the value
- mixed $value - The value to assign to the parameter
Chainable
Returns the array of parameters.
- bool $orig - If true returns the first assigned values of the parameters
Sets several parameters.
- array $array - An array of $key => $value
Chainable