<?php
declare(strict_types=1);
namespace App\Component\JsRouting\Extractor;
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class FrontendJsRouteExtractor extends ExposedRoutesExtractor
{
public function getRoutes(): RouteCollection
{
$isAdmin = $this->router->getContext()->getParameter('IS_ADMIN') ?? false;
$routes = parent::getRoutes();
/**
* @var string $name
* @var Route $route
*/
foreach (iterator_to_array($routes->getIterator()) as $name => $route) {
if (!$this->shouldRouteBeRemoved((bool) $isAdmin, $route)) {
continue;
}
$routes->remove($name);
}
return $routes;
}
public function getCachePath($locale)
{
$isAdmin = $this->router->getContext()->getParameter('IS_ADMIN') ?? false;
$cachePath = (bool) $isAdmin ?
$this->cacheDir.\DIRECTORY_SEPARATOR.'fosJsRouting_admin' :
$this->cacheDir.\DIRECTORY_SEPARATOR.'fosJsRouting';
if (!file_exists($cachePath)) {
mkdir($cachePath);
}
if (isset($this->bundles['JMSI18nRoutingBundle'])) {
$cachePath = $cachePath.\DIRECTORY_SEPARATOR.'data.'.$locale.'.json';
} else {
$cachePath = $cachePath.\DIRECTORY_SEPARATOR.'data.json';
}
return $cachePath;
}
private function shouldRouteBeRemoved(bool $isAdmin, Route $route): bool
{
if ($isAdmin) {
return false;
}
return str_starts_with($route->getPath(), '/admin');
}
}