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

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. use Symfony\Component\HttpKernel\Kernel;
  20. /**
  21.  * Controller class.
  22.  *
  23.  * @author William DURAND <william.durand1@gmail.com>
  24.  */
  25. class Controller
  26. {
  27.     /**
  28.      * @var mixed
  29.      */
  30.     protected $serializer;
  31.     /**
  32.      * @var ExposedRoutesExtractorInterface
  33.      */
  34.     protected $exposedRoutesExtractor;
  35.     /**
  36.      * @var CacheControlConfig
  37.      */
  38.     protected $cacheControlConfig;
  39.     /**
  40.      * @var boolean
  41.      */
  42.     protected $debug;
  43.     /**
  44.      * Default constructor.
  45.      *
  46.      * @param object                          $serializer             Any object with a serialize($data, $format) method
  47.      * @param ExposedRoutesExtractorInterface $exposedRoutesExtractor The extractor service.
  48.      * @param array                           $cacheControl
  49.      * @param boolean                         $debug
  50.      */
  51.     public function __construct($serializerExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = array(), $debug false)
  52.     {
  53.         $this->serializer             $serializer;
  54.         $this->exposedRoutesExtractor $exposedRoutesExtractor;
  55.         $this->cacheControlConfig     = new CacheControlConfig($cacheControl);
  56.         $this->debug                  $debug;
  57.     }
  58.     /**
  59.      * indexAction action.
  60.      */
  61.     public function indexAction(Request $request$_format)
  62.     {
  63.         if (version_compare(strtolower(Kernel::VERSION), '2.1.0-dev''<')) {
  64.             if (null !== $session $request->getSession()) {
  65.                 // keep current flashes for one more request
  66.                 $session->setFlashes($session->getFlashes());
  67.             }
  68.         } else {
  69.             $session $request->getSession();
  70.             if ($request->hasPreviousSession() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
  71.                 // keep current flashes for one more request if using AutoExpireFlashBag
  72.                 $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
  73.             }
  74.         }
  75.         $cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath($request->getLocale()), $this->debug);
  76.         if (!$cache->isFresh()) {
  77.             $exposedRoutes $this->exposedRoutesExtractor->getRoutes();
  78.             $serializedRoutes $this->serializer->serialize($exposedRoutes'json');
  79.             $cache->write($serializedRoutes$this->exposedRoutesExtractor->getResources());
  80.         } else {
  81.             $serializedRoutes file_get_contents(method_exists($cache'getPath') ? $cache->getPath() : (string) $cache);
  82.             $exposedRoutes json_decode($serializedRoutestrue);
  83.         }
  84.         $routesResponse = new RoutesResponse(
  85.             $this->exposedRoutesExtractor->getBaseUrl(),
  86.             $exposedRoutes,
  87.             $this->exposedRoutesExtractor->getPrefix($request->getLocale()),
  88.             $this->exposedRoutesExtractor->getHost(),
  89.             $this->exposedRoutesExtractor->getScheme()
  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. }