src/Controller/LegacyController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Application\Service\Helper\LinkGenerator;
  4. use App\Application\Service\Session\SessionService;
  5. use App\Service\LegacyControllerService;
  6. use Psr\Log\LoggerInterface;
  7. use Request as LegacyRequest;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class LegacyController extends AbstractController
  13. {
  14.     public const TAXONOMY_CONTROLLER 'taxonomyController';
  15.     public const MANUFACTURER_CONTROLLER 'manufacturerController';
  16.     public const PRODUCT_CONTROLLER 'productController';
  17.     public const TAG_CONTROLLER 'tagController';
  18.     private LoggerInterface $logger;
  19.     private SessionService $sessionService;
  20.     private LegacyControllerService $legacyControllerService;
  21.     private LinkGenerator $linkGenerator;
  22.     public function __construct(
  23.         LoggerInterface $logger,
  24.         SessionService $sessionService,
  25.         LegacyControllerService $legacyControllerService,
  26.         LinkGenerator $linkGenerator
  27.     ) {
  28.         $this->logger $logger;
  29.         $this->sessionService $sessionService;
  30.         $this->legacyControllerService $legacyControllerService;
  31.         $this->linkGenerator $linkGenerator;
  32.     }
  33.     /**
  34.      * @Route("/{anything}", name="legacy_front_endpoints", priority=-5, requirements={"anything"="^((?!rest/).)*$"})
  35.      */
  36.     public function legacyFrontEndpoints(Request $requeststring $anything): Response
  37.     {
  38.         /** @var array $controllerData */
  39.         $controllerData $request->request->get('legacy_controller');
  40.         if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
  41.             throw $this->createNotFoundException();
  42.         }
  43.         if ($newRoute $this->redirectToNewRoutes(
  44.             $controllerData['controller'],
  45.             reset($controllerData['args'])
  46.         )) {
  47.             return $this->redirect($newRouteResponse::HTTP_MOVED_PERMANENTLY);
  48.         }
  49.         $legacyController null;
  50.         $parameter $this->sessionService->get('id_model') ?? reset($controllerData['args']) ?? null;
  51.         try {
  52.             if ($parameter) {
  53.                 $legacyController = new $controllerData['controller']($parameter);
  54.             } else {
  55.                 $legacyController = new $controllerData['controller']();
  56.             }
  57.         } catch (\Throwable $t) {
  58.             $this->logger->critical('Error loading legacy controller: '.$anything.' '.$t->getMessage());
  59.         }
  60.         if (!method_exists($legacyController$controllerData['method'])) {
  61.             throw $this->createNotFoundException();
  62.         }
  63.         call_user_func_array([$legacyController$controllerData['method']], $controllerData['args']);
  64.         return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
  65.     }
  66.     /**
  67.      * @param int|string $parameter
  68.      */
  69.     protected function redirectToNewRoutes(string $controller$parameter): ?string
  70.     {
  71.         switch ($controller) {
  72.             case self::TAXONOMY_CONTROLLER:
  73.                 return $this->linkGenerator->getTaxonomyLink($parameter);
  74.             case self::MANUFACTURER_CONTROLLER:
  75.                 return $this->linkGenerator->getManufacturerLink($parameter);
  76.             case self::PRODUCT_CONTROLLER:
  77.                 return $this->linkGenerator->getProductLink($parameter);
  78.             case self::TAG_CONTROLLER:
  79.                 return $this->generateUrl('shop_tag', ['url' => $parameter'lang' => $this->sessionService->getLocale()]);
  80.         }
  81.         return null;
  82.     }
  83. }