src/EventListener/LegacyBootstrapListener.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Application\Service\Api\Service\FileService;
  4. use App\Application\Service\Helper\ToolsService;
  5. use App\Application\Service\Session\SessionService;
  6. use App\Controller\LegacyController;
  7. use App\Entity\System\Catalog;
  8. use App\Service\EnvironmentService;
  9. use Request as LegacyRequest;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  14. use Symfony\Component\Routing\RouterInterface;
  15. class LegacyBootstrapListener
  16. {
  17.     public const CONTOLLERS_CHECKOUT = ['cartController''subscriptionController'];
  18.     public const METHOD_CHECKOUT = ['checkout''mobilecart'];
  19.     private RequestStack $requestStack;
  20.     private RouterInterface $router;
  21.     private EnvironmentService $environmentService;
  22.     private string $legacyDir;
  23.     private SessionService $sessionService;
  24.     private ToolsService $toolsService;
  25.     public function __construct(
  26.         RequestStack $requestStack,
  27.         RouterInterface $router,
  28.         EnvironmentService $environmentService,
  29.         FileService $fileService,
  30.         SessionService $sessionService,
  31.         ToolsService $toolsService
  32.     ) {
  33.         $this->requestStack $requestStack;
  34.         $this->router $router;
  35.         $this->legacyDir $fileService->getLegacyDir();
  36.         $this->environmentService $environmentService;
  37.         $this->sessionService $sessionService;
  38.         $this->toolsService $toolsService;
  39.     }
  40.     public function onKernelRequest(RequestEvent $event): void
  41.     {
  42.         if ($this->environmentService->isApi()) {
  43.             return;
  44.         }
  45.         $symfonyRequest $event->getRequest();
  46.         $refTapfiliate $symfonyRequest->query->get('ref');
  47.         $this->requestStack->getSession()->set('isCheckout'false);
  48.         try {
  49.             $pathInfo $this->router->match($event->getRequest()->getPathInfo());
  50.         } catch (ResourceNotFoundException $exception) {
  51.             return;
  52.         }
  53.         $this->defineLayoutsPath($event->getRequest()->getPathInfo());
  54.         if (
  55.             \array_key_exists('_route'$pathInfo)
  56.             && ($pathInfo['_route'] === 'legacy_front_endpoints'
  57.             || $pathInfo['_route'] === 'legacy_admin_endpoints')
  58.         ) {
  59.             $legacyRequest = new LegacyRequest(); // TODO move logic inside LegacyRequest here
  60.             $controller $legacyRequest->getControlador().LegacyRequest::DEFAULT_NAME_CONTROLLER;
  61.             $method $legacyRequest->getMetodo();
  62.             $args $legacyRequest->getArgumentos();
  63.             $this->sessionService->set('controller'$controller);
  64.             $this->sessionService->set('metodo'$method);
  65.             $this->sessionService->set('argumentos'$args);
  66.             $this->sessionService->set('url'$legacyRequest->getUrl());
  67.             $this->sessionService->delete('id_model');
  68.             if (!$this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY) || empty($this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY))) {
  69.                 $this->sessionService->set(SessionService::ALLOWED_CATALOGS_KEY, [Catalog::DEFAULT_CATALOG_BIGBUY_REFERENCE]);
  70.             }
  71.             $resolvedController $this->resolveLegacyController($legacyRequest$controller$method$args);
  72.             $this->saveGoogleAnalyticsCookie($legacyRequest$resolvedController['class']);
  73.             $symfonyRequestData $symfonyRequest->request->get('legacy_controller');
  74.             $symfonyRequest->request->set('legacy_controller', [
  75.                 'url' => $legacyRequest->getUrl(),
  76.                 'controller' => $resolvedController['class'],
  77.                 'method' => $resolvedController['method'],
  78.                 'args' => $resolvedController['args'],
  79.             ]);
  80.             if (
  81.                 $symfonyRequestData !== null
  82.                 && !isset($symfonyRequestData['ref'])
  83.                 && $refTapfiliate !== null
  84.             ) {
  85.                 $symfonyRequest->request->set('legacy_controller', [
  86.                     'url' => $legacyRequest->getUrl(),
  87.                     'controller' => $resolvedController['class'],
  88.                     'method' => $resolvedController['method'],
  89.                     'args' => $resolvedController['args'],
  90.                     'ref' => $refTapfiliate,
  91.                 ]);
  92.             }
  93.             $this->populateSfSessionFromSessionGlobal($symfonyRequest->getSession());
  94.             if (in_array($controllerself::CONTOLLERS_CHECKOUT) && in_array($methodself::METHOD_CHECKOUT)) {
  95.                 $this->requestStack->getSession()->set('isCheckout'true);
  96.             }
  97.         }
  98.     }
  99.     /**
  100.      * @param string $path
  101.      * @param string $controller
  102.      *
  103.      * @return string
  104.      */
  105.     private function getLegacyControllerPath(string $pathstring $controller): string
  106.     {
  107.         return $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$controller.'.php';
  108.     }
  109.     /**
  110.      * @param LegacyRequest $legacyRequest
  111.      * @param string $controller
  112.      * @param string $method
  113.      * @param array $args
  114.      *
  115.      * @return array
  116.      */
  117.     private function resolveLegacyController(LegacyRequest $legacyRequeststring $controllerstring $method, array $args): array
  118.     {
  119.         $path FRONT_DIR;
  120.         if ($legacyRequest->getAdmin()) {
  121.             $path ADMIN_DIR;
  122.         }
  123.         $controllerPath $this->getLegacyControllerPath($path$controller);
  124.         $realIP '';
  125.         if (isset($_SERVER['HTTP_X_REAL_IP'])) {
  126.             $realIP $_SERVER['HTTP_X_REAL_IP'];
  127.         } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  128.             $realIP $_SERVER['REMOTE_ADDR'];
  129.         }
  130.         if (!empty($args) && $legacyRequest->getService()) {
  131.             $args $this->sessionService->get('argumentos');
  132.         }
  133.         if (\in_array(
  134.             $controller,
  135.             [
  136.                 LegacyController::TAXONOMY_CONTROLLER,
  137.                 LegacyController::MANUFACTURER_CONTROLLER,
  138.                 LegacyController::TAG_CONTROLLER,
  139.                 LegacyController::PRODUCT_CONTROLLER,
  140.             ],
  141.             true
  142.         )) {
  143.             return [
  144.                 'class' => $controller,
  145.                 'method' => $method,
  146.                 'args' => $args,
  147.             ];
  148.         }
  149.         if (!is_callable([$controller$method]) || !is_readable($controllerPath)) {
  150.             return [
  151.                 'class' => LegacyRequest::NOT_FOUND_CONTROLLER,
  152.                 'method' => LegacyRequest::DEFAULT_METHOD,
  153.                 'args' => $args,
  154.             ];
  155.             //            throw new NotFoundHttpException('Not Found');
  156.         }
  157.         // Require requested controller and all the rest from the same folder (admin, front..) because in certain cases a controller calls another...
  158.         // Sort descending because of a single case (admin/sliderController must be loaded before admin/bannerController). So dirty, I know.
  159.         foreach (scandir($this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$pathSCANDIR_SORT_DESCENDING) as $filename) {
  160.             $controllerPath $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$path.'/'.$filename;
  161.             if (is_file($controllerPath) && strpos($filename'Controller')) {
  162.                 require_once $controllerPath;
  163.             }
  164.         }
  165.         return [
  166.             'class' => $controller,
  167.             'method' => $method,
  168.             'args' => $args,
  169.         ];
  170.     }
  171.     private function saveGoogleAnalyticsCookie(LegacyRequest $legacyRequeststring $class): void
  172.     {
  173.         if (
  174.             LegacyRequest::NOT_FOUND_CONTROLLER !== $class && LegacyRequest::MAINTENANCE_CONTROLLER !== $class
  175.             && $legacyRequest->getFront() && !\Tools::getValue('ajax')
  176.         ) {
  177.             \GACookie::SaveCookie();
  178.         }
  179.     }
  180.     private function populateSfSessionFromSessionGlobal(SessionInterface $session): void
  181.     {
  182.         foreach ($_SESSION as $key => $value) {
  183.             if ($key !== '_sf2_attributes') {
  184.                 $session->set($key$value);
  185.             }
  186.         }
  187.     }
  188.     private function defineLayoutsPath(string $route): void
  189.     {
  190.         $templateDir FRONT_DIR;
  191.         $isMobile $this->toolsService::isMobile();
  192.         if (preg_match('/^\/'.ADMIN_DIR.'\//'$route$m)) {
  193.             $templateDir ADMIN_DIR;
  194.             if (!defined('IS_ADMIN')) {
  195.                 define('IS_ADMIN''1');
  196.             }
  197.         }
  198.         if (!defined('LAYOUTS_PATH')) {
  199.             /** @phpstan-ignore-next-line  */
  200.             define('LAYOUTS_PATH', ($isMobile MOBILE_VIEWS.DIRECTORY_SEPARATOR '').$templateDir.DIRECTORY_SEPARATOR.'layout'.DIRECTORY_SEPARATOR);
  201.         }
  202.         if (!defined('PARTIALS_PATH')) {
  203.             /** @phpstan-ignore-next-line  */
  204.             define('PARTIALS_PATH', ($isMobile MOBILE_VIEWS.DIRECTORY_SEPARATOR '').$templateDir.DIRECTORY_SEPARATOR.'partial'.DIRECTORY_SEPARATOR);
  205.         }
  206.     }
  207. }