Example #1
0
 /**
  * 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;
 }
Example #2
0
 protected function getCallback(Request $request)
 {
     $callback = $request->query->get($this->callbackParam);
     $validator = new \JsonpCallbackValidator();
     if (!$validator->validate($callback)) {
         throw new BadRequestHttpException('Invalid JSONP callback value');
     }
     return $callback;
 }
 /**
  * Serializes an object.
  *
  * Implements FormatterInterface::formatItem($item)
  *
  * @param mixed $item
  *
  * @return array
  */
 public function formatItem($item, $format = 'json')
 {
     $formatted = null;
     switch ($format) {
         case 'json':
             // serialize properties with null values
             $context = new SerializationContext();
             $context->setSerializeNull(true);
             $context->enableMaxDepthChecks(true);
             $formatted = $this->getSerializer()->serialize($item, 'json', $context);
             break;
         case 'jsonp':
             $callback = $this->getRequest()->query->get('jsonp.callback', 'JSONP.callback');
             // validate against XSS
             $validator = new \JsonpCallbackValidator();
             if (!$validator->validate($callback)) {
                 throw new BadRequestHttpException('Invalid JSONP callback value');
             }
             $context = new SerializationContext();
             $context->setSerializeNull(true);
             $json = $this->getSerializer()->serialize($item, 'json');
             $formatted = sprintf('/**/%s(%s)', $callback, $json);
             break;
         default:
             // any other format is not supported
             throw new \InvalidArgumentException(sprintf('Format not supported: %s', $format));
     }
     return $formatted;
 }
 public function testCallStatically()
 {
     $this->assertTrue(\JsonpCallbackValidator::validate('foo'));
 }