/** * Filter the `core.request` event to decorated the action * * @param GetResponseEvent $event * * @return void * * @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; } }
/** * 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; }
public function testGenerateWithPageAliasFromHybridPage() { $page = $this->getMock('Sonata\\PageBundle\\Model\\PageInterface'); $page->expects($this->exactly(5))->method('isHybrid')->will($this->returnValue(true)); $page->expects($this->exactly(5))->method('getRouteName')->will($this->returnValue('test_route')); $site = $this->getMock('Sonata\\PageBundle\\Model\\SiteInterface'); $this->siteSelector->expects($this->exactly(5))->method('retrieve')->will($this->returnValue($site)); $cmsManager = $this->getMock('Sonata\\PageBundle\\CmsManager\\CmsManagerInterface'); $cmsManager->expects($this->exactly(5))->method('getPageByPageAlias')->will($this->returnValue($page)); $this->cmsSelector->expects($this->exactly(5))->method('retrieve')->will($this->returnValue($cmsManager)); $this->defaultRouter->expects($this->at(0))->method('generate')->with($this->equalTo('test_route'), $this->equalTo(array('key' => 'value')), $this->equalTo(UrlGeneratorInterface::ABSOLUTE_PATH))->will($this->returnValue('/test/key/value')); $this->defaultRouter->expects($this->at(1))->method('generate')->with($this->equalTo('test_route'), $this->equalTo(array('key' => 'value')), $this->equalTo(UrlGeneratorInterface::ABSOLUTE_PATH))->will($this->returnValue('/test/key/value')); $this->defaultRouter->expects($this->at(2))->method('generate')->with($this->equalTo('test_route'), $this->equalTo(array('key' => 'value')), $this->equalTo(UrlGeneratorInterface::RELATIVE_PATH))->will($this->returnValue('test/key/value')); $this->defaultRouter->expects($this->at(3))->method('generate')->with($this->equalTo('test_route'), $this->equalTo(array('key' => 'value')), $this->equalTo(UrlGeneratorInterface::ABSOLUTE_URL))->will($this->returnValue('http://localhost/test/key/value')); $this->defaultRouter->expects($this->at(4))->method('generate')->with($this->equalTo('test_route'), $this->equalTo(array('key' => 'value')), $this->equalTo(UrlGeneratorInterface::NETWORK_PATH))->will($this->returnValue('//localhost/test/key/value')); $url = $this->router->generate('_page_alias_homepage', array('key' => 'value')); $this->assertEquals('/test/key/value', $url); $url = $this->router->generate('_page_alias_homepage', array('key' => 'value'), UrlGeneratorInterface::ABSOLUTE_PATH); $this->assertEquals('/test/key/value', $url); $url = $this->router->generate('_page_alias_homepage', array('key' => 'value'), UrlGeneratorInterface::RELATIVE_PATH); $this->assertEquals('test/key/value', $url); $url = $this->router->generate('_page_alias_homepage', array('key' => 'value'), UrlGeneratorInterface::ABSOLUTE_URL); $this->assertEquals('http://localhost/test/key/value', $url); $url = $this->router->generate('_page_alias_homepage', array('key' => 'value'), UrlGeneratorInterface::NETWORK_PATH); $this->assertEquals('//localhost/test/key/value', $url); }
/** * Handles a native error * * @param GetResponseForExceptionEvent $event * * @throws mixed */ private function handleNativeError(GetResponseForExceptionEvent $event) { if (true === $this->debug) { return; } if (true === $this->status) { return; } $this->status = true; $exception = $event->getException(); $statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; $cmsManager = $this->cmsManagerSelector->retrieve(); if ($event->getRequest()->get('_route') && !$this->decoratorStrategy->isRouteNameDecorable($event->getRequest()->get('_route'))) { return; } if (!$this->decoratorStrategy->isRouteUriDecorable($event->getRequest()->getPathInfo())) { return; } if (!$this->hasErrorCode($statusCode)) { return; } $message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()); $this->logException($exception, $exception, $message); try { $page = $this->getErrorCodePage($statusCode); $cmsManager->setCurrentPage($page); $response = $this->pageServiceManager->execute($page, $event->getRequest(), array(), new Response('', $statusCode)); } catch (\Exception $e) { $this->logException($exception, $e); $event->setException($e); $this->handleInternalError($event); return; } $event->setResponse($response); }
/** * {@inheritdoc} */ public function load(BlockInterface $block) { if (is_numeric($block->getSetting('pageId', null))) { $cmsManager = $this->cmsManagerSelector->retrieve(); $site = $block->getPage()->getSite(); $block->setSetting('pageId', $cmsManager->getPage($site, $block->getSetting('pageId'))); } }
/** * @param null|\Sonata\PageBundle\Model\PageInterface $page * @param array $options * @return */ public function breadcrumb(PageInterface $page = null, array $options = array()) { if (!$page) { $page = $this->cmsManagerSelector->retrieve()->getCurrentPage(); } $options = array_merge(array('separator' => '', 'current_class' => '', 'last_separator' => '', 'force_view_home_page' => true, 'container_attr' => array('class' => 'sonata-page-breadcrumbs')), $options); $breadcrumbs = array(); if ($page) { $breadcrumbs = $page->getParents(); if ($options['force_view_home_page'] && (!isset($breadcrumbs[0]) || $breadcrumbs[0]->getRouteName() != 'homepage')) { $homePage = $this->cmsManagerSelector->retrieve()->getPageByRouteName('homepage'); if ($homePage) { array_unshift($breadcrumbs, $homePage); } } } return $this->render('SonataPageBundle:Page:breadcrumb.html.twig', array('page' => $page, 'breadcrumbs' => $breadcrumbs, 'options' => $options)); }
/** * @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); }
/** * 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); }
public function testGenerateWithPageAliasFromHybridPage() { $page = $this->getMock('Sonata\\PageBundle\\Model\\PageInterface'); $page->expects($this->any())->method('isHybrid')->will($this->returnValue(true)); $page->expects($this->once())->method('getRouteName')->will($this->returnValue('test_route')); $site = $this->getMock('Sonata\\PageBundle\\Model\\SiteInterface'); $this->siteSelector->expects($this->once())->method('retrieve')->will($this->returnValue($site)); $cmsManager = $this->getMock('Sonata\\PageBundle\\CmsManager\\CmsManagerInterface'); $cmsManager->expects($this->once())->method('getPageByPageAlias')->will($this->returnValue($page)); $this->cmsSelector->expects($this->once())->method('retrieve')->will($this->returnValue($cmsManager)); $this->defaultRouter->expects($this->once())->method('generate')->with($this->equalTo('test_route'), $this->equalTo(array('key' => 'value')), $this->equalTo(true))->will($this->returnValue('http://localhost/test/key/value')); $url = $this->router->generate('_page_alias_homepage', array('key' => 'value'), true); $this->assertEquals('http://localhost/test/key/value', $url); }
/** * Handles a native error. * * @param GetResponseForExceptionEvent $event * * @throws mixed */ private function handleNativeError(GetResponseForExceptionEvent $event) { if (true === $this->debug) { return; } if (true === $this->status) { return; } $this->status = true; $exception = $event->getException(); $statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; $cmsManager = $this->cmsManagerSelector->retrieve(); if ($event->getRequest()->get('_route') && !$this->decoratorStrategy->isRouteNameDecorable($event->getRequest()->get('_route'))) { return; } if (!$this->decoratorStrategy->isRouteUriDecorable($event->getRequest()->getPathInfo())) { return; } if (!$this->hasErrorCode($statusCode)) { return; } $message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()); $this->logException($exception, $exception, $message); try { $page = $this->getErrorCodePage($statusCode); $cmsManager->setCurrentPage($page); if ($page->getSite()->getLocale() !== $event->getRequest()->getLocale()) { // Compare locales because Request returns the default one if null. // If 404, LocaleListener from HttpKernel component of Symfony is not called. // It uses the "_locale" attribute set by SiteSelectorInterface to set the request locale. // So in order to translate messages, force here the locale with the site. $event->getRequest()->setLocale($page->getSite()->getLocale()); } $response = $this->pageServiceManager->execute($page, $event->getRequest(), array(), new Response('', $statusCode)); } catch (\Exception $e) { $this->logException($exception, $e); $event->setException($e); $this->handleInternalError($event); return; } $event->setResponse($response); }
/** * @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; }
/** * Retrieves a page object from a page alias. * * @param string $alias * * @return \Sonata\PageBundle\Model\PageInterface|null * * @throws PageNotFoundException */ protected function getPageByPageAlias($alias) { $site = $this->siteSelector->retrieve(); $page = $this->cmsSelector->retrieve()->getPageByPageAlias($site, $alias); return $page; }
/** * Return the current Page. * * @return PageInterface */ protected function getCurrentPage() { $cms = $this->cmsSelector->retrieve(); return $cms->getCurrentPage(); }