Hooks
@TODO remove Laravel links to preserve version integrity
A hook
is a method of directly adding functionality to a class from the outside and without having to extend it or directly modify it.
Introduction
A hook
is similar to a callback trigger. A hook applies to any instance of a class and any parent that inherits that class.
Bindings
By binding
a hook you can change the context of $this
so that the hook behaves like an actual method of the class. Where $this
refers to the class the hook is being run on and not where the hook is being defined (like normal Closure behavior).
Basic Usage
Use the \Anomaly\Streams\Platform\Traits\Hookable
trait in your class to get started.
Hookable::hook()
The hookable
method let's you register a hook on a hookable class.
Returns: $this
Arguments
Key | Required | Type | Default | Description |
---|---|---|---|---|
$hook |
true |
string |
none |
The hook name. |
$callback |
true |
string|Closure |
none |
The callback logic or callable string. |
Example
$hookable->hook(
'avatar',
function ($email) {
return 'https://www.gravatar.com/avatar/' . md5($email);
}
);
$hookable->avatar('[email protected]');
Hookable::bind()
The bind
method is very similar to hook
but the callback is available for all instances of the class as well as any parents of the class.
Returns: $this
Arguments
Key | Required | Type | Default | Description |
---|---|---|---|---|
$hook |
true |
string |
none |
The hook name. |
$callback |
true |
string|Closure |
none |
The callback logic or callable string. |
Example
$hookable->bind(
'customer',
function () {
/* @var UserModel $this */
return $this->hasOne(CustomerModel::class, 'user_id');
}
);
$hookable->bind(
'get_customer',
function () {
/* @var UserModel $this */
return $this->customer()->first();
}
);
Hookable::call()
The call
method fires the hook.
Returns: mixed
Arguments
Key | Required | Type | Default | Description |
---|---|---|---|---|
$hook |
true |
string |
none |
The hook to call. |
$parameters |
false |
array |
null |
Parameters to pass to the callback. |
Example
$hookable->call('get_customer')->billing_address;
Twig
{{ user().call('get_customer').billing_address }}
Hookable::hasHook()
The hasHook
method returns whether a hook exists or not for the object.
Returns: boolean
Arguments
Key | Required | Type | Default | Description |
---|---|---|---|---|
$hook |
true |
string |
none |
The hook name to check existance of. |
Example
$hookable->hasHook('get_customer');
Method Handlers
Not always, but generally classes in the Streams Platform that use the Hookable
trait will map the __call
method through hooks.
What this means is that hooks will be checked for when the __call
method is triggered.
If the hook
is get_customer
the method will be getCustomer
.
// First bind the hook.
$hookable->bind(
'get_customer',
function () {
/* @var UserModel $this */
return $this->customer()->first();
}
);
// Now fire using the handler method.
$hookable->getCustomer()->billing_address;
Getter Behavior
Not always, but generally classes in the Streams Platform that use the Hookable
trait will map the __get
method through hooks.
What this means is that hooks prefixed with get_
will be checked for when the __get
method is triggered.
// Register the hook
$hookable->bind(
'get_customer',
function () {
/* @var UserModel $this */
return $this->customer()->first();
}
);
// Call the hook with method handler.
$hookable->customer->billing_address;
This method is very helpful in the view layer:
{{ user().customer.billing_address }}