src/EventListener/LegacyBootstrapListener.php line 197

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.             $this->sessionService->delete('id_model');
  67.             if (!$this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY) || empty($this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY))) {
  68.                 $this->sessionService->set(SessionService::ALLOWED_CATALOGS_KEY, [Catalog::DEFAULT_CATALOG_BIGBUY_REFERENCE]);
  69.             }
  70.             $resolvedController $this->resolveLegacyController($legacyRequest$controller$method$args);
  71.             $this->saveGoogleAnalyticsCookie($legacyRequest$resolvedController['class']);
  72.             $symfonyRequestData $symfonyRequest->request->get('legacy_controller');
  73.             $symfonyRequest->request->set('legacy_controller', [
  74.                 'url' => $legacyRequest->getUrl(),
  75.                 'controller' => $resolvedController['class'],
  76.                 'method' => $resolvedController['method'],
  77.                 'args' => $resolvedController['args'],
  78.             ]);
  79.             if (
  80.                 $symfonyRequestData !== null
  81.                 && !isset($symfonyRequestData['ref'])
  82.                 && $refTapfiliate !== null
  83.             ) {
  84.                 $symfonyRequest->request->set('legacy_controller', [
  85.                     'url' => $legacyRequest->getUrl(),
  86.                     'controller' => $resolvedController['class'],
  87.                     'method' => $resolvedController['method'],
  88.                     'args' => $resolvedController['args'],
  89.                     'ref' => $refTapfiliate,
  90.                 ]);
  91.             }
  92.             $this->populateSfSessionFromSessionGlobal($symfonyRequest->getSession());
  93.             if (in_array($controllerself::CONTOLLERS_CHECKOUT) && in_array($methodself::METHOD_CHECKOUT)) {
  94.                 $this->requestStack->getSession()->set('isCheckout'true);
  95.             }
  96.         }
  97.     }
  98.     /**
  99.      * @param string $path
  100.      * @param string $controller
  101.      *
  102.      * @return string
  103.      */
  104.     private function getLegacyControllerPath(string $pathstring $controller): string
  105.     {
  106.         return $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$controller.'.php';
  107.     }
  108.     /**
  109.      * @param LegacyRequest $legacyRequest
  110.      * @param string $controller
  111.      * @param string $method
  112.      * @param array $args
  113.      *
  114.      * @return array
  115.      */
  116.     private function resolveLegacyController(LegacyRequest $legacyRequeststring $controllerstring $method, array $args): array
  117.     {
  118.         if ($legacyRequest->getAdmin()) {
  119.             $path ADMIN_DIR;
  120.         } elseif ($legacyRequest->getService()) {
  121.             $path SERVICE_DIR;
  122.         } else {
  123.             $path FRONT_DIR;
  124.         }
  125.         $controllerPath $this->getLegacyControllerPath($path$controller);
  126.         $realIP '';
  127.         if (isset($_SERVER['HTTP_X_REAL_IP'])) {
  128.             $realIP $_SERVER['HTTP_X_REAL_IP'];
  129.         } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  130.             $realIP $_SERVER['REMOTE_ADDR'];
  131.         }
  132.         if (!empty($args) && $legacyRequest->getService()) {
  133.             $args $this->sessionService->get('argumentos');
  134.         }
  135.         if (!is_callable([$controller$method]) || !is_readable($controllerPath)) {
  136.             return [
  137.                 'class' => LegacyRequest::NOT_FOUND_CONTROLLER,
  138.                 'method' => LegacyRequest::DEFAULT_METHOD,
  139.                 'args' => $args,
  140.             ];
  141.             //            throw new NotFoundHttpException('Not Found');
  142.         }
  143.         // Require requested controller and all the rest from the same folder (service, admin, front..) because in certain cases a controller calls another...
  144.         // Sort descending because of a single case (admin/sliderController must be loaded before admin/bannerController). So dirty, I know.
  145.         foreach (scandir($this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$pathSCANDIR_SORT_DESCENDING) as $filename) {
  146.             $controllerPath $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$path.'/'.$filename;
  147.             if (is_file($controllerPath) && strpos($filename'Controller')) {
  148.                 require_once $controllerPath;
  149.             }
  150.         }
  151.         return [
  152.             'class' => $controller,
  153.             'method' => $method,
  154.             'args' => $args,
  155.         ];
  156.     }
  157.     private function saveGoogleAnalyticsCookie(LegacyRequest $legacyRequeststring $class): void
  158.     {
  159.         if (
  160.             LegacyRequest::NOT_FOUND_CONTROLLER !== $class && LegacyRequest::MAINTENANCE_CONTROLLER !== $class
  161.             && $legacyRequest->getFront() && !\Tools::getValue('ajax')
  162.         ) {
  163.             \GACookie::SaveCookie();
  164.         }
  165.     }
  166.     private function populateSfSessionFromSessionGlobal(SessionInterface $session): void
  167.     {
  168.         foreach ($_SESSION as $key => $value) {
  169.             if ($key !== '_sf2_attributes') {
  170.                 $session->set($key$value);
  171.             }
  172.         }
  173.     }
  174.     private function defineLayoutsPath(string $route): void
  175.     {
  176.         $templateDir FRONT_DIR;
  177.         $isMobile $this->toolsService::isMobile();
  178.         if (preg_match('/^\/'.ADMIN_DIR.'\//'$route$m)) {
  179.             $templateDir ADMIN_DIR;
  180.             if (!defined('IS_ADMIN')) {
  181.                 define('IS_ADMIN''1');
  182.             }
  183.         } // SI ESTAMOS EN SERVICE
  184.         elseif (preg_match('/^\/'.SERVICE_DIR.'\//'$route$m)) {
  185.             $templateDir SERVICE_DIR;
  186.             if (!defined('IS_SERVICE')) {
  187.                 define('IS_SERVICE''1');
  188.             }
  189.         }
  190.         if (!defined('LAYOUTS_PATH')) {
  191.             /** @phpstan-ignore-next-line  */
  192.             define('LAYOUTS_PATH', ($isMobile MOBILE_VIEWS.DIRECTORY_SEPARATOR '').$templateDir.DIRECTORY_SEPARATOR.'layout'.DIRECTORY_SEPARATOR);
  193.         }
  194.         if (!defined('PARTIALS_PATH')) {
  195.             /** @phpstan-ignore-next-line  */
  196.             define('PARTIALS_PATH', ($isMobile MOBILE_VIEWS.DIRECTORY_SEPARATOR '').$templateDir.DIRECTORY_SEPARATOR.'partial'.DIRECTORY_SEPARATOR);
  197.         }
  198.     }
  199. }