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\HttpException;
  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(private mixed $serializer, private ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = [], private bool $debug false)
  36.     {
  37.         $this->cacheControlConfig = new CacheControlConfig($cacheControl);
  38.     }
  39.     public function indexAction(Request $request$_format): Response
  40.     {
  41.         $session $request->hasSession() ? $request->getSession() : null;
  42.         if ($request->hasPreviousSession() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
  43.             // keep current flashes for one more request if using AutoExpireFlashBag
  44.             $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
  45.         }
  46.         $cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath($request->getLocale()), $this->debug);
  47.         if (!$cache->isFresh() || $this->debug) {
  48.             $exposedRoutes $this->exposedRoutesExtractor->getRoutes();
  49.             $serializedRoutes $this->serializer->serialize($exposedRoutes'json');
  50.             $cache->write($serializedRoutes$this->exposedRoutesExtractor->getResources());
  51.         } else {
  52.             $path method_exists($cache'getPath') ? $cache->getPath() : (string) $cache;
  53.             $serializedRoutes file_get_contents($path);
  54.             $exposedRoutes $this->serializer->deserialize(
  55.                 $serializedRoutes,
  56.                 'Symfony\Component\Routing\RouteCollection',
  57.                 'json'
  58.             );
  59.         }
  60.         $routesResponse = new RoutesResponse(
  61.             $this->exposedRoutesExtractor->getBaseUrl(),
  62.             $exposedRoutes,
  63.             $this->exposedRoutesExtractor->getPrefix($request->getLocale()),
  64.             $this->exposedRoutesExtractor->getHost(),
  65.             $this->exposedRoutesExtractor->getPort(),
  66.             $this->exposedRoutesExtractor->getScheme(),
  67.             $request->getLocale(),
  68.             $request->query->has('domain') ? explode(','$request->query->get('domain')) : []
  69.         );
  70.         $content $this->serializer->serialize($routesResponse'json');
  71.         if (null !== $callback $request->query->get('callback')) {
  72.             $validator = new \JsonpCallbackValidator();
  73.             if (!$validator->validate($callback)) {
  74.                 throw new HttpException(400'Invalid JSONP callback value');
  75.             }
  76.             $content '/**/'.$callback.'('.$content.');';
  77.         }
  78.         $response = new Response($content200, ['Content-Type' => $request->getMimeType($_format)]);
  79.         $this->cacheControlConfig->apply($response);
  80.         return $response;
  81.     }
  82. }