Skip to content

Commit 8d501ca

Browse files
authored
Merge pull request PrestaShop#37125 from matthieu-rolland/filter-module-hook
Make getHookModuleExecList customizable from modules
2 parents e8cbac5 + b806043 commit 8d501ca

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

classes/Hook.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
2525
*/
2626

27+
use PrestaShop\PrestaShop\Adapter\ContainerBuilder;
2728
use PrestaShop\PrestaShop\Adapter\LegacyLogger;
2829
use PrestaShop\PrestaShop\Adapter\ServiceLocator;
2930
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
3031
use PrestaShop\PrestaShop\Core\Exception\CoreException;
32+
use PrestaShop\PrestaShop\Core\Hook\HookModuleFilter;
3133
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
3234

3335
class HookCore extends ObjectModel
@@ -761,6 +763,12 @@ public static function getHookModuleExecList($hookName = null)
761763
}
762764
}
763765

766+
$hookModuleFilter = self::getHookModuleFilter();
767+
768+
if (!empty($hookModuleFilter) && !empty($modulesToInvoke)) {
769+
$modulesToInvoke = $hookModuleFilter->filterHookModuleExecList($modulesToInvoke, $hookName);
770+
}
771+
764772
return !empty($modulesToInvoke) ? $modulesToInvoke : false;
765773
}
766774

@@ -1109,6 +1117,27 @@ private static function getHookRegistry()
11091117
return null;
11101118
}
11111119

1120+
/**
1121+
* @return HookModuleFilter|null
1122+
* @throws \PrestaShop\PrestaShop\Core\Exception\ContainerNotFoundException
1123+
*/
1124+
private static function getHookModuleFilter()
1125+
{
1126+
$serviceContainer = SymfonyContainer::getInstance();
1127+
1128+
if (is_null($serviceContainer)) {
1129+
$serviceContainer = ContainerBuilder::getContainer('front', _PS_MODE_DEV_);
1130+
}
1131+
1132+
try {
1133+
$hookModuleFilter = $serviceContainer->get(HookModuleFilter::class);
1134+
} catch (Exception $e) {
1135+
return null;
1136+
}
1137+
1138+
return $hookModuleFilter;
1139+
}
1140+
11121141
/**
11131142
* Retrieves all modules registered to any hook, indexed by hok name.
11141143
*

config/services/common.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ services:
2525
PrestaShopBundle\DependencyInjection\RuntimeConstEnvVarProcessor:
2626
public: false
2727
tags: ['container.env_var_processor']
28+
29+
PrestaShop\PrestaShop\Core\Hook\HookModuleFilter:
30+
arguments:
31+
- !tagged core.hook_module_exec_filter

src/Core/Hook/HookModuleFilter.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <[email protected]>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
namespace PrestaShop\PrestaShop\Core\Hook;
28+
29+
/**
30+
* This service is responsible for filtering the list of modules for a given hook that is returned by
31+
* the getHookModuleExecList method from Hook.php. It is called at the very end of getHookModuleExecList.
32+
*
33+
* How to use it to filter a list of modules for a hook:
34+
*
35+
* In your module, create a service which implements the HookModuleFilterInterface and give it
36+
* the tag named core.hook_module_exec_filter. Then in your service, you can filter the list of modules
37+
* in the filterHookModuleExecList method, according to your own logic.
38+
*
39+
* Your service will automatically be sent in this class's constructor, and be used to filter the list of modules.
40+
*/
41+
class HookModuleFilter implements HookModuleFilterInterface
42+
{
43+
private $hookModuleFilters;
44+
45+
public function __construct(iterable $hookModuleFilters)
46+
{
47+
$this->hookModuleFilters = $hookModuleFilters;
48+
}
49+
50+
public function filterHookModuleExecList(array $modules, string $hookName): array
51+
{
52+
foreach ($this->hookModuleFilters as $hookModuleFilter) {
53+
$modules = $hookModuleFilter->filterHookModuleExecList($modules, $hookName);
54+
}
55+
56+
return $modules;
57+
}
58+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <[email protected]>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
namespace PrestaShop\PrestaShop\Core\Hook;
28+
29+
/**
30+
* This interface must be implemented by all services that will be used by the HookModuleFilter service.
31+
* See HookModuleFilter.php for more explanations.
32+
*/
33+
interface HookModuleFilterInterface
34+
{
35+
public function filterHookModuleExecList(array $modules, string $hookName): array;
36+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
imports:
22
- { resource: ./circuit_breaker.yml }
3+
4+
services:
5+
PrestaShop\PrestaShop\Core\Hook\HookModuleFilter:
6+
arguments:
7+
- !tagged core.hook_module_exec_filter

0 commit comments

Comments
 (0)