/**
  * indexAction action.
  */
 public function indexAction(Request $request, $_format)
 {
     if (version_compare(Kernel::VERSION, '2.1.0-dev', '<')) {
         if (null !== ($session = $request->getSession())) {
             // keep current flashes for one more request
             $session->setFlashes($session->getFlashes());
         }
     } else {
         $session = $request->getSession();
         if (null !== $session && $session->getFlashBag() instanceof AutoExpireFlashBag) {
             // keep current flashes for one more request if using AutoExpireFlashBag
             $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
         }
     }
     $cache = new ConfigCache($this->cacheDir . '/fosJsRouting.json', $this->debug);
     if (!$cache->isFresh()) {
         $content = $this->serializer->serialize(new RoutesResponse($this->exposedRoutesExtractor->getBaseUrl(), $this->exposedRoutesExtractor->getRoutes()), 'json');
         $cache->write($content, $this->exposedRoutesExtractor->getResources());
     }
     $content = file_get_contents((string) $cache);
     if ($callback = $request->query->get('callback')) {
         $content = $callback . '(' . $content . ');';
     }
     return new Response($content, 200, array('Content-Type' => $request->getMimeType($_format)));
 }
Beispiel #2
0
 /**
  * @param string $cacheDir
  * @param string $cachePrefix
  * @param string $kernelContainerClass
  * @param ExposedRoutesExtractorInterface $fosJsRoutesExtractor
  * @param array $routingLocales
  */
 public function __construct($cacheDir, $cachePrefix, $kernelContainerClass, $fosJsRoutesExtractor, $routingLocales)
 {
     $this->cacheDir = $cacheDir;
     $this->cachePrefix = $cachePrefix;
     $this->fs = new Filesystem();
     $cacheFolder = $cacheDir . DIRECTORY_SEPARATOR;
     $fosJsRoutingFiles = array();
     foreach ($routingLocales as $locale) {
         $fosJsRoutingFiles[] = $fosJsRoutesExtractor->getCachePath($locale);
     }
     $this->cacheTypes = array("symfony.annotations" => array("{$cacheFolder}/annotations"), "symfony.routing.generator" => array("{$cacheFolder}{$cachePrefix}UrlGenerator.php", "{$cacheFolder}{$cachePrefix}UrlGenerator.php.meta"), "symfony.routing.matcher" => array("{$cacheFolder}{$cachePrefix}UrlMatcher.php", "{$cacheFolder}{$cachePrefix}UrlMatcher.php.meta"), "symfony.routing.fosjs" => $fosJsRoutingFiles, "symfony.config" => array("{$cacheFolder}{$kernelContainerClass}.php", "{$cacheFolder}{$kernelContainerClass}.php.meta", "{$cacheFolder}{$kernelContainerClass}.xml", "{$cacheFolder}{$kernelContainerClass}Compiler.log", "{$cacheFolder}classes.map"));
 }
 /**
  * indexAction action.
  */
 public function indexAction(Request $request, $_format)
 {
     $cache = new ConfigCache($this->cacheDir . '/fosJsRouting.json', $this->debug);
     if (!$cache->isFresh()) {
         $content = $this->serializer->serialize(new RoutesResponse($this->exposedRoutesExtractor->getBaseUrl(), $this->exposedRoutesExtractor->getRoutes()), 'json');
         $cache->write($content, $this->exposedRoutesExtractor->getResources());
     }
     $content = file_get_contents((string) $cache);
     if ($callback = $request->query->get('callback')) {
         $content = $callback . '(' . $content . ')';
     }
     return new Response($content, 200, array('Content-Type' => $request->getMimeType($_format)));
 }
 /**
  * indexAction action.
  */
 public function indexAction(Request $request, $_format)
 {
     $session = $request->getSession();
     if ($request->hasPreviousSession() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
         // keep current flashes for one more request if using AutoExpireFlashBag
         $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
     }
     $cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath($request->getLocale()), $this->debug);
     if (!$cache->isFresh()) {
         $exposedRoutes = $this->exposedRoutesExtractor->getRoutes();
         $serializedRoutes = $this->serializer->serialize($exposedRoutes, 'json');
         $cache->write($serializedRoutes, $this->exposedRoutesExtractor->getResources());
     } else {
         $serializedRoutes = file_get_contents((string) $cache);
         $exposedRoutes = $this->serializer->deserialize($serializedRoutes, 'Symfony\\Component\\Routing\\RouteCollection', 'json');
     }
     $routesResponse = new RoutesResponse($this->exposedRoutesExtractor->getBaseUrl(), $exposedRoutes, $this->exposedRoutesExtractor->getPrefix($request->getLocale()), $this->exposedRoutesExtractor->getHost(), $this->exposedRoutesExtractor->getScheme(), $request->getLocale());
     $content = $this->serializer->serialize($routesResponse, 'json');
     if (null !== ($callback = $request->query->get('callback'))) {
         $validator = new \JsonpCallbackValidator();
         if (!$validator->validate($callback)) {
             throw new HttpException(400, 'Invalid JSONP callback value');
         }
         $content = $callback . '(' . $content . ');';
     }
     $response = new Response($content, 200, array('Content-Type' => $request->getMimeType($_format)));
     $this->cacheControlConfig->apply($response);
     return $response;
 }
 /**
  * Performs the routes dump.
  *
  * @param InputInterface  $input  The command input
  * @param OutputInterface $output The command output
  */
 private function doDump(InputInterface $input, OutputInterface $output)
 {
     if (!is_dir($dir = dirname($this->targetPath))) {
         $output->writeln('<info>[dir+]</info>  ' . $dir);
         if (false === @mkdir($dir, 0777, true)) {
             throw new \RuntimeException('Unable to create directory ' . $dir);
         }
     }
     $output->writeln('<info>[file+]</info> ' . $this->targetPath);
     $baseUrl = $this->getContainer()->hasParameter('fos_js_routing.request_context_base_url') ? $this->getContainer()->getParameter('fos_js_routing.request_context_base_url') : $this->extractor->getBaseUrl();
     $content = $this->serializer->serialize(new RoutesResponse($baseUrl, $this->extractor->getRoutes($input->getOption('sets')), $input->getOption('locale'), $this->extractor->getHost(), $this->extractor->getScheme()), 'json');
     $content = sprintf("%s(%s);", $input->getOption('callback'), $content);
     if (false === @file_put_contents($this->targetPath, $content)) {
         throw new \RuntimeException('Unable to write file ' . $this->targetPath);
     }
 }