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

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