Download the PHP package rubenmartindev/prestashop-module-hook-bus without Composer
On this page you can find all versions of the php package rubenmartindev/prestashop-module-hook-bus. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rubenmartindev/prestashop-module-hook-bus
More information about rubenmartindev/prestashop-module-hook-bus
Files in rubenmartindev/prestashop-module-hook-bus
Package prestashop-module-hook-bus
Short Description Flexible and centralized way to route and handle PrestaShop hooks
License MIT
Informations about the package prestashop-module-hook-bus
PrestaShop Hook Bus
This library provides a flexible and centralized way to route and handle PrestaShop hooks.
Overview
As you develop your module and add hooks to the main file, the module can grow disproportionately; all logic ends up in a single place, breaking the SOLID Single Responsibility principle and making maintenance and readability chaotic.
Ideally, each hook should be self-contained and have a single responsibility. Making it easy to configure, use, and maintain.
Compared to managing hooks directly in your module's main class, Hook Bus offers:
- Better separation of concerns.
- Easier testing.
- Cleaner module classes.
- Dependency Injection friendly handlers.
- Reusable hook logic.
The library is composed of three main building blocks:
- Identifier: determines the hook identity.
- Locator: resolves the Handler for that identity.
- Handler: contains the hook business logic.
Requirements
- PHP 5.6+
- PrestaShop 1.6.x / 1.7.x / 8.x / 9.x+
Installation
Install via Composer in your PrestaShop module:
Complete Example
A typical module structure could look like this:
Configure the Hook Bus once in the module constructor:
Each hook is implemented in its own Handler:
Factory
To make configuring and creating the Hook Bus easier, HookBusFactory provides static methods:
| Method | Best suited for |
|---|---|
create() |
Full control over the Identifier and Locator. |
createWithArray() |
Quick setup using an array of handlers. |
createWithCallable() |
Dynamic handler resolution. |
createWithContainer() |
Symfony service containers and Dependency Injection. |
HookBusFactory::create()
| Argument | Type | Description |
|---|---|---|
hookIdentifier |
HookIdentifierInterface |
The Identifier that will be used to locate the Handler. |
handlerLocator |
HandlerLocatorInterface |
The Locator where the Handlers are registered. |
Example
HookBusFactory::createWithArray()
| Argument | Type | Description |
|---|---|---|
hookIdentifier |
HookIdentifierInterface |
The Identifier that will be used to locate the Handler. |
handlers |
array |
An array with key representing the Identifier and value the Handler. If the handler implements NamedHandlerInterface, key is optional. |
Example
HookBusFactory::createWithCallable()
| Argument | Type | Description |
|---|---|---|
hookIdentifier |
HookIdentifierInterface |
The Identifier that will be used to locate the Handler. |
callable |
callable |
A callback that will resolve which Handler will be used. |
Example
HookBusFactory::createWithContainer()
| Argument | Type | Description |
|---|---|---|
container |
ContainerInterface |
Container where services are registered. |
hookIdentifier |
HookIdentifierInterface |
The Identifier that will be used to locate the Handler. |
handlers |
array |
An array with key representing the Identifier and value the service to use as Handler. If the handler implements NamedHandlerInterface, key is optional. |
Example
[!NOTE] Service container integration requires PrestaShop 1.7 or higher, as PrestaShop 1.6 does not use Symfony, or, use your own container, for example
prestashop/module-lib-service-container.
Identifier
Used to identify the hook and to locate the corresponding Handler during the dispatch cycle.
If the Identifier cannot resolve the identity, it will throw the exception UnresolvedHookIdentifierException.
Literal
The provided string will be used literally as the identifier.
Example
Method Hook
Designed to be used inside the module's public hook<hook_name>() methods. It obtains the identifier from the method name; for example, the method hookActionAdminControllerSetMedia() will be the identifier actionAdminControllerSetMedia.
Example
Callable
Through a callback we can generate a custom identifier.
Example
Custom Identifier
You can create your own Identifiers to generate custom identities. It should implement the interface HookIdentifierInterface.
Example
Locator
Where Handlers are located and which identities are associated with them.
If the Locator cannot resolve which Handler is assigned to the identity, it will throw the exception MissingHandlerException.
Array
Example
Callable
Example
Container
Example
Custom Locator
You can create your own Locators that return which Handler will manage the hook. It should implement the interface HandlerLocatorInterface.
If you want it to be compatible with the Factory, you should implement the interface AppendableHandlerLocatorInterface to enable the addHandler() method.
Example
Handler
Handlers are responsible for resolving what to do with the hook. They must implement the interface HookHandlerInterface.
The handle() method receives the $params argument, which is the same argument that the native hook<hook_name>() method of the module received.
If the Handler implements the interface NamedHandlerInterface, the Factory can infer the identity automatically from the static method getIdentityName().
Example
Exceptions
All library exceptions extend HookBusException.
| Exception | Description |
|---|---|
MissingHandlerException |
No Handler was found for the resolved identity. |
UnresolvedHookIdentifierException |
The Identifier could not resolve an identity. |