1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||||
6 | */ |
||||
7 | |||||
8 | namespace Spryker\Shared\Application\Routing; |
||||
9 | |||||
10 | use Silex\Application; |
||||
11 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||||
12 | use Symfony\Component\Routing\Generator\UrlGenerator; |
||||
13 | use Symfony\Component\Routing\RequestContext; |
||||
14 | use Symfony\Component\Routing\RouteCollection; |
||||
15 | use Symfony\Component\Routing\RouterInterface; |
||||
16 | |||||
17 | abstract class AbstractRouter implements RouterInterface |
||||
18 | { |
||||
19 | /** |
||||
20 | * @var \Silex\Application |
||||
21 | */ |
||||
22 | protected $app; |
||||
23 | |||||
24 | /** |
||||
25 | * @var bool|null |
||||
26 | */ |
||||
27 | protected $sslEnabled; |
||||
28 | |||||
29 | /** |
||||
30 | * @var \Symfony\Component\Routing\RequestContext|null |
||||
31 | */ |
||||
32 | protected $context; |
||||
33 | |||||
34 | /** |
||||
35 | * Set the sslEnabledFlag to |
||||
36 | * true to force ssl |
||||
37 | * false to force http |
||||
38 | * null to not force anything (both https or http allowed) |
||||
39 | * |
||||
40 | * @param \Silex\Application $app |
||||
41 | * @param bool|null $sslEnabled |
||||
42 | */ |
||||
43 | public function __construct(Application $app, $sslEnabled = null) |
||||
44 | { |
||||
45 | $this->app = $app; |
||||
46 | $this->sslEnabled = $sslEnabled; |
||||
47 | } |
||||
48 | |||||
49 | /** |
||||
50 | * {@inheritDoc} |
||||
51 | * |
||||
52 | * @param \Symfony\Component\Routing\RequestContext $context |
||||
53 | * |
||||
54 | * @return void |
||||
55 | */ |
||||
56 | public function setContext(RequestContext $context): void |
||||
57 | { |
||||
58 | $this->context = $context; |
||||
59 | } |
||||
60 | |||||
61 | /** |
||||
62 | * @inheritDoc |
||||
63 | */ |
||||
64 | public function getContext(): RequestContext |
||||
65 | { |
||||
66 | return $this->context; |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
67 | } |
||||
68 | |||||
69 | /** |
||||
70 | * @inheritDoc |
||||
71 | */ |
||||
72 | public function getRouteCollection() |
||||
73 | { |
||||
74 | return new RouteCollection(); |
||||
75 | } |
||||
76 | |||||
77 | /** |
||||
78 | * @return bool |
||||
79 | */ |
||||
80 | public function isSslEnabled() |
||||
81 | { |
||||
82 | return $this->sslEnabled; |
||||
83 | } |
||||
84 | |||||
85 | /** |
||||
86 | * @param string $pathInfo |
||||
87 | * |
||||
88 | * @return array|null |
||||
89 | */ |
||||
90 | protected function checkScheme($pathInfo) |
||||
91 | { |
||||
92 | $wantedScheme = $this->isSslEnabled() ? 'https' : 'http'; |
||||
93 | if ($this->getContext()->getScheme() !== $wantedScheme) { |
||||
94 | $url = $wantedScheme . '://' . $this->context->getHost() . $pathInfo; |
||||
0 ignored issues
–
show
The method
getHost() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
95 | |||||
96 | return [ |
||||
97 | '_controller' => function ($url) { |
||||
98 | return new RedirectResponse($url, 301); |
||||
99 | }, |
||||
100 | '_route' => null, |
||||
101 | 'url' => $url, |
||||
102 | ]; |
||||
103 | } |
||||
104 | |||||
105 | return null; |
||||
106 | } |
||||
107 | |||||
108 | /** |
||||
109 | * @param string $pathInfo |
||||
110 | * @param string|int $referenceType |
||||
111 | * |
||||
112 | * @return string |
||||
113 | */ |
||||
114 | protected function getUrlOrPathForType($pathInfo, $referenceType) |
||||
115 | { |
||||
116 | $url = $pathInfo; |
||||
117 | $scheme = $this->context->getScheme(); |
||||
118 | |||||
119 | if ( |
||||
120 | $referenceType !== static::NETWORK_PATH && |
||||
121 | ($scheme === 'http' && $this->sslEnabled === true || $scheme === 'https' && $this->sslEnabled === false) |
||||
122 | ) { |
||||
123 | $referenceType = static::ABSOLUTE_URL; |
||||
124 | } |
||||
125 | |||||
126 | switch ($referenceType) { |
||||
127 | case static::ABSOLUTE_URL: |
||||
128 | case static::NETWORK_PATH: |
||||
129 | $url = $this->buildUrl($pathInfo, $referenceType); |
||||
130 | |||||
131 | break; |
||||
132 | case static::ABSOLUTE_PATH: |
||||
133 | $url = $pathInfo; |
||||
134 | |||||
135 | break; |
||||
136 | case static::RELATIVE_PATH: |
||||
137 | $url = UrlGenerator::getRelativePath($this->context->getPathInfo(), $pathInfo); |
||||
138 | |||||
139 | break; |
||||
140 | } |
||||
141 | |||||
142 | return $url; |
||||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * @param string $pathInfo |
||||
147 | * @param string|int $referenceType |
||||
148 | * |
||||
149 | * @return string |
||||
150 | */ |
||||
151 | private function buildUrl($pathInfo, $referenceType) |
||||
152 | { |
||||
153 | $scheme = $this->getScheme(); |
||||
154 | $port = $this->getPortPart($scheme); |
||||
155 | $schemeAuthority = $referenceType === static::NETWORK_PATH ? '//' : "$scheme://"; |
||||
156 | $schemeAuthority .= $this->context->getHost() . $port; |
||||
157 | |||||
158 | return $schemeAuthority . $this->context->getBaseUrl() . $pathInfo; |
||||
159 | } |
||||
160 | |||||
161 | /** |
||||
162 | * @param string $scheme |
||||
163 | * |
||||
164 | * @return string |
||||
165 | */ |
||||
166 | private function getPortPart($scheme) |
||||
167 | { |
||||
168 | $port = ''; |
||||
169 | if ($scheme === 'http' && $this->context->getHttpPort() !== 80) { |
||||
170 | $port = ':' . $this->context->getHttpPort(); |
||||
171 | } elseif ($scheme === 'https' && $this->context->getHttpsPort() !== 443) { |
||||
172 | $port = ':' . $this->context->getHttpsPort(); |
||||
173 | } |
||||
174 | |||||
175 | return $port; |
||||
176 | } |
||||
177 | |||||
178 | /** |
||||
179 | * @return string |
||||
180 | */ |
||||
181 | private function getScheme() |
||||
182 | { |
||||
183 | $scheme = $this->context->getScheme(); |
||||
184 | if (is_bool($this->sslEnabled)) { |
||||
185 | $scheme = ($this->sslEnabled) ? 'https' : 'http'; |
||||
186 | } |
||||
187 | |||||
188 | return $scheme; |
||||
189 | } |
||||
190 | } |
||||
191 |