Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

MVC/Symfony/Routing/Generator.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\Routing;
8
9 use eZ\Publish\Core\MVC\Symfony\SiteAccess;
10 use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
11 use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessRouterInterface;
12 use Psr\Log\LoggerInterface;
13 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14 use Symfony\Component\Routing\RequestContext;
15
19 abstract class Generator implements SiteAccessAware
20 {
22 protected $requestContext;
23
25 protected $siteAccessRouter;
26
28 protected $siteAccess;
29
31 protected $logger;
32
36 public function setRequestContext(RequestContext $requestContext)
37 {
38 $this->requestContext = $requestContext;
39 }
40
44 public function setSiteAccessRouter(SiteAccessRouterInterface $siteAccessRouter)
45 {
46 $this->siteAccessRouter = $siteAccessRouter;
47 }
48
52 public function setSiteAccess(SiteAccess $siteAccess = null)
53 {
54 $this->siteAccess = $siteAccess;
55 }
56
60 public function setLogger(LoggerInterface $logger = null)
61 {
62 $this->logger = $logger;
63 }
64
75 public function generate($urlResource, array $parameters, $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
76 {
77 $siteAccess = $this->siteAccess;
78 $requestContext = $this->requestContext;
79
80 // Retrieving the appropriate SiteAccess to generate the link for.
81 if (isset($parameters['siteaccess'])) {
82 $siteAccess = $this->siteAccessRouter->matchByName($parameters['siteaccess']);
83 if ($siteAccess instanceof SiteAccess && $siteAccess->matcher instanceof SiteAccess\VersatileMatcher) {
84 $requestContext = $this->getContextBySimplifiedRequest($siteAccess->matcher->getRequest());
85 } elseif ($this->logger) {
86 $siteAccess = $this->siteAccess;
87 $this->logger->notice("Could not generate a link using provided 'siteaccess' parameter: {$parameters['siteaccess']}. Generating using current context.");
88 unset($parameters['siteaccess']);
89 }
90 }
91
92 $url = $this->doGenerate($urlResource, $parameters);
93
94 // Add the SiteAccess URI back if needed.
95 if ($siteAccess && $siteAccess->matcher instanceof SiteAccess\URILexer) {
96 $url = $siteAccess->matcher->analyseLink($url);
97 }
98
99 $url = $requestContext->getBaseUrl() . $url;
100
101 if ($referenceType === UrlGeneratorInterface::ABSOLUTE_URL) {
102 $url = $this->generateAbsoluteUrl($url, $requestContext);
103 }
104
105 return $url;
106 }
107
116 abstract public function doGenerate($urlResource, array $parameters);
117
126 protected function generateAbsoluteUrl($uri, RequestContext $requestContext)
127 {
128 $scheme = $requestContext->getScheme();
129 $port = '';
130 if ($scheme === 'http' && $requestContext->getHttpPort() != 80) {
131 $port = ':' . $requestContext->getHttpPort();
132 } elseif ($scheme === 'https' && $requestContext->getHttpsPort() != 443) {
133 $port = ':' . $requestContext->getHttpsPort();
134 }
135
136 return $scheme . '://' . $requestContext->getHost() . $port . $uri;
137 }
138
146 private function getContextBySimplifiedRequest(SimplifiedRequest $simplifiedRequest)
147 {
148 $context = clone $this->requestContext;
149 if ($simplifiedRequest->scheme) {
150 $context->setScheme($simplifiedRequest->scheme);
151 }
152
153 if ($simplifiedRequest->port) {
154 switch ($simplifiedRequest->scheme) {
155 case 'https':
156 $context->setHttpsPort($simplifiedRequest->port);
157 break;
158 default:
159 $context->setHttpPort($simplifiedRequest->port);
160 break;
161 }
162 }
163
164 if ($simplifiedRequest->host) {
165 $context->setHost($simplifiedRequest->host);
166 }
167
168 if ($simplifiedRequest->pathinfo) {
169 $context->setPathInfo($simplifiedRequest->pathinfo);
170 }
171
172 return $context;
173 }
174 }
eZ\Publish\Core\MVC\Symfony\Routing\Generator
Base class for eZ Publish Url generation.
Definition: MVC/Symfony/Routing/Generator.php:20
eZ\Publish\Core\MVC\Symfony\Routing\Generator\generate
generate($urlResource, array $parameters, $referenceType=UrlGeneratorInterface::ABSOLUTE_PATH)
Triggers URL generation for $urlResource and $parameters.
Definition: MVC/Symfony/Routing/Generator.php:75
eZ\Publish\Core\MVC\Symfony\Routing\Generator\$siteAccess
$siteAccess
Definition: MVC/Symfony/Routing/Generator.php:28
eZ\Publish\Core\MVC\Symfony\Routing\Generator\$logger
$logger
Definition: MVC/Symfony/Routing/Generator.php:31
eZ\Publish\Core\MVC\Symfony\Routing\Generator\setLogger
setLogger(LoggerInterface $logger=null)
Definition: MVC/Symfony/Routing/Generator.php:60
eZ\Publish\Core\MVC\Symfony\Routing\Generator\setSiteAccess
setSiteAccess(SiteAccess $siteAccess=null)
Definition: MVC/Symfony/Routing/Generator.php:52
eZ\Publish\Core\MVC\Symfony\Routing\Generator\setSiteAccessRouter
setSiteAccessRouter(SiteAccessRouterInterface $siteAccessRouter)
Definition: MVC/Symfony/Routing/Generator.php:44
eZ\Publish\Core\MVC\Symfony\Routing\Generator\$siteAccessRouter
$siteAccessRouter
Definition: MVC/Symfony/Routing/Generator.php:25
eZ\Publish\Core\MVC\Symfony\Routing\Generator\getContextBySimplifiedRequest
getContextBySimplifiedRequest(SimplifiedRequest $simplifiedRequest)
Merges context from $simplifiedRequest into a clone of the current context.
Definition: MVC/Symfony/Routing/Generator.php:146
eZ\Publish\Core\MVC\Symfony\Routing\Generator\setRequestContext
setRequestContext(RequestContext $requestContext)
Definition: MVC/Symfony/Routing/Generator.php:36
eZ\Publish\Core\MVC\Symfony\Routing\Generator\generateAbsoluteUrl
generateAbsoluteUrl($uri, RequestContext $requestContext)
Generates an absolute URL from $uri and the request context.
Definition: MVC/Symfony/Routing/Generator.php:126
eZ\Publish\Core\MVC\Symfony\Routing\Generator\doGenerate
doGenerate($urlResource, array $parameters)
Generates the URL from $urlResource and $parameters.
eZ\Publish\Core\MVC\Symfony\Routing\Generator\$requestContext
$requestContext
Definition: MVC/Symfony/Routing/Generator.php:22
eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest
Definition: SimplifiedRequest.php:21
eZ\Publish\Core\MVC\Symfony\SiteAccess
Base struct for a siteaccess representation.
Definition: SiteAccess.php:16
eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware
Interface for SiteAccess aware services.
Definition: SiteAccessAware.php:15
eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessRouterInterface
Definition: SiteAccessRouterInterface.php:12
eZ\Publish\Core\MVC\Symfony\SiteAccess\URILexer
Interface for SiteAccess matchers that need to alter the URI after matching.
Definition: URILexer.php:14
eZ\Publish\Core\MVC\Symfony\SiteAccess\VersatileMatcher
Interface for SiteAccess matchers.
Definition: VersatileMatcher.php:16
eZ\Publish\Core\MVC\Symfony\Routing
Definition: ChainRouter.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.