/** * Triggers URL generation for $urlResource and $parameters. * * @param mixed $urlResource Type can be anything, depending on the context. It's up to the router to pass the appropriate value to the implementor. * @param array $parameters Arbitrary hash of parameters to generate a link. * SiteAccess name can be provided as 'siteaccess' to generate a link to it (cross siteaccess link). * @param int $referenceType The type of reference to be generated (one of the constants) * * @return string */ public function generate($urlResource, array $parameters, $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) { $siteAccess = $this->siteAccess; $requestContext = $this->requestContext; // Retrieving the appropriate SiteAccess to generate the link for. if (isset($parameters['siteaccess'])) { $siteAccess = $this->siteAccessRouter->matchByName($parameters['siteaccess']); if ($siteAccess instanceof SiteAccess && $siteAccess->matcher instanceof SiteAccess\VersatileMatcher) { $requestContext = $this->getContextBySimplifiedRequest($siteAccess->matcher->getRequest()); } else { if ($this->logger) { $siteAccess = $this->siteAccess; $this->logger->notice("Could not generate a link using provided 'siteaccess' parameter: {$parameters['siteaccess']}. Generating using current context."); unset($parameters['siteaccess']); } } } $url = $this->doGenerate($urlResource, $parameters); // Add the SiteAccess URI back if needed. if ($siteAccess && $siteAccess->matcher instanceof SiteAccess\URILexer) { $url = $siteAccess->matcher->analyseLink($url); } $url = $requestContext->getBaseUrl() . $url; if ($referenceType === UrlGeneratorInterface::ABSOLUTE_URL) { $url = $this->generateAbsoluteUrl($url, $requestContext); } return $url; }
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) { $siteAccess = $this->siteAccess; $originalContext = $context = $this->getContext(); $isSiteAccessAware = $this->isSiteAccessAwareRoute($name); // Retrieving the appropriate SiteAccess to generate the link for. if (isset($parameters['siteaccess']) && $isSiteAccessAware) { $siteAccess = $this->siteAccessRouter->matchByName($parameters['siteaccess']); if ($siteAccess instanceof SiteAccess && $siteAccess->matcher instanceof SiteAccess\VersatileMatcher) { // Switch request context for link generation. $context = $this->getContextBySimplifiedRequest($siteAccess->matcher->getRequest()); $this->setContext($context); } else { if ($this->logger) { $siteAccess = $this->siteAccess; $this->logger->notice("Could not generate a link using provided 'siteaccess' parameter: {$parameters['siteaccess']}. Generating using current context."); } } unset($parameters['siteaccess']); } $url = parent::generate($name, $parameters, $referenceType); // Now putting back SiteAccess URI if needed. if ($isSiteAccessAware && $siteAccess && $siteAccess->matcher instanceof URILexer) { if ($referenceType == self::ABSOLUTE_URL || $referenceType == self::NETWORK_PATH) { $scheme = $context->getScheme(); $port = ''; if ($scheme === 'http' && $this->context->getHttpPort() != 80) { $port = ':' . $this->context->getHttpPort(); } else { if ($scheme === 'https' && $this->context->getHttpsPort() != 443) { $port = ':' . $this->context->getHttpsPort(); } } $base = $context->getHost() . $port . $context->getBaseUrl(); } else { $base = $context->getBaseUrl(); } $linkUri = $base ? substr($url, strpos($url, $base) + strlen($base)) : $url; $url = str_replace($linkUri, $siteAccess->matcher->analyseLink($linkUri), $url); } // Switch back to original context, for next links generation. $this->setContext($originalContext); return $url; }
/** * Switches configuration scope to $siteAccessName and returns the new SiteAccess to use for preview. * * @param string $siteAccessName * * @return SiteAccess */ public function changeConfigScope($siteAccessName) { $event = new ScopeChangeEvent($this->siteAccessRouter->matchByName($siteAccessName)); $this->eventDispatcher->dispatch(MVCEvents::CONFIG_SCOPE_CHANGE, $event); return $event->getSiteAccess(); }