/** * {@inheritdoc} */ public function loadParent($uuid) { $session = $this->sessionManager->getSession(); $contentsNode = $this->sessionManager->getContentNode($this->requestAnalyzer->getWebspace()->getKey()); $node = $session->getNodeByIdentifier($uuid); if ($node->getDepth() <= $contentsNode->getDepth()) { throw new ParentNotFoundException($uuid); } return $this->load($node->getParent()->getIdentifier()); }
/** * {@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(); }
/** * {@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; } }
/** * {@inheritdoc} */ public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) { $code = $exception->getStatusCode(); $showException = $request->attributes->get('showException', $this->debug); $template = $this->requestAnalyzer->getWebspace()->getTheme()->getErrorTemplate($code); if ($showException || $request->getRequestFormat() !== 'html' || $template === null) { return parent::showAction($request, $exception, $logger); } $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); $parameter = ['status_code' => $code, 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', 'exception' => $exception, 'currentContent' => $currentContent]; $data = $this->parameterResolver->resolve($parameter, $this->requestAnalyzer); return new Response($this->twig->render($template, $data), $code); }
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); }
/** * Renders all the SEO tags supported by Sulu. * * @param array $seoExtension The values delivered by the SEO extension of Sulu * @param array $content The content of the current page * @param string[] $urls All the localized URLs for the current page * @param string $shadowBaseLocale The displayed language, in case the current page is a shadow * * @return string The rendered HTML tags of the SEO extension */ public function renderSeoTags(array $seoExtension, array $content, array $urls, $shadowBaseLocale) { $html = ''; // FIXME this is only necessary because we have to set a default parameter $webspace = $this->requestAnalyzer->getWebspace(); $webspaceKey = null; if ($webspace) { $webspaceKey = $webspace->getKey(); } $html .= $this->renderTitle($seoExtension, $content); $html .= $this->renderMetaTags($seoExtension); $html .= $this->renderAlternateLinks($urls, $webspaceKey); $html .= $this->renderCanonicalTag($seoExtension, $urls, $shadowBaseLocale, $webspaceKey); return $html; }
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()); }
/** * {@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()]]; }
/** * Returns the security system. * * @return string */ protected function getSystem() { $system = $this->suluSystem; if ($this->requestAnalyzer != null && $this->requestAnalyzer->getWebspace() !== null && $this->requestAnalyzer->getWebspace()->getSecurity() !== null) { // if the request analyzer is available, overwrite the system $system = $this->requestAnalyzer->getWebspace()->getSecurity()->getSystem(); } return $system; }
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'); }
/** * Returns alternate link HTML tags with href-lang attributes. * * @param array $urls * * @return string */ public function getAlternateLinks($urls) { // determine default and current values $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey(); $currentPortal = $this->requestAnalyzer->getPortal(); $defaultLocale = null; if ($currentPortal !== null && ($defaultLocale = $currentPortal->getDefaultLocalization()) !== null) { $defaultLocale = $defaultLocale->getLocalization(); } $result = []; foreach ($urls as $locale => $url) { if ($url !== null) { if ($locale === $defaultLocale) { $result[] = $this->getAlternate($url, $webspaceKey, $locale, true); } $result[] = $this->getAlternate($url, $webspaceKey, $locale); } } return implode(PHP_EOL, $result); }
/** * 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)); }
/** * {@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; }
/** * @dataProvider provideSeoData * * @param $seoExtension * @param $content * @param $urls * @param $defaultLocale * @param $shadowBaseLocale * @param $expectedResults * @param array $unexpectedResults */ public function testRenderSeoTags($seoExtension, $content, $urls, $defaultLocale, $shadowBaseLocale, $expectedResults, $unexpectedResults = []) { /** @var Localization $localization */ $localization = $this->prophesize(Localization::class); $localization->getLocalization()->willReturn($defaultLocale); /** @var Portal $portal */ $portal = $this->prophesize(Portal::class); $portal->getDefaultLocalization()->willReturn($localization->reveal()); $this->requestAnalyzer->getPortal()->willReturn($portal->reveal()); $webspace = $this->prophesize(Webspace::class); $this->requestAnalyzer->getWebspace()->willReturn($webspace); $this->contentPath->getContentPath(Argument::cetera())->will(function ($arguments) { return '/' . str_replace('_', '-', $arguments[2]) . $arguments[0]; }); $result = $this->seoTwigExtension->renderSeoTags($seoExtension, $content, $urls, $shadowBaseLocale); foreach ($expectedResults as $expectedResult) { $this->assertContains($expectedResult, $result); } foreach ($unexpectedResults as $unexpectedResult) { $this->assertNotContains($unexpectedResult, $result); } }
/** * Returns alternate link HTML tags with href-lang attributes. * * @param array $urls * * @return string * * @deprecated since 1.1 use SeoTwigExtension::renderSeoTags - sulu_seo */ public function getAlternateLinks($urls) { // determine default and current values $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey(); $currentPortal = $this->requestAnalyzer->getPortal(); $defaultLocale = null; if ($currentPortal !== null && ($defaultLocale = $currentPortal->getXDefaultLocalization()) !== null) { $defaultLocale = $defaultLocale->getLocalization(); } $result = []; foreach ($urls as $locale => $url) { // url = '/' means that there is no translation for this page // the only exception is the homepage where the requested resource-locator is '/' if ($url !== '/' || $this->requestAnalyzer->getResourceLocator() === '/') { if ($locale === $defaultLocale) { $result[] = $this->getAlternate($url, $webspaceKey, $locale, true); } $result[] = $this->getAlternate($url, $webspaceKey, $locale); } } return implode(PHP_EOL, $result); }
/** * {@inheritdoc} */ public function getRouteCollectionForRequest(Request $request) { $collection = new RouteCollection(); $routeDocument = $this->requestAnalyzer->getAttribute('customUrlRoute'); $customUrlDocument = $this->requestAnalyzer->getAttribute('customUrl'); $localization = $this->requestAnalyzer->getAttribute('localization'); if (null === $routeDocument || null === $localization) { return $collection; } if ($routeDocument->isHistory()) { // if custom-url is not redirect to avoid double redirects. if (!$routeDocument->getTargetDocument()->getTargetDocument()->isRedirect()) { return $this->addHistoryRedirectToRouteCollection($request, $routeDocument, $collection, $this->requestAnalyzer->getWebspace()->getKey()); } $routeDocument = $routeDocument->getTargetDocument(); $customUrlDocument = $routeDocument->getTargetDocument(); } if ($customUrlDocument === null || $customUrlDocument->isPublished() === false || $customUrlDocument->getTargetDocument() !== null && $customUrlDocument->getTargetDocument()->getWorkflowStage() !== WorkflowStage::PUBLISHED) { return $collection; } $collection->add(uniqid('custom_url_route_', true), new Route($request->getPathInfo(), ['_custom_url' => $customUrlDocument, '_webspace' => $this->requestAnalyzer->getWebspace(), '_environment' => $this->environment])); return $collection; }
/** * {@inheritdoc} */ public function breadcrumbFunction($uuid) { $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey(); $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization(); return $this->navigationMapper->getBreadcrumb($uuid, $webspaceKey, $locale); }
/** * @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); }