src/EventListener/LegacyBootstrapListener.php line 52

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