예제 #1
0
 /**
  * Action for ajax route rendering a block by calling his executeAjax() method
  *
  * @param Request $request Symfony request object
  * @param integer $pageId  Page identifier
  * @param integer $blockId Block identifier
  */
 public function execute(Request $request, $pageId, $blockId)
 {
     $cmsManager = $this->cmsManagerSelector->retrieve();
     $page = $cmsManager->getPageById($pageId);
     $block = $cmsManager->getBlock($blockId);
     if (!$block instanceof BlockInterface) {
         throw new BlockNotFoundException(sprintf('Unable to find block identifier "%s" in page "%s".', $blockId, $pageId));
     }
     $response = $this->blockRenderer->render($block);
     return $response;
 }
예제 #2
0
 /**
  * @param Request $request
  *
  * @return mixed
  */
 public function cacheAction(Request $request)
 {
     $parameters = array_merge($request->query->all(), $request->attributes->all());
     if ($request->get('_token') != $this->computeHash($parameters)) {
         throw new AccessDeniedHttpException('Invalid token');
     }
     $manager = $this->getManager($request);
     $page = $manager->getPageById($request->get('page_id'));
     if (!$page) {
         throw new NotFoundHttpException(sprintf('Page not found : %s', $request->get('page_id')));
     }
     $block = $manager->getBlock($request->get('block_id'));
     $blockContext = $this->contextManager->get($block);
     if ($this->recorder) {
         $this->recorder->add($blockContext->getBlock());
         $this->recorder->push();
     }
     $response = $this->blockRenderer->render($blockContext);
     if ($this->recorder) {
         $keys = $this->recorder->pop();
         $keys['page_id'] = $page->getId();
         $keys['block_id'] = $block->getId();
         foreach ($keys as $key => $value) {
             $response->headers->set($this->normalize($key), $value);
         }
     }
     $response->headers->set('x-sonata-page-not-decorable', true);
     return $response;
 }
예제 #3
0
 /**
  * @param Request $request
  *
  * @throws NotFoundHttpException
  * @throws AccessDeniedHttpException
  *
  * @return mixed
  */
 public function cacheAction(Request $request)
 {
     $parameters = array_merge($request->query->all(), $request->attributes->all());
     if ($request->get('_token') != $this->computeHash($parameters)) {
         throw new AccessDeniedHttpException('Invalid token');
     }
     $block = $this->blockLoader->load(array('name' => $request->get('block_id')));
     if (!$block) {
         throw new NotFoundHttpException(sprintf('Block not found : %s', $request->get('block_id')));
     }
     $settings = $request->get(BlockContextManagerInterface::CACHE_KEY, array());
     if (!is_array($settings)) {
         throw new \RuntimeException(sprintf('Query string parameter `%s` is not an array', BlockContextManagerInterface::CACHE_KEY));
     }
     return $this->blockRenderer->render($this->blockContextManager->get($block, $settings));
 }
예제 #4
0
    /**
     * @param Request $request
     *
     * @return Response
     */
    public function cacheAction(Request $request)
    {
        $cms = $this->cmsSelector->retrieve();
        try {
            $page = $cms->getPageById($request->get('page_id'));
        } catch (PageNotFoundException $e) {
            $page = false;
        }
        $block = $cms->getBlock($request->get('block_id'));
        if (!$page || !$block) {
            return new Response('', 404);
        }
        $options = array();
        $blockContext = $this->contextManager->get($block, $options);
        $response = $this->blockRenderer->render($blockContext);
        $response->setPrivate();
        //  always set to private
        if ($this->sync) {
            return $response;
        }
        $response->setContent(sprintf('
    (function () {
        var block = document.getElementById("block-cms-%s");

        var div = document.createElement("div");
        div.innerHTML = %s;

        for (var node in div.childNodes) {
            if (div.childNodes[node] && div.childNodes[node].nodeType == 1) {
                block.parentNode.replaceChild(div.childNodes[node], block);
            }
        }
    })();
', $block->getId(), json_encode($response->getContent())));
        $response->headers->set('Content-Type', 'application/javascript');
        return $response;
    }
예제 #5
0
 /**
  * @param mixed $block
  * @param array $options
  *
  * @return null|Response
  */
 public function render($block, array $options = array())
 {
     $blockContext = $this->blockContextManager->get($block, $options);
     if (!$blockContext instanceof BlockContextInterface) {
         return '';
     }
     $stats = array();
     if ($this->stopwatch) {
         $stats = $this->startTracing($blockContext->getBlock());
     }
     $service = $this->blockServiceManager->get($blockContext->getBlock());
     $this->computeAssets($blockContext, $stats);
     $useCache = $blockContext->getSetting('use_cache');
     $cacheKeys = $response = false;
     $cacheService = $useCache ? $this->getCacheService($blockContext->getBlock(), $stats) : false;
     if ($cacheService) {
         $cacheKeys = array_merge($service->getCacheKeys($blockContext->getBlock()), $blockContext->getSetting('extra_cache_keys'));
         if ($this->stopwatch) {
             $stats['cache']['keys'] = $cacheKeys;
         }
         // Please note, some cache handler will always return true (js for instance)
         // This will allows to have a non cacheable block, but the global page can still be cached by
         // a reverse proxy, as the generated page will never get the generated Response from the block.
         if ($cacheService->has($cacheKeys)) {
             $cacheElement = $cacheService->get($cacheKeys);
             if ($this->stopwatch) {
                 $stats['cache']['from_cache'] = false;
             }
             if (!$cacheElement->isExpired() && $cacheElement->getData() instanceof Response) {
                 /* @var Response $response */
                 if ($this->stopwatch) {
                     $stats['cache']['from_cache'] = true;
                 }
                 $response = $cacheElement->getData();
             }
         }
     }
     if (!$response) {
         $recorder = null;
         if ($this->cacheManager) {
             $recorder = $this->cacheManager->getRecorder();
             if ($recorder) {
                 $recorder->add($blockContext->getBlock());
                 $recorder->push();
             }
         }
         $response = $this->blockRenderer->render($blockContext);
         $contextualKeys = $recorder ? $recorder->pop() : array();
         if ($this->stopwatch) {
             $stats['cache']['contextual_keys'] = $contextualKeys;
         }
         if ($response->isCacheable() && $cacheKeys && $cacheService) {
             $cacheService->set($cacheKeys, $response, $response->getTtl(), $contextualKeys);
         }
     }
     if ($this->stopwatch) {
         $stats['cache']['created_at'] = $response->getDate();
         $stats['cache']['ttl'] = $response->getTtl() ?: 0;
         $stats['cache']['age'] = $response->getAge();
     }
     // update final ttl for the whole Response
     if ($this->cacheHandler) {
         $this->cacheHandler->updateMetadata($response, $blockContext);
     }
     if ($this->stopwatch) {
         $this->stopTracing($blockContext->getBlock(), $stats);
     }
     return $response->getContent();
 }