<?php
namespace App\EventListener;
use App\Application\Service\Api\Service\FileService;
use App\Application\Service\Helper\ToolsService;
use App\Application\Service\Session\SessionService;
use App\Controller\LegacyController;
use App\Entity\System\Catalog;
use App\Service\EnvironmentService;
use Request as LegacyRequest;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RouterInterface;
class LegacyBootstrapListener
{
public const CONTOLLERS_CHECKOUT = ['cartController', 'subscriptionController'];
public const METHOD_CHECKOUT = ['checkout', 'mobilecart'];
private RequestStack $requestStack;
private RouterInterface $router;
private EnvironmentService $environmentService;
private string $legacyDir;
private SessionService $sessionService;
private ToolsService $toolsService;
public function __construct(
RequestStack $requestStack,
RouterInterface $router,
EnvironmentService $environmentService,
FileService $fileService,
SessionService $sessionService,
ToolsService $toolsService
) {
$this->requestStack = $requestStack;
$this->router = $router;
$this->legacyDir = $fileService->getLegacyDir();
$this->environmentService = $environmentService;
$this->sessionService = $sessionService;
$this->toolsService = $toolsService;
}
public function onKernelRequest(RequestEvent $event): void
{
if ($this->environmentService->isApi()) {
return;
}
$symfonyRequest = $event->getRequest();
$refTapfiliate = $symfonyRequest->query->get('ref');
$this->requestStack->getSession()->set('isCheckout', false);
try {
$pathInfo = $this->router->match($event->getRequest()->getPathInfo());
} catch (ResourceNotFoundException $exception) {
return;
}
$isMobile = $this->toolsService::isMobile();
if (!defined('LAYOUTS_PATH')) {
/** @phpstan-ignore-next-line */
define('LAYOUTS_PATH', ($isMobile ? 'mobile'.DIRECTORY_SEPARATOR : '').'front'.DIRECTORY_SEPARATOR.'layout'.DIRECTORY_SEPARATOR);
}
if (!defined('PARTIALS_PATH')) {
/** @phpstan-ignore-next-line */
define('PARTIALS_PATH', ($isMobile ? 'mobile'.DIRECTORY_SEPARATOR : '').'front'.DIRECTORY_SEPARATOR.'partial'.DIRECTORY_SEPARATOR);
}
if (\array_key_exists('_route', $pathInfo) && ($pathInfo['_route'] === 'legacy_front_endpoints')) {
$legacyRequest = new LegacyRequest(); // TODO move logic inside LegacyRequest here
$controller = $legacyRequest->getControlador().LegacyRequest::DEFAULT_NAME_CONTROLLER;
$method = $legacyRequest->getMetodo();
$args = $legacyRequest->getArgumentos();
$this->sessionService->set('controller', $controller);
$this->sessionService->set('metodo', $method);
$this->sessionService->set('argumentos', $args);
$this->sessionService->set('url', $legacyRequest->getUrl());
$this->sessionService->delete('id_model');
if (!$this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY) || empty($this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY))) {
$this->sessionService->set(SessionService::ALLOWED_CATALOGS_KEY, [Catalog::DEFAULT_CATALOG_BIGBUY_REFERENCE]);
}
$resolvedController = $this->resolveLegacyController($controller, $method, $args);
if (LegacyRequest::NOT_FOUND_CONTROLLER !== $resolvedController['class'] && !\Tools::getValue('ajax')) {
\GACookie::SaveCookie();
}
$symfonyRequestData = $symfonyRequest->request->get('legacy_controller');
$symfonyRequest->request->set('legacy_controller', [
'url' => $legacyRequest->getUrl(),
'controller' => $resolvedController['class'],
'method' => $resolvedController['method'],
'args' => $resolvedController['args'],
]);
if (
$symfonyRequestData !== null
&& !isset($symfonyRequestData['ref'])
&& $refTapfiliate !== null
) {
$symfonyRequest->request->set('legacy_controller', [
'url' => $legacyRequest->getUrl(),
'controller' => $resolvedController['class'],
'method' => $resolvedController['method'],
'args' => $resolvedController['args'],
'ref' => $refTapfiliate,
]);
}
foreach ($_SESSION as $key => $value) {
if ($key !== '_sf2_attributes') {
$symfonyRequest->getSession()->set($key, $value);
}
}
if (in_array($controller, self::CONTOLLERS_CHECKOUT) && in_array($method, self::METHOD_CHECKOUT)) {
$this->requestStack->getSession()->set('isCheckout', true);
}
}
}
private function resolveLegacyController(string $controller, string $method, array $args): array
{
$controllerPath = $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.'front'.DIRECTORY_SEPARATOR.$controller.'.php';
$realIP = '';
if (isset($_SERVER['HTTP_X_REAL_IP'])) {
$realIP = $_SERVER['HTTP_X_REAL_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$realIP = $_SERVER['REMOTE_ADDR'];
}
if (\in_array(
$controller,
[
LegacyController::TAXONOMY_CONTROLLER,
LegacyController::MANUFACTURER_CONTROLLER,
LegacyController::TAG_CONTROLLER,
LegacyController::PRODUCT_CONTROLLER,
],
true
)) {
return [
'class' => $controller,
'method' => $method,
'args' => $args,
];
}
if (!is_callable([$controller, $method]) || !is_readable($controllerPath)) {
return [
'class' => LegacyRequest::NOT_FOUND_CONTROLLER,
'method' => LegacyRequest::DEFAULT_METHOD,
'args' => $args,
];
// throw new NotFoundHttpException('Not Found');
}
// Require requested controller and all the rest from the same folder (admin, front..) because in certain cases a controller calls another...
// Sort descending because of a single case (admin/sliderController must be loaded before admin/bannerController). So dirty, I know.
foreach (scandir($this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.'front', SCANDIR_SORT_DESCENDING) as $filename) {
$controllerPath = $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.'front/'.$filename;
if (is_file($controllerPath) && strpos($filename, 'Controller')) {
require_once $controllerPath;
}
}
return [
'class' => $controller,
'method' => $method,
'args' => $args,
];
}
}