Download the PHP package jenryollivierre/php-application-hooks without Composer
On this page you can find all versions of the php package jenryollivierre/php-application-hooks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jenryollivierre/php-application-hooks
More information about jenryollivierre/php-application-hooks
Files in jenryollivierre/php-application-hooks
Package php-application-hooks
Short Description PHP Application Hooks is a lightweight standalone package that lets you control when and where code can be executed within your application.
License MIT
Informations about the package php-application-hooks
PHP Application Hooks
Introduction
PHP Application Hooks is a lightweight standalone package that lets you control when and where specific blocks of code can be executed within your application.
Installation
install via composer with composer require "jenryollivierre/php-application-hooks": "2.0.*"
or simply download from github
How To Use
Get things started by creating an instance that can be accessed globally.
Then, at the point in your application where you will want 3rd party apps to be able to do something, you call on either of the following methods.
$hooks->applyActions($name, $arguments)
$hooks->applyFilters($name, $value, $arguments)
Actions
We use actions when we want an action/task to be carried out. To let 3rd party apps be able to carry out actions at a specific point in your application, you call on the $hooks->applyActions()
method, which takes 3 arguments.
- string $name :: the name to identify the action by. This should be unique.
- array $arguments :: an array of arguments that you want to pass to 3rd party apps hooking into your action.
- bool $return :: whether to return the contents of the action. This defaults to false as actions in general are not meant to return anything.
Example of Actions
Now, for the 3rd party apps to hook into the action of your application, they would call on the global $hook instance and call on the addAction()
method.
The addAction()
method takes 4 parameters.
- string $name :: the name of the action to hook into.
- callable $callback :: Closures | function name | array of a class instance and its method
- int $priority :: defaults to 100. Higher number priority actions are ran latest.
- int $arguments :: the number of arguments to pass to the $callback. By default, this passes all arguments. If the main application passed an array of 5 arguments to the action, the 3rd party app can state 2 to only get the first 2 parameters.
To resolve the example above, the given callback to use was Post::cleanUpDatabase(). It would work like this:
Filters
We use filters when we want to filter a value. To let filters be carried out at a specific point in your application, you call on the $hooks->applyFilters()
method, which takes 3 arguments.
- string $name :: the name to identify the filter by. This should be unique.
- mixed $value :: the value that is to be filtered.
- array $arguments :: an array of arguments that you want to pass to 3rd party apps hooking into your filter.
** Note that the $value parameter will also be passed to the $arguments as the first parameter. So technically, everything in the $arguments array will be passed to 3rd party apps as the 2nd parameter onwards.
Example of Filters
Now, for 3rd party apps to hook into our filter, they would call on the global $hook instance and call on the addFilter()
method.
The addFilter()
method takes 4 parameters.
- string $name :: the name of the filter to hook into.
- callable $callback :: Closures | function name | array of a class instance and its method
- int $priority :: defaults to 100. Higher number priority filters are ran latest.
- int $arguments :: the number of arguments to pass to the $callback. By default, this passes all arguments.
Extending & Making Your Own
The package is fully extensible, allowing you to create your own 'hooks'. You simply extend the 'JenryOllivierre\Hooks\HooksFoundation' class and then define your own methods. For example:
Or rolling your own:
The HooksFoundation class has a lot of useful methods to allow you to fully extend and make your own hooks class. They are:
Security Vulnerability
If you discover any security vulnerability, please email Jenry Ollivierre at [email protected]
License
PHP Application Hooks is open source software licensed under the MIT License