src/Controller/LegacyController.php line 123

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 App\ViewManager\Landing\CmsService;
  7. use App\ViewManager\Landing\LandingService;
  8. use Psr\Log\LoggerInterface;
  9. use Request as LegacyRequest;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class LegacyController extends AbstractController
  16. {
  17.     private LoggerInterface $logger;
  18.     private SessionService $sessionService;
  19.     private LinkGenerator $linkGenerator;
  20.     private LandingService $landingService;
  21.     private CmsService $cmsService;
  22.     private LegacyControllerService $legacyControllerService;
  23.     public function __construct(
  24.         LoggerInterface $logger,
  25.         SessionService $sessionService,
  26.         LinkGenerator $linkGenerator,
  27.         LandingService $landingService,
  28.         CmsService $cmsService,
  29.         LegacyControllerService $legacyControllerService
  30.     ) {
  31.         $this->logger $logger;
  32.         $this->sessionService $sessionService;
  33.         $this->linkGenerator $linkGenerator;
  34.         $this->landingService $landingService;
  35.         $this->cmsService $cmsService;
  36.         $this->legacyControllerService $legacyControllerService;
  37.     }
  38.     /**
  39.      * @Route("/admin{anything}", name="legacy_admin_endpoints", priority="-3", requirements={"anything"=".*"})
  40.      */
  41.     public function legacyNotFrontEndpoints(Request $request): Response
  42.     {
  43.         $this->sessionService->set('cms_new'false);
  44.         /** @var array $controllerData */
  45.         $controllerData $request->request->get('legacy_controller');
  46.         if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
  47.             throw $this->createNotFoundException();
  48.         }
  49.         if (LegacyRequest::MAINTENANCE_CONTROLLER === $controllerData['controller']) {
  50.             throw new ServiceUnavailableHttpException();
  51.         }
  52.         $legacyController = new $controllerData['controller']();
  53.         if (!method_exists($legacyController$controllerData['method'])) {
  54.             throw $this->createNotFoundException();
  55.         }
  56.         call_user_func_array([$legacyController$controllerData['method']], $controllerData['args']);
  57.         if (empty($legacyController->getView())) {
  58.             $this->sessionService->deleteFlashData();
  59.             return new Response('');
  60.             // throw $this->createNotFoundException();
  61.         }
  62.         $html $legacyController->display();
  63.         $this->sessionService->deleteFlashData();
  64.         return new Response($html);
  65.     }
  66.     /**
  67.      * @Route("/{anything}", name="legacy_front_endpoints", priority=-5, requirements={"anything"="^((?!rest/).)*$"})
  68.      */
  69.     public function legacyFrontEndpoints(Request $requeststring $anything): Response
  70.     {
  71.         $cmsLanguage $this->cmsService->getCmsInformation($anything);
  72.         $this->sessionService->set('cms_new'false);
  73.         if ($cmsLanguage && !$cmsLanguage->getCms()->getLegacy()) {
  74.             $this->sessionService->set('id_model'$cmsLanguage->getCms()->getId());
  75.             $this->sessionService->set('cms_new'true);
  76.             return $this->render(
  77.                 $this->landingService->getLandingBaseTemplate($cmsLanguage),
  78.                 $this->landingService->getParametersForBaseCms($cmsLanguage)
  79.             );
  80.         }
  81.         /** @var array $controllerData */
  82.         $controllerData $request->request->get('legacy_controller');
  83.         if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
  84.             throw $this->createNotFoundException();
  85.         }
  86.         if ($newRoute $this->redirectToNewRoutes(
  87.             $controllerData['controller'],
  88.             reset($controllerData['args'])
  89.         )) {
  90.             return $this->redirect($newRouteResponse::HTTP_MOVED_PERMANENTLY);
  91.         }
  92.         $legacyController null;
  93.         $parameter $this->sessionService->get('id_model') ?? reset($controllerData['args']) ?? null;
  94.         try {
  95.             if ($parameter) {
  96.                 $legacyController = new $controllerData['controller']($parameter);
  97.             } else {
  98.                 $legacyController = new $controllerData['controller']();
  99.             }
  100.         } catch (\Throwable $t) {
  101.             $this->logger->critical('Error loading legacy controller: '.$anything.' '.$t->getMessage());
  102.         }
  103.         if (!method_exists($legacyController$controllerData['method'])) {
  104.             throw $this->createNotFoundException();
  105.         }
  106.         call_user_func_array([$legacyController$controllerData['method']], $controllerData['args']);
  107.         return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
  108.     }
  109.     /**
  110.      * @param int|string $parameter
  111.      */
  112.     protected function redirectToNewRoutes(string $controller$parameter): ?string
  113.     {
  114.         switch ($controller) {
  115.             case \taxonomyController::class:
  116.                 return $this->linkGenerator->getTaxonomyLink($parameter);
  117.             case \manufacturerController::class:
  118.                 return $this->linkGenerator->getManufacturerLink($parameter);
  119.             case \productController::class:
  120.                 return $this->linkGenerator->getProductLink($parameter);
  121.             case \tagController::class:
  122.                 return $this->generateUrl('shop_tag', ['url' => $parameter'lang' => $this->sessionService->getLocale()]);
  123.         }
  124.         return null;
  125.     }
  126. }