* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ namespace PrestaShopBundle\EventListener; use PrestaShop\PrestaShop\Core\Hook\HookDispatcherInterface; use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\FilterControllerEvent; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; /** * @todo Extract logic outside of EventSubscriber */ class ActionDispatcherLegacyHooksSubscriber implements EventSubscriberInterface { public const DISPATCHER_BEFORE_ACTION = 'actionDispatcherBefore'; public const DISPATCHER_AFTER_ACTION = 'actionDispatcherAfter'; /** * List of available front controllers types. */ public const FRONT_OFFICE_CONTROLLER = 1; public const BACK_OFFICE_CONTROLLER = 2; public const MODULE_CONTROLLER = 3; public const NA_CONTROLLER = 0; /** * @var HookDispatcherInterface */ private $hookDispatcher; public function __construct(HookDispatcherInterface $hookDispatcher) { $this->hookDispatcher = $hookDispatcher; } public static function getSubscribedEvents() { return [ KernelEvents::CONTROLLER => [ ['callActionDispatcherBeforeHook', 100], ], KernelEvents::RESPONSE => [ ['callActionDispatcherAfterHook', 255], ], ]; } public function callActionDispatcherBeforeHook(FilterControllerEvent $event) { if (!$event->isMasterRequest()) { return; } $requestAttributes = $event->getRequest()->attributes; $controllerType = self::NA_CONTROLLER; $controller = is_array($event->getController()) ? $event->getController()[0] : $event->getController() ; if ($controller instanceof FrameworkBundleAdminController) { $controllerType = self::BACK_OFFICE_CONTROLLER; } $this->hookDispatcher->dispatchWithParameters(self::DISPATCHER_BEFORE_ACTION, [ 'controller_type' => $controllerType, ]); $requestAttributes->set('controller_type', $controllerType); $requestAttributes->set('controller_name', get_class($controller)); } public function callActionDispatcherAfterHook(FilterResponseEvent $event) { if (!$event->isMasterRequest()) { return; } $requestAttributes = $event->getRequest()->attributes; if ($requestAttributes->has('controller_type') && $requestAttributes->has('controller_name')) { $this->hookDispatcher->dispatchWithParameters(self::DISPATCHER_AFTER_ACTION, [ 'controller_type' => $requestAttributes->get('controller_type'), 'controller_class' => $requestAttributes->get('controller_name'), 'is_module' => 0, ]); } } }