Download the PHP package flexpress/component-hooks without Composer
On this page you can find all versions of the php package flexpress/component-hooks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package component-hooks
FlexPress theme component
Install via pimple
Before we done anything else we need to setup the configs in pimple
What we have done here is created two configs, one for the hooker and one for a hookable class, we have then passed in the configHookable to the hooker so it can hook all its hooks up.
Note that that $c['objectStorage'] is the SPLObjectStorage class.
Setting up hooks
To add hook we must first create a class that uses the HookableTrait, lets make a very basic class that does this:
Simples, all done but we should probably add a hook method, so lets do that now:
This example adds a hook for the config class and the adminInit function for the action admin_init, not that the method is in camelcase.
As well as setting up actions you can add filters like this:
In this example we have added a filter that strips the tags from the title by hooking into the the_title filter.
So for filters you simply add this to the docblock above the method:
and for filters you do the same but change the type to filter:
Advanced usage
As well as specifying the type of hook you can also specify the priority like this:
And finally you can also specify the number of paramters you expect like this:
Which allows you to add both action and filter hooks, speficy the priority as well as the number of params you expect, so nothing is taken away from the add_action and add_filter functions.
Public methods - Hooker
- hookUp() - Loops through all the hookables passed to the hooker and hooks them up.
Public methods - HookableTrait
- hookUp() - Uses reflection to find all public methods with a valid docblock and hook them up.
Protected methods - HookableTrait
- getMethodAttributes() - Gets all the attibutes of a methods docblock.
- getHookName($methodName, $attributes) - For a given method name and attibutes, gets the hook name. e.g. theContent becomes the_content
- registerHook($hook_name, $method_name, $attributes) - Actuallys registers the hook depending on what type it is.