<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rubenmartindev / prestashop-module-hook-bus example snippets
use RubenMartinDev\PrestashopModuleHookBus\HookBusFactory;
use RubenMartinDev\PrestashopModuleHookBus\HookBusInterface;
use RubenMartinDev\PrestashopModuleHookBus\Identifier\MethodHookIdentifier;
class MyModule extends Module
{
/** @var HookBusInterface */
private $hookBus;
public function __construct()
{
// ...
$this->hookBus = HookBusFactory::createWithArray(
new MethodHookIdentifier(),
[
new DisplayHeaderHandler(),
new DisplayFooterHandler(),
]
);
}
public function hookDisplayHeader(array $params)
{
return $this->hookBus->dispatch(__FUNCTION__, $params);
}
public function hookDisplayFooter(array $params)
{
return $this->hookBus->dispatch(__FUNCTION__, $params);
}
}
use RubenMartinDev\PrestashopModuleHookBus\Handler\NamedHandlerInterface;
final class DisplayHeaderHandler implements NamedHandlerInterface
{
public static function getIdentityName()
{
return 'displayHeader';
}
public function handle(array $params = [])
{
// Hook logic here
}
}
use RubenMartinDev\PrestashopModuleHookBus\HookBusFactory;
use RubenMartinDev\PrestashopModuleHookBus\Identifier\LiteralIdentifier;
use RubenMartinDev\PrestashopModuleHookBus\Locator\ArrayLocator;
$arrayLocator = new ArrayLocator();
$arrayLocator->addHandler('displayHeader', new DisplayHeaderHandler());
HookBusFactory::create(
new LiteralIdentifier(),
$arrayLocator
);
use RubenMartinDev\PrestashopModuleHookBus\HookBusFactory;
use RubenMartinDev\PrestashopModuleHookBus\Identifier\LiteralIdentifier;
HookBusFactory::createWithArray(
new LiteralIdentifier(),
['displayHeader' => new DisplayHeaderHandler()]
);
use RubenMartinDev\PrestashopModuleHookBus\HookBusFactory;
use RubenMartinDev\PrestashopModuleHookBus\Identifier\LiteralIdentifier;
HookBusFactory::createWithCallable(
new LiteralIdentifier(),
function ($identifier) {
if ('displayHeader' === $identifier) {
return new DisplayHeaderHandler();
}
}
);
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use RubenMartinDev\PrestashopModuleHookBus\HookBusFactory;
use RubenMartinDev\PrestashopModuleHookBus\Identifier\LiteralIdentifier;
$container = SymfonyContainer::getInstance();
HookBusFactory::createWithContainer(
$container,
new LiteralIdentifier(),
['displayHeader' => 'my_module.hook.handler.display_header']
);
use RubenMartinDev\PrestashopModuleHookBus\HookBusFactory;
use RubenMartinDev\PrestashopModuleHookBus\Identifier\LiteralIdentifier;
class MyModule extends Module
{
public function __construct()
{
// ...
$this->hookBus = HookBusFactory::createWithArray(
new LiteralIdentifier(),
// ...
);
}
public function hookDisplayHeader(array $params)
{
return $this->hookBus->dispatch('myCustomIdentity', $params);
}
}
use RubenMartinDev\PrestashopModuleHookBus\HookBusFactory;
use RubenMartinDev\PrestashopModuleHookBus\Identifier\MethodHookIdentifier;
class MyModule extends Module
{
public function __construct()
{
// ...
$this->hookBus = HookBusFactory::createWithArray(
new MethodHookIdentifier(),
// ...
);
}
public function hookDisplayHeader(array $params)
{
return $this->hookBus->dispatch(__FUNCTION__, $params);
}
}
use RubenMartinDev\PrestashopModuleHookBus\HookBusFactory;
use RubenMartinDev\PrestashopModuleHookBus\Identifier\CallableIdentifier;
class MyModule extends Module
{
public function __construct()
{
// ...
$this->hookBus = HookBusFactory::createWithArray(
new CallableIdentifier(function ($hookName) {
if ('myCustomIdentity' === $hookName) {
return 'myCustomIdentityHandler';
}
}),
// ...
);
}
public function hookDisplayHeader(array $params)
{
return $this->hookBus->dispatch('myCustomIdentity', $params);
}
}
use RubenMartinDev\PrestashopModuleHookBus\Identifier\HookIdentifierInterface;
final class MyCustomIdentifier implements HookIdentifierInterface
{
public function identify($hookName)
{
return \strtolower($hookName);
}
}
use RubenMartinDev\PrestashopModuleHookBus\Locator\ArrayLocator;
$locator = new ArrayLocator();
$locator->addHandler('displayHeader', new DisplayHeaderHandler());
$locator->addHandler('displayFooter', new DisplayFooterHandler());
use RubenMartinDev\PrestashopModuleHookBus\Locator\CallableLocator;
$locator = new CallableLocator(function ($hookName) {
if ('displayHeader' === $hookName) {
return new DisplayHeaderHandler();
}
if ('displayFooter' === $hookName) {
return new DisplayFooterHandler();
}
return null;
});
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use RubenMartinDev\PrestashopModuleHookBus\Locator\ContainerLocator;
$container = SymfonyContainer::getInstance();
$locator = new ContainerLocator($container);
$locator->addHandler('displayHeader', 'my_module.hook.handler.display_header');
$locator->addHandler('displayFooter', 'my_module.hook.handler.display_footer');
use RubenMartinDev\PrestashopModuleHookBus\Locator\HandlerLocatorInterface;
final class MyCustomLocator implements HandlerLocatorInterface
{
public function getHandlerForIdentity($identity)
{
if ('displayHeader' === $identity) {
return new DisplayHeaderHandler();
}
if ('displayFooter' === $identity) {
return new DisplayFooterHandler();
}
throw MissingHandlerException::forIdentity($identity);
}
}
use RubenMartinDev\PrestashopModuleHookBus\Handler\NamedHandlerInterface;
final class DisplayHeaderHandler implements NamedHandlerInterface
{
public static function getIdentityName()
{
return 'displayHeader';
}
public function handle(array $params = [])
{
$cartTotal = $params['cart']->getOrderTotal();
return Tools::displayPrice($cartTotal);
}
}