Download the PHP package slaxweb/hooks without Composer
On this page you can find all versions of the php package slaxweb/hooks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download slaxweb/hooks
More information about slaxweb/hooks
Files in slaxweb/hooks
Informations about the package hooks
Hooks
Hooks component for the SlaxWeb/Framework, to execute arbitrary code in regular application execution. Even when the component is meant for the SlaxWeb/Framework it can be used outside of it as well. It the SlaxWeb/Logger as an additional component.
For ease of use the component provides a Factory and a Service Provider for the Pimple/Container.
Installation
Easiest form of installation is through composer, edit your composer.json file to contain:
Usage
This section will cover instantiation of the Hooks container, and adding simple hook definitions to the container, as well as execution of this definition, and advanced execution of hook definitions.
Instantiation
You do not need to instantiate the Logger component it self, but you do have to instantiate the Config component, and make sure that the logger configuration is loaded, refer to Logger documentation on how to get this done.
When you got the dependencies instantiated the usage is pretty straight forward:
You can achieve the same thing using the Pimple Dependency Injection Container, just make sure that you have registered the Service Providers of all 3 components:
- SlaxWeb/Config
- SlaxWeb/Logger
- SlaxWeb/Hooks
When you have all those three providers registered, you can safely use the hooks container:
Multiple Hook Definitions
As it is already visible from above examples, hook execution method will return the value of the hook definition. But a single hook point may hold more than one definition. In this case the exec method will return all return values in an array, except if the return value of definition was null. If no definition returned a valid return value, the exec method will return an empty array.
For a better representation of the above examples, please refer to the code example bellow:
Definition Execution Interruption
Additionally to multiple definitions per hook, a hook can also prevent other definitions from executing. For this case an instance of the container is passed into the hook definition as the first parameter. Each hook may call stopExec method on that passed in container object, and it will stop execution of other hook definitions.
Hook definition parameters
When calling the exec method you can pass additional parameters to it, and all those parameters will be passed on to your hook definition.