src/Controller/FOSController.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This Controller is intended to extends FOS\JsRoutingBundle\Controller\Controller
  5.  * as it doesn't work well on Symfony > 5
  6.  */
  7. namespace App\Controller;
  8. use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
  9. use FOS\JsRoutingBundle\Response\RoutesResponse;
  10. use FOS\JsRoutingBundle\Util\CacheControlConfig;
  11. use Symfony\Component\Config\ConfigCache;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. class FOSController
  17. {
  18.     protected CacheControlConfig $cacheControlConfig;
  19.     /**
  20.      * Default constructor.
  21.      *
  22.      * @param object                          $serializer             Any object with a serialize($data, $format) method
  23.      * @param ExposedRoutesExtractorInterface $exposedRoutesExtractor the extractor service
  24.      * @param bool                            $debug
  25.      */
  26.     public function __construct(private mixed $serializer, private ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = [], private bool $debug false)
  27.     {
  28.         $this->cacheControlConfig = new CacheControlConfig($cacheControl);
  29.     }
  30.     public function indexAction(Request $request$_format): Response
  31.     {
  32.         $session $request->hasSession() ? $request->getSession() : null;
  33.         
  34.         // MAIN CHANGES IS HERE AutoExpireFlashBag CHANGED TO FlashBag
  35.         if ($request->hasPreviousSession() && $session->getFlashBag() instanceof FlashBag) {
  36.             // keep current flashes for one more request if using AutoExpireFlashBag
  37.             $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
  38.         }
  39.         $cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath($request->getLocale()), $this->debug);
  40.         if (!$cache->isFresh() || $this->debug) {
  41.             $exposedRoutes $this->exposedRoutesExtractor->getRoutes();
  42.             $serializedRoutes $this->serializer->serialize($exposedRoutes'json');
  43.             $cache->write($serializedRoutes$this->exposedRoutesExtractor->getResources());
  44.         } else {
  45.             $path method_exists($cache'getPath') ? $cache->getPath() : (string) $cache;
  46.             $serializedRoutes file_get_contents($path);
  47.             $exposedRoutes $this->serializer->deserialize(
  48.                 $serializedRoutes,
  49.                 'Symfony\Component\Routing\RouteCollection',
  50.                 'json'
  51.             );
  52.         }
  53.         $routesResponse = new RoutesResponse(
  54.             $this->exposedRoutesExtractor->getBaseUrl(),
  55.             $exposedRoutes,
  56.             $this->exposedRoutesExtractor->getPrefix($request->getLocale()),
  57.             $this->exposedRoutesExtractor->getHost(),
  58.             $this->exposedRoutesExtractor->getPort(),
  59.             $this->exposedRoutesExtractor->getScheme(),
  60.             $request->getLocale(),
  61.             $request->query->has('domain') ? explode(','$request->query->get('domain')) : []
  62.         );
  63.         $content $this->serializer->serialize($routesResponse'json');
  64.         if (null !== $callback $request->query->get('callback')) {
  65.             $validator = new \JsonpCallbackValidator();
  66.             if (!$validator->validate($callback)) {
  67.                 throw new HttpException(400'Invalid JSONP callback value');
  68.             }
  69.             $content '/**/'.$callback.'('.$content.');';
  70.         }
  71.         $response = new Response($content200, ['Content-Type' => $request->getMimeType($_format)]);
  72.         $this->cacheControlConfig->apply($response);
  73.         return $response;
  74.     }
  75. }