src/Component/JsRouting/Extractor/FrontendJsRouteExtractor.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Component\JsRouting\Extractor;
  4. use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor;
  5. use Symfony\Component\Routing\Route;
  6. use Symfony\Component\Routing\RouteCollection;
  7. class FrontendJsRouteExtractor extends ExposedRoutesExtractor
  8. {
  9.     public function getRoutes(): RouteCollection
  10.     {
  11.         $isAdmin $this->router->getContext()->getParameter('IS_ADMIN') ?? false;
  12.         $routes parent::getRoutes();
  13.         /**
  14.          * @var string $name
  15.          * @var Route  $route
  16.          */
  17.         foreach (iterator_to_array($routes->getIterator()) as $name => $route) {
  18.             if (!$this->shouldRouteBeRemoved((bool) $isAdmin$route)) {
  19.                 continue;
  20.             }
  21.             $routes->remove($name);
  22.         }
  23.         return $routes;
  24.     }
  25.     public function getCachePath($locale)
  26.     {
  27.         $isAdmin $this->router->getContext()->getParameter('IS_ADMIN') ?? false;
  28.         $cachePath = (bool) $isAdmin ?
  29.             $this->cacheDir.\DIRECTORY_SEPARATOR.'fosJsRouting_admin' :
  30.             $this->cacheDir.\DIRECTORY_SEPARATOR.'fosJsRouting';
  31.         if (!file_exists($cachePath)) {
  32.             mkdir($cachePath);
  33.         }
  34.         if (isset($this->bundles['JMSI18nRoutingBundle'])) {
  35.             $cachePath $cachePath.\DIRECTORY_SEPARATOR.'data.'.$locale.'.json';
  36.         } else {
  37.             $cachePath $cachePath.\DIRECTORY_SEPARATOR.'data.json';
  38.         }
  39.         return $cachePath;
  40.     }
  41.     private function shouldRouteBeRemoved(bool $isAdminRoute $route): bool
  42.     {
  43.         if ($isAdmin) {
  44.             return false;
  45.         }
  46.         return str_starts_with($route->getPath(), '/admin');
  47.     }
  48. }