vendor/friendsofsymfony/jsrouting-bundle/Controller/Controller.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the FOSJsRoutingBundle package.
  5.  *
  6.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace FOS\JsRoutingBundle\Controller;
  12. use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
  13. use FOS\JsRoutingBundle\Response\RoutesResponse;
  14. use FOS\JsRoutingBundle\Util\CacheControlConfig;
  15. use Symfony\Component\Config\ConfigCache;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
  19. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  20. /**
  21.  * Controller class.
  22.  *
  23.  * @author William DURAND <william.durand1@gmail.com>
  24.  */
  25. class Controller
  26. {
  27.     protected CacheControlConfig $cacheControlConfig;
  28.     /**
  29.      * Default constructor.
  30.      *
  31.      * @param object                          $serializer             Any object with a serialize($data, $format) method
  32.      * @param ExposedRoutesExtractorInterface $exposedRoutesExtractor the extractor service
  33.      * @param bool                            $debug
  34.      */
  35.     public function __construct(
  36.         private RoutesResponse $routesResponse,
  37.         private mixed $serializer,
  38.         private ExposedRoutesExtractorInterface $exposedRoutesExtractor,
  39.         array $cacheControl = [],
  40.         private bool $debug false,
  41.     ) {
  42.         $this->cacheControlConfig = new CacheControlConfig($cacheControl);
  43.     }
  44.     public function indexAction(Request $request$_format): Response
  45.     {
  46.         if (!$request->attributes->getBoolean('_stateless') && $request->hasSession()
  47.             && ($session $request->getSession())->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag
  48.         ) {
  49.             // keep current flashes for one more request if using AutoExpireFlashBag
  50.             $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
  51.         }
  52.         $cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath($request->getLocale()), $this->debug);
  53.         if (!$cache->isFresh() || $this->debug) {
  54.             $exposedRoutes $this->exposedRoutesExtractor->getRoutes();
  55.             $serializedRoutes $this->serializer->serialize($exposedRoutes'json');
  56.             $cache->write($serializedRoutes$this->exposedRoutesExtractor->getResources());
  57.         } else {
  58.             $path method_exists($cache'getPath') ? $cache->getPath() : (string) $cache;
  59.             $serializedRoutes file_get_contents($path);
  60.             $exposedRoutes $this->serializer->deserialize(
  61.                 $serializedRoutes,
  62.                 'Symfony\Component\Routing\RouteCollection',
  63.                 'json'
  64.             );
  65.         }
  66.         $this->routesResponse->setBaseUrl($this->exposedRoutesExtractor->getBaseUrl());
  67.         $this->routesResponse->setRoutes($exposedRoutes);
  68.         $this->routesResponse->setPrefix($this->exposedRoutesExtractor->getPrefix($request->getLocale()));
  69.         $this->routesResponse->setHost($this->exposedRoutesExtractor->getHost());
  70.         $this->routesResponse->setPort($this->exposedRoutesExtractor->getPort());
  71.         $this->routesResponse->setScheme($this->exposedRoutesExtractor->getScheme());
  72.         $this->routesResponse->setLocale($request->getLocale());
  73.         $this->routesResponse->setDomains($request->query->has('domain') ? explode(','$request->query->get('domain')) : []);
  74.         $content $this->serializer->serialize($this->routesResponse'json');
  75.         if (null !== $callback $request->query->get('callback')) {
  76.             if (!\JsonpCallbackValidator::validate($callback)) {
  77.                 throw new BadRequestHttpException('Invalid JSONP callback value');
  78.             }
  79.             $content '/**/'.$callback.'('.$content.');';
  80.         }
  81.         $response = new Response($content200, ['Content-Type' => $request->getMimeType($_format)]);
  82.         $this->cacheControlConfig->apply($response);
  83.         return $response;
  84.     }
  85. }