src/EventListener/LegacyBootstrapListener.php line 126

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\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.         $isMobile $this->toolsService::isMobile();
  53.         if (!defined('LAYOUTS_PATH')) {
  54.             /** @phpstan-ignore-next-line  */
  55.             define('LAYOUTS_PATH', ($isMobile 'mobile'.DIRECTORY_SEPARATOR '').'front'.DIRECTORY_SEPARATOR.'layout'.DIRECTORY_SEPARATOR);
  56.         }
  57.         if (!defined('PARTIALS_PATH')) {
  58.             /** @phpstan-ignore-next-line  */
  59.             define('PARTIALS_PATH', ($isMobile 'mobile'.DIRECTORY_SEPARATOR '').'front'.DIRECTORY_SEPARATOR.'partial'.DIRECTORY_SEPARATOR);
  60.         }
  61.         if (\array_key_exists('_route'$pathInfo) && ($pathInfo['_route'] === 'legacy_front_endpoints')) {
  62.             $legacyRequest = new LegacyRequest(); // TODO move logic inside LegacyRequest here
  63.             $controller $legacyRequest->getControlador().LegacyRequest::DEFAULT_NAME_CONTROLLER;
  64.             $method $legacyRequest->getMetodo();
  65.             $args $legacyRequest->getArgumentos();
  66.             $this->sessionService->set('controller'$controller);
  67.             $this->sessionService->set('metodo'$method);
  68.             $this->sessionService->set('argumentos'$args);
  69.             $this->sessionService->set('url'$legacyRequest->getUrl());
  70.             $this->sessionService->delete('id_model');
  71.             if (!$this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY) || empty($this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY))) {
  72.                 $this->sessionService->set(SessionService::ALLOWED_CATALOGS_KEY, [Catalog::DEFAULT_CATALOG_BIGBUY_REFERENCE]);
  73.             }
  74.             $resolvedController $this->resolveLegacyController($controller$method$args);
  75.             if (LegacyRequest::NOT_FOUND_CONTROLLER !== $resolvedController['class'] && !\Tools::getValue('ajax')) {
  76.                 \GACookie::SaveCookie();
  77.             }
  78.             $symfonyRequestData $symfonyRequest->request->get('legacy_controller');
  79.             $symfonyRequest->request->set('legacy_controller', [
  80.                 'url' => $legacyRequest->getUrl(),
  81.                 'controller' => $resolvedController['class'],
  82.                 'method' => $resolvedController['method'],
  83.                 'args' => $resolvedController['args'],
  84.             ]);
  85.             if (
  86.                 $symfonyRequestData !== null
  87.                 && !isset($symfonyRequestData['ref'])
  88.                 && $refTapfiliate !== null
  89.             ) {
  90.                 $symfonyRequest->request->set('legacy_controller', [
  91.                     'url' => $legacyRequest->getUrl(),
  92.                     'controller' => $resolvedController['class'],
  93.                     'method' => $resolvedController['method'],
  94.                     'args' => $resolvedController['args'],
  95.                     'ref' => $refTapfiliate,
  96.                 ]);
  97.             }
  98.             foreach ($_SESSION as $key => $value) {
  99.                 if ($key !== '_sf2_attributes') {
  100.                     $symfonyRequest->getSession()->set($key$value);
  101.                 }
  102.             }
  103.             if (in_array($controllerself::CONTOLLERS_CHECKOUT) && in_array($methodself::METHOD_CHECKOUT)) {
  104.                 $this->requestStack->getSession()->set('isCheckout'true);
  105.             }
  106.         }
  107.     }
  108.     private function resolveLegacyController(string $controllerstring $method, array $args): array
  109.     {
  110.         $controllerPath $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.'front'.DIRECTORY_SEPARATOR.$controller.'.php';
  111.         $realIP '';
  112.         if (isset($_SERVER['HTTP_X_REAL_IP'])) {
  113.             $realIP $_SERVER['HTTP_X_REAL_IP'];
  114.         } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  115.             $realIP $_SERVER['REMOTE_ADDR'];
  116.         }
  117.         if (\in_array(
  118.             $controller,
  119.             [
  120.                 LegacyController::TAXONOMY_CONTROLLER,
  121.                 LegacyController::MANUFACTURER_CONTROLLER,
  122.                 LegacyController::TAG_CONTROLLER,
  123.                 LegacyController::PRODUCT_CONTROLLER,
  124.             ],
  125.             true
  126.         )) {
  127.             return [
  128.                 'class' => $controller,
  129.                 'method' => $method,
  130.                 'args' => $args,
  131.             ];
  132.         }
  133.         if (!is_callable([$controller$method]) || !is_readable($controllerPath)) {
  134.             return [
  135.                 'class' => LegacyRequest::NOT_FOUND_CONTROLLER,
  136.                 'method' => LegacyRequest::DEFAULT_METHOD,
  137.                 'args' => $args,
  138.             ];
  139.             //            throw new NotFoundHttpException('Not Found');
  140.         }
  141.         // Require requested controller and all the rest from the same folder (admin, front..) because in certain cases a controller calls another...
  142.         // Sort descending because of a single case (admin/sliderController must be loaded before admin/bannerController). So dirty, I know.
  143.         foreach (scandir($this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.'front'SCANDIR_SORT_DESCENDING) as $filename) {
  144.             $controllerPath $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.'front/'.$filename;
  145.             if (is_file($controllerPath) && strpos($filename'Controller')) {
  146.                 require_once $controllerPath;
  147.             }
  148.         }
  149.         return [
  150.             'class' => $controller,
  151.             'method' => $method,
  152.             'args' => $args,
  153.         ];
  154.     }
  155. }