getCurrentLocalization() public method

Returns the current localization for this Request.
public getCurrentLocalization ( ) : Localization
return Sulu\Component\Localization\Localization
示例#1
0
 /**
  * @param HitEvent $event
  */
 public function onHit(HitEvent $event)
 {
     if ($event->getMetadata()->reflection->getName() !== 'Sulu\\Bundle\\EventBundle\\Entity\\Event') {
         return;
     }
     $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization();
     $document = $event->getHit()->getDocument();
     $eventApiEntity = $this->eventManager->findByIdAndLocale($document->getId(), $locale);
     if (!$eventApiEntity) {
         return;
     }
     $startDate = $eventApiEntity->getStartDate();
     $endDate = $eventApiEntity->getEndDate();
     $categories = $eventApiEntity->getCategories();
     $categoryTitles = array();
     foreach ($categories as $category) {
         $categoryTitles[] = $category->getName();
     }
     $startDateField = new Field('start_date', $startDate->format('c'), Field::TYPE_STRING);
     $document->addField($startDateField);
     if ($endDate) {
         $endDateField = new Field('end_date', $endDate->format('c'), Field::TYPE_STRING);
         $document->addField($endDateField);
     }
     $categoryTitleField = new Field('category_title', implode(', ', $categoryTitles), Field::TYPE_STRING);
     $document->addField($categoryTitleField);
     $url = $this->router->generate('sulu_events.detail', array('id' => $eventApiEntity->getId(), 'slug' => $eventApiEntity->getSlug()));
     $document->setUrl($url);
 }
示例#2
0
 private function initialize()
 {
     if ($this->initialized || $this->requestAnalyzer->getCurrentLocalization() === null) {
         return;
     }
     $this->translator->setLocale($this->requestAnalyzer->getCurrentLocalization()->getLocale(Localization::LCID));
     $this->initialized = true;
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function sitemapFunction($locale = null, $webspaceKey = null)
 {
     if ($webspaceKey === null) {
         $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey();
     }
     if ($locale === null) {
         $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization();
     }
     return $this->sitemapGenerator->generate($webspaceKey, $locale)->getSitemap();
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function loadSnippet($uuid, $locale = null)
 {
     if ($locale === null) {
         $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization();
     }
     try {
         $snippet = $this->contentMapper->load($uuid, $this->requestAnalyzer->getWebspace()->getKey(), $locale);
         return $this->structureResolver->resolve($snippet);
     } catch (DocumentNotFoundException $ex) {
         return;
     }
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function getContentPath($url, $webspaceKey = null, $locale = null, $domain = null)
 {
     if ($webspaceKey !== null && $this->requestAnalyzer) {
         $portalUrls = $this->webspaceManager->findUrlsByResourceLocator($url, $this->environment, $locale ?: $this->requestAnalyzer->getCurrentLocalization()->getLocalization(), $webspaceKey, $domain);
         if (count($portalUrls) > 0) {
             return rtrim($portalUrls[0], '/');
         }
     } elseif (strpos($url, '/') === 0 && $this->requestAnalyzer) {
         return rtrim($this->requestAnalyzer->getResourceLocatorPrefix() . $url, '/');
     }
     return $url;
 }
示例#6
0
 public function getDefault($snippetType, $webspaceKey = null, $locale = null)
 {
     if (!$webspaceKey) {
         $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey();
     }
     if (!$locale) {
         $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization();
     }
     $ids = [$this->defaultSnippetManager->loadIdentifier($webspaceKey, $snippetType)];
     // to filter null default snippet
     $ids = array_filter($ids);
     return $this->snippetResolver->resolve($ids, $webspaceKey, $locale);
 }
 protected function setUp()
 {
     parent::setUp();
     $this->contentMapper = $this->prophesize('Sulu\\Component\\Content\\Mapper\\ContentMapperInterface');
     $this->requestAnalyzer = $this->prophesize('Sulu\\Component\\Webspace\\Analyzer\\RequestAnalyzerInterface');
     $this->contentTypeManager = $this->prophesize('Sulu\\Component\\Content\\ContentTypeManagerInterface');
     $this->structureManager = $this->prophesize('Sulu\\Component\\Content\\Compat\\StructureManagerInterface');
     $this->sessionManager = $this->prophesize('Sulu\\Component\\PHPCR\\SessionManager\\SessionManagerInterface');
     $this->session = $this->prophesize('PHPCR\\SessionInterface');
     $this->node = $this->prophesize('PHPCR\\NodeInterface');
     $this->parentNode = $this->prophesize('PHPCR\\NodeInterface');
     $this->startPageNode = $this->prophesize('PHPCR\\NodeInterface');
     $webspace = new Webspace();
     $webspace->setKey('sulu_test');
     $locale = new Localization();
     $locale->setCountry('us');
     $locale->setLanguage('en');
     $this->requestAnalyzer->getWebspace()->willReturn($webspace);
     $this->requestAnalyzer->getCurrentLocalization()->willReturn($locale);
     $this->contentTypeManager->get('text_line')->willReturn(new TextLine(''));
     $this->sessionManager->getSession()->willReturn($this->session->reveal());
     $this->sessionManager->getContentNode('sulu_test')->willReturn($this->startPageNode->reveal());
     $this->session->getNodeByIdentifier('123-123-123')->willReturn($this->node->reveal());
     $this->session->getNodeByIdentifier('321-321-321')->willReturn($this->parentNode->reveal());
     $this->node->getIdentifier()->willReturn('123-123-123');
     $this->node->getParent()->willReturn($this->parentNode->reveal());
     $this->node->getDepth()->willReturn(4);
     $this->parentNode->getIdentifier()->willReturn('321-321-321');
     $this->parentNode->getDepth()->willReturn(3);
     $this->startPageNode->getDepth()->willReturn(3);
     $this->structureResolver = new StructureResolver($this->contentTypeManager->reveal(), $this->structureManager->reveal());
 }
示例#8
0
 /**
  * {@inheritdoc}
  */
 public function resolve(RequestAnalyzerInterface $requestAnalyzer)
 {
     // determine default locale (if one exists)
     $defaultLocalization = $requestAnalyzer->getPortal()->getDefaultLocalization();
     $defaultLocale = $defaultLocalization ? $defaultLocalization->getLocalization() : null;
     return ['request' => ['webspaceKey' => $requestAnalyzer->getWebspace()->getKey(), 'defaultLocale' => $defaultLocale, 'locale' => $requestAnalyzer->getCurrentLocalization()->getLocalization(), 'portalUrl' => $requestAnalyzer->getPortalUrl(), 'resourceLocatorPrefix' => $requestAnalyzer->getResourceLocatorPrefix(), 'resourceLocator' => $requestAnalyzer->getResourceLocator(), 'get' => $requestAnalyzer->getGetParameters(), 'post' => $requestAnalyzer->getPostParameters(), 'analyticsKey' => $requestAnalyzer->getAnalyticsKey()]];
 }
示例#9
0
 /**
  * Finds the correct route for the current request.
  * It loads the correct data with the content mapper.
  *
  * @param Request $request A request against which to match.
  *
  * @return \Symfony\Component\Routing\RouteCollection with all Routes that
  *                                                    could potentially match $request. Empty collection if nothing can
  *                                                    match.
  */
 public function getRouteCollectionForRequest(Request $request)
 {
     $collection = new RouteCollection();
     $htmlExtension = '.html';
     $resourceLocator = $this->requestAnalyzer->getResourceLocator();
     if ($this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_REDIRECT || $this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_PARTIAL) {
         // redirect webspace correctly with language
         $collection->add('redirect_' . uniqid(), $this->getRedirectWebSpaceRoute($request));
     } elseif ($request->getRequestFormat() === 'html' && substr($request->getPathInfo(), -strlen($htmlExtension)) === $htmlExtension) {
         // redirect *.html to * (without url)
         $collection->add('redirect_' . uniqid(), $this->getRedirectRoute($request, $this->getUrlWithoutEndingTrailingSlash($resourceLocator)));
     } else {
         // just show the page
         $portal = $this->requestAnalyzer->getPortal();
         $language = $this->requestAnalyzer->getCurrentLocalization()->getLocalization();
         try {
             // load content by url ignore ending trailing slash
             $content = $this->contentMapper->loadByResourceLocator(rtrim($resourceLocator, '/'), $portal->getWebspace()->getKey(), $language);
             if (preg_match('/\\/$/', $resourceLocator) && $this->requestAnalyzer->getResourceLocatorPrefix() && $content->getNodeState() === StructureInterface::STATE_PUBLISHED) {
                 // redirect page to page without slash at the end
                 $collection->add('redirect_' . uniqid(), $this->getRedirectWebSpaceRoute($request));
             } elseif ($content->getNodeType() === Structure::NODE_TYPE_INTERNAL_LINK && $content->getNodeState() === StructureInterface::STATE_PUBLISHED) {
                 // redirect internal link
                 $collection->add($content->getKey() . '_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . $content->getResourceLocator()));
             } elseif ($content->getNodeType() === Structure::NODE_TYPE_EXTERNAL_LINK && $content->getNodeState() === StructureInterface::STATE_PUBLISHED) {
                 $collection->add($content->getKey() . '_' . uniqid(), $this->getRedirectRoute($request, $content->getResourceLocator()));
             } elseif ($content->getNodeState() === StructureInterface::STATE_TEST || !$content->getHasTranslation() || !$this->checkResourceLocator()) {
                 // error 404 page not published
                 throw new ResourceLocatorNotFoundException();
             } else {
                 // show the page
                 $collection->add($content->getKey() . '_' . uniqid(), $this->getStructureRoute($request, $content));
             }
         } catch (ResourceLocatorNotFoundException $exc) {
             // just do not add any routes to the collection
         } catch (ResourceLocatorMovedException $exc) {
             // old url resource was moved
             $collection->add($exc->getNewResourceLocatorUuid() . '_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . $exc->getNewResourceLocator()));
         } catch (RepositoryException $exc) {
             // just do not add any routes to the collection
         }
     }
     return $collection;
 }
示例#10
0
 protected function setUp()
 {
     parent::setUp();
     $this->requestAnalyzer = $this->prophesize(RequestAnalyzerInterface::class);
     $webspace = new Webspace();
     $webspace->setKey('sulu_test');
     $locale = new Localization();
     $locale->setLanguage('en');
     $portal = new Portal();
     $portal->setDefaultLocalization($locale);
     $this->requestAnalyzer->getWebspace()->willReturn($webspace);
     $this->requestAnalyzer->getPortal()->willReturn($portal);
     $this->requestAnalyzer->getCurrentLocalization()->willReturn($locale);
     $this->contentPath = $this->prophesize(ContentPathInterface::class);
     $this->contentPath->getContentPath('/test', 'sulu_test', 'de')->willReturn('/de/test');
     $this->contentPath->getContentPath('/test-en', 'sulu_test', 'en')->willReturn('/en/test-en');
     $this->contentPath->getContentPath('/test-en-us', 'sulu_test', 'en-us')->willReturn('/en/test-en-us');
     $this->contentPath->getContentPath('/test-en-us', 'sulu_test', 'en_us')->willReturn('/en/test-en-us');
     $this->contentPath->getContentPath('/test-fr', 'sulu_test', 'fr')->willReturn('/fr/test-fr');
 }
示例#11
0
 /**
  * Returns the search results for the given query.
  *
  * @param Request $request
  *
  * @return Response
  */
 public function queryAction(Request $request)
 {
     $query = $this->getRequestParameter($request, 'q', true);
     $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocale();
     $webspace = $this->requestAnalyzer->getWebspace();
     $queryString = '';
     if (strlen($query) < 3) {
         $queryString .= '+("' . self::escapeDoubleQuotes($query) . '") ';
     } else {
         $queryValues = explode(' ', $query);
         foreach ($queryValues as $queryValue) {
             if (strlen($queryValue) > 2) {
                 $queryString .= '+("' . self::escapeDoubleQuotes($queryValue) . '" OR ' . preg_replace('/([^\\pL\\s\\d])/u', '?', $queryValue) . '* OR ' . preg_replace('/([^\\pL\\s\\d])/u', '', $queryValue) . '~) ';
             } else {
                 $queryString .= '+("' . self::escapeDoubleQuotes($queryValue) . '") ';
             }
         }
     }
     $hits = $this->searchManager->createSearch($queryString)->locale($locale)->index('page_' . $webspace->getKey() . '_published')->execute();
     return $this->engine->renderResponse($webspace->getTemplate('search'), $this->parameterResolver->resolve(['query' => $query, 'hits' => $hits], $this->requestAnalyzer));
 }
示例#12
0
 /**
  * {@inheritdoc}
  */
 public function getContentPath($route, $webspaceKey = null, $locale = null, $domain = null, $scheme = null, $withoutDomain = true)
 {
     // if the request analyzer null or a route is passed which is relative or inclusive a domain nothing should be
     // done (this is important for external-links in navigations)
     if (!$this->requestAnalyzer || strpos($route, '/') !== 0) {
         return $route;
     }
     $scheme = $scheme ?: $this->requestAnalyzer->getAttribute('scheme');
     $locale = $locale ?: $this->requestAnalyzer->getCurrentLocalization()->getLocale();
     $webspaceKey = $webspaceKey ?: $this->requestAnalyzer->getWebspace()->getKey();
     $url = null;
     $host = $this->requestAnalyzer->getAttribute('host');
     if (!$domain && $this->webspaceManager->findWebspaceByKey($webspaceKey)->hasDomain($host, $this->environment)) {
         $domain = $host;
     }
     $url = $this->webspaceManager->findUrlByResourceLocator($route, $this->environment, $locale, $webspaceKey, $domain, $scheme);
     if (!$withoutDomain && !$url) {
         $url = $this->webspaceManager->findUrlByResourceLocator($route, $this->environment, $locale, $webspaceKey, null, $scheme);
     }
     return $url ?: $route;
 }
示例#13
0
 /**
  * {@inheritdoc}
  */
 public function breadcrumbFunction($uuid)
 {
     $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey();
     $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization();
     return $this->navigationMapper->getBreadcrumb($uuid, $webspaceKey, $locale);
 }
示例#14
0
 /**
  * Finds the correct route for the current request.
  * It loads the correct data with the content mapper.
  *
  * @param Request $request A request against which to match
  *
  * @return \Symfony\Component\Routing\RouteCollection with all Routes that
  *                                                    could potentially match $request. Empty collection if nothing can
  *                                                    match
  */
 public function getRouteCollectionForRequest(Request $request)
 {
     $collection = new RouteCollection();
     // no portal information without localization supported
     if ($this->requestAnalyzer->getCurrentLocalization() === null && $this->requestAnalyzer->getMatchType() !== RequestAnalyzerInterface::MATCH_TYPE_PARTIAL && $this->requestAnalyzer->getMatchType() !== RequestAnalyzerInterface::MATCH_TYPE_REDIRECT) {
         return $collection;
     }
     $htmlExtension = '.html';
     $resourceLocator = $this->requestAnalyzer->getResourceLocator();
     if ($this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_REDIRECT || $this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_PARTIAL) {
         // redirect webspace correctly with locale
         $collection->add('redirect_' . uniqid(), $this->getRedirectWebSpaceRoute());
     } elseif ($request->getRequestFormat() === 'html' && substr($request->getPathInfo(), -strlen($htmlExtension)) === $htmlExtension) {
         // redirect *.html to * (without url)
         $collection->add('redirect_' . uniqid(), $this->getRedirectRoute($request, $this->getUrlWithoutEndingTrailingSlash($resourceLocator)));
     } else {
         // just show the page
         $portal = $this->requestAnalyzer->getPortal();
         $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization();
         $resourceLocatorStrategy = $this->resourceLocatorStrategyPool->getStrategyByWebspaceKey($portal->getWebspace()->getKey());
         try {
             // load content by url ignore ending trailing slash
             /** @var PageDocument $document */
             $document = $this->documentManager->find($resourceLocatorStrategy->loadByResourceLocator(rtrim($resourceLocator, '/'), $portal->getWebspace()->getKey(), $locale), $locale, ['load_ghost_content' => false]);
             if (!$document->getTitle()) {
                 // If the title is empty the document does not exist in this locale
                 // Necessary because of https://github.com/sulu/sulu/issues/2724, otherwise locale could be checked
                 return $collection;
             }
             if (preg_match('/\\/$/', $resourceLocator) && $this->requestAnalyzer->getResourceLocatorPrefix()) {
                 // redirect page to page without slash at the end
                 $collection->add('redirect_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . rtrim($resourceLocator, '/')));
             } elseif ($document->getRedirectType() === RedirectType::INTERNAL) {
                 // redirect internal link
                 $redirectUrl = $this->requestAnalyzer->getResourceLocatorPrefix() . $document->getRedirectTarget()->getResourceSegment();
                 if ($request->getQueryString()) {
                     $redirectUrl .= '?' . $request->getQueryString();
                 }
                 $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getRedirectRoute($request, $redirectUrl));
             } elseif ($document->getRedirectType() === RedirectType::EXTERNAL) {
                 $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getRedirectRoute($request, $document->getRedirectExternal()));
             } elseif (!$this->checkResourceLocator()) {
                 return $collection;
             } else {
                 // convert the page to a StructureBridge because of BC
                 /** @var PageBridge $structure */
                 $structure = $this->structureManager->wrapStructure($this->documentInspector->getMetadata($document)->getAlias(), $this->documentInspector->getStructureMetadata($document));
                 $structure->setDocument($document);
                 // show the page
                 $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getStructureRoute($request, $structure));
             }
         } catch (ResourceLocatorNotFoundException $exc) {
             // just do not add any routes to the collection
         } catch (ResourceLocatorMovedException $exc) {
             // old url resource was moved
             $collection->add($exc->getNewResourceLocatorUuid() . '_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . $exc->getNewResourceLocator()));
         } catch (RepositoryException $exc) {
             // just do not add any routes to the collection
         }
     }
     return $collection;
 }
 /**
  * @param string $uuid
  * @return array
  */
 public function load($uuid)
 {
     $contentStructure = $this->contentMapper->load($uuid, $this->requestAnalyzer->getWebspace()->getKey(), $this->requestAnalyzer->getCurrentLocalization()->getLocalization());
     return $this->structureResolver->resolve($contentStructure);
 }