Пример #1
0
 /**
  * Filter the `core.request` event to decorated the action.
  *
  * @param GetResponseEvent $event
  *
  * @throws InternalErrorException
  * @throws PageNotFoundException
  */
 public function onCoreRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $cms = $this->cmsSelector->retrieve();
     if (!$cms) {
         throw new InternalErrorException('No CMS Manager available');
     }
     // true cms page
     if ($request->get('_route') == PageInterface::PAGE_ROUTE_CMS_NAME) {
         return;
     }
     if (!$this->decoratorStrategy->isRequestDecorable($request)) {
         return;
     }
     $site = $this->siteSelector->retrieve();
     if (!$site) {
         throw new InternalErrorException('No site available for the current request with uri ' . htmlspecialchars($request->getUri(), ENT_QUOTES));
     }
     if ($site->getLocale() && $site->getLocale() != $request->get('_locale')) {
         throw new PageNotFoundException(sprintf('Invalid locale - site.locale=%s - request._locale=%s', $site->getLocale(), $request->get('_locale')));
     }
     try {
         $page = $cms->getPageByRouteName($site, $request->get('_route'));
         if (!$page->getEnabled() && !$this->cmsSelector->isEditor()) {
             throw new PageNotFoundException(sprintf('The page is not enabled : id=%s', $page->getId()));
         }
         $cms->setCurrentPage($page);
     } catch (PageNotFoundException $e) {
         return;
     }
 }
 /**
  * @param PageBlockInterface $block
  * @param array              $options
  *
  * @return string
  */
 public function renderBlock(PageBlockInterface $block, array $options = array())
 {
     if ($block->getEnabled() === false && !$this->cmsManagerSelector->isEditor()) {
         return '';
     }
     // defined extra default key for the cache
     $pageCacheKeys = array('manager' => $block->getPage() instanceof SnapshotPageProxy ? 'snapshot' : 'page', 'page_id' => $block->getPage()->getId());
     // build the parameters array
     $options = array_merge(array('use_cache' => isset($options['use_cache']) ? $options['use_cache'] : true, 'extra_cache_keys' => array()), $pageCacheKeys, $options);
     // make sure the parameters array contains all valid keys
     $options['extra_cache_keys'] = array_merge($options['extra_cache_keys'], $pageCacheKeys);
     return $this->blockHelper->render($block, $options);
 }
Пример #3
0
 /**
  * Filter the `core.response` event to decorate the action.
  *
  * @param FilterResponseEvent $event
  *
  * @throws InternalErrorException
  */
 public function onCoreResponse(FilterResponseEvent $event)
 {
     $cms = $this->cmsSelector->retrieve();
     $response = $event->getResponse();
     $request = $event->getRequest();
     if ($this->cmsSelector->isEditor()) {
         $response->setPrivate();
         if (!$request->cookies->has('sonata_page_is_editor')) {
             $response->headers->setCookie(new Cookie('sonata_page_is_editor', 1));
         }
     }
     $page = $cms->getCurrentPage();
     // display a validation page before redirecting, so the editor can edit the current page
     if ($page && $response->isRedirection() && $this->cmsSelector->isEditor() && !$request->get('_sonata_page_skip')) {
         $response = new Response($this->templating->render('SonataPageBundle:Page:redirect.html.twig', array('response' => $response, 'page' => $page)));
         $response->setPrivate();
         $event->setResponse($response);
         return;
     }
     if (!$this->decoratorStrategy->isDecorable($event->getRequest(), $event->getRequestType(), $response)) {
         return;
     }
     if (!$this->cmsSelector->isEditor() && $request->cookies->has('sonata_page_is_editor')) {
         $response->headers->clearCookie('sonata_page_is_editor');
     }
     if (!$page) {
         throw new InternalErrorException('No page instance available for the url, run the sonata:page:update-core-routes and sonata:page:create-snapshots commands');
     }
     // only decorate hybrid page or page with decorate = true
     if (!$page->isHybrid() || !$page->getDecorate()) {
         return;
     }
     $parameters = array('content' => $response->getContent());
     $response = $this->pageServiceManager->execute($page, $request, $parameters, $response);
     if (!$this->cmsSelector->isEditor() && $page->isCms()) {
         $response->setTtl($page->getTtl());
     }
     $event->setResponse($response);
 }
 /**
  * Handles a kernel exception.
  *
  * @param GetResponseForExceptionEvent $event
  *
  * @throws \Exception
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if ($event->getException() instanceof NotFoundHttpException && $this->cmsManagerSelector->isEditor()) {
         $pathInfo = $event->getRequest()->getPathInfo();
         // can only create a CMS page, so the '_route' must be null
         $creatable = !$event->getRequest()->get('_route') && $this->decoratorStrategy->isRouteUriDecorable($pathInfo);
         if ($creatable) {
             $response = new Response($this->templating->render('SonataPageBundle:Page:create.html.twig', array('pathInfo' => $pathInfo, 'site' => $this->siteSelector->retrieve(), 'creatable' => $creatable)), 404);
             $event->setResponse($response);
             $event->stopPropagation();
             return;
         }
     }
     if ($event->getException() instanceof InternalErrorException) {
         $this->handleInternalError($event);
     } else {
         $this->handleNativeError($event);
     }
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function match($pathinfo)
 {
     $cms = $this->cmsSelector->retrieve();
     $site = $this->siteSelector->retrieve();
     if (!$cms instanceof CmsManagerInterface) {
         throw new ResourceNotFoundException('No CmsManager defined');
     }
     if (!$site instanceof SiteInterface) {
         throw new ResourceNotFoundException('No site defined');
     }
     try {
         $page = $cms->getPageByUrl($site, $pathinfo);
     } catch (PageNotFoundException $e) {
         throw new ResourceNotFoundException($pathinfo, 0, $e);
     }
     if (!$page || !$page->isCms()) {
         throw new ResourceNotFoundException($pathinfo);
     }
     if (!$page->getEnabled() && !$this->cmsSelector->isEditor()) {
         throw new ResourceNotFoundException($pathinfo);
     }
     $cms->setCurrentPage($page);
     return array('_controller' => 'sonata.page.page_service_manager:execute', '_route' => PageInterface::PAGE_ROUTE_CMS_NAME, 'page' => $page, 'path' => $pathinfo, 'params' => array());
 }