Skip to content

Commit

Permalink
Merge pull request PrestaShop#37125 from matthieu-rolland/filter-modu…
Browse files Browse the repository at this point in the history
…le-hook

Make getHookModuleExecList customizable from modules
  • Loading branch information
nicosomb authored Dec 17, 2024
2 parents e8cbac5 + b806043 commit 8d501ca
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
29 changes: 29 additions & 0 deletions classes/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

use PrestaShop\PrestaShop\Adapter\ContainerBuilder;
use PrestaShop\PrestaShop\Adapter\LegacyLogger;
use PrestaShop\PrestaShop\Adapter\ServiceLocator;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Exception\CoreException;
use PrestaShop\PrestaShop\Core\Hook\HookModuleFilter;
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

class HookCore extends ObjectModel
Expand Down Expand Up @@ -761,6 +763,12 @@ public static function getHookModuleExecList($hookName = null)
}
}

$hookModuleFilter = self::getHookModuleFilter();

if (!empty($hookModuleFilter) && !empty($modulesToInvoke)) {
$modulesToInvoke = $hookModuleFilter->filterHookModuleExecList($modulesToInvoke, $hookName);
}

return !empty($modulesToInvoke) ? $modulesToInvoke : false;
}

Expand Down Expand Up @@ -1109,6 +1117,27 @@ private static function getHookRegistry()
return null;
}

/**
* @return HookModuleFilter|null
* @throws \PrestaShop\PrestaShop\Core\Exception\ContainerNotFoundException
*/
private static function getHookModuleFilter()
{
$serviceContainer = SymfonyContainer::getInstance();

if (is_null($serviceContainer)) {
$serviceContainer = ContainerBuilder::getContainer('front', _PS_MODE_DEV_);
}

try {
$hookModuleFilter = $serviceContainer->get(HookModuleFilter::class);
} catch (Exception $e) {
return null;
}

return $hookModuleFilter;
}

/**
* Retrieves all modules registered to any hook, indexed by hok name.
*
Expand Down
4 changes: 4 additions & 0 deletions config/services/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ services:
PrestaShopBundle\DependencyInjection\RuntimeConstEnvVarProcessor:
public: false
tags: ['container.env_var_processor']

PrestaShop\PrestaShop\Core\Hook\HookModuleFilter:
arguments:
- !tagged core.hook_module_exec_filter
58 changes: 58 additions & 0 deletions src/Core/Hook/HookModuleFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Hook;

/**
* This service is responsible for filtering the list of modules for a given hook that is returned by
* the getHookModuleExecList method from Hook.php. It is called at the very end of getHookModuleExecList.
*
* How to use it to filter a list of modules for a hook:
*
* In your module, create a service which implements the HookModuleFilterInterface and give it
* the tag named core.hook_module_exec_filter. Then in your service, you can filter the list of modules
* in the filterHookModuleExecList method, according to your own logic.
*
* Your service will automatically be sent in this class's constructor, and be used to filter the list of modules.
*/
class HookModuleFilter implements HookModuleFilterInterface
{
private $hookModuleFilters;

public function __construct(iterable $hookModuleFilters)
{
$this->hookModuleFilters = $hookModuleFilters;
}

public function filterHookModuleExecList(array $modules, string $hookName): array
{
foreach ($this->hookModuleFilters as $hookModuleFilter) {
$modules = $hookModuleFilter->filterHookModuleExecList($modules, $hookName);
}

return $modules;
}
}
36 changes: 36 additions & 0 deletions src/Core/Hook/HookModuleFilterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Hook;

/**
* This interface must be implemented by all services that will be used by the HookModuleFilter service.
* See HookModuleFilter.php for more explanations.
*/
interface HookModuleFilterInterface
{
public function filterHookModuleExecList(array $modules, string $hookName): array;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
imports:
- { resource: ./circuit_breaker.yml }

services:
PrestaShop\PrestaShop\Core\Hook\HookModuleFilter:
arguments:
- !tagged core.hook_module_exec_filter

0 comments on commit 8d501ca

Please sign in to comment.