Пример #1
0
 /**
  * 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;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getBaseUrl()
 {
     $site = $this->selector->retrieve();
     if ($site) {
         return parent::getBaseUrl() . $site->getRelativePath();
     }
     return parent::getBaseUrl();
 }
 public function onLateKernelRequest(GetResponseEvent $event)
 {
     $site = $this->siteSelector->retrieve();
     if ($site) {
         $this->translatableListener->setTranslatableLocale($site->getLocale());
         $this->translatableListener->setFallbackLocales($site->getLocales());
     }
 }
Пример #4
0
 public function generatePageMenu($menu)
 {
     $site = $this->site_selector->retrieve();
     $page = $this->page_manager->getPageByUrl($site, '/');
     $menu->addChild($page->getName(), array('route' => $page->getRouteName(), 'routeParameters' => array('path' => $page->getUrl())));
     if (!$page) {
         return false;
     }
     $this->iterate($menu[$page->getName()], $page);
 }
 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);
 }
 /**
  * {@inheritdoc}
  */
 public function execute(BlockContextInterface $blockContext, Response $response = null)
 {
     $settings = $blockContext->getSettings();
     $cmsManager = $this->cmsManagerSelector->retrieve();
     if ($settings['current']) {
         $page = $cmsManager->getCurrentPage();
     } elseif ($settings['pageId']) {
         $page = $settings['pageId'];
     } else {
         try {
             $page = $cmsManager->getPage($this->siteSelector->retrieve(), '/');
         } catch (PageNotFoundException $e) {
             $page = false;
         }
     }
     return $this->renderResponse($blockContext->getTemplate(), array('page' => $page, 'block' => $blockContext->getBlock(), 'settings' => $settings), $response);
 }
 /**
  * Forwards pathInfo to subrequests.
  * Allows HostPathSiteSelector to work.
  *
  * @param string $controller
  * @param array  $attributes
  * @param array  $query
  *
  * @return ControllerReference
  */
 public function controller($controller, $attributes = array(), $query = array())
 {
     $globals = $this->environment->getGlobals();
     if (!isset($attributes['pathInfo'])) {
         $sitePath = $this->siteSelector->retrieve()->getRelativePath();
         $currentPathInfo = $globals['app']->getRequest()->getPathInfo();
         $attributes['pathInfo'] = $sitePath . $currentPathInfo;
     }
     return $this->httpKernelExtension->controller($controller, $attributes, $query);
 }
Пример #8
0
 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 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);
     }
 }
 /**
  * Gets the site from site selector.
  *
  * @return Site|null
  */
 protected function getSite()
 {
     return $this->siteSelector->retrieve();
 }
Пример #11
0
 /**
  * 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;
 }