Total Complexity | 59 |
Total Lines | 303 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 2 | Features | 1 |
Complex classes like UrlHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UrlHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class UrlHelper extends CraftUrlHelper |
||
28 | { |
||
29 | // Public Static Properties |
||
30 | // ========================================================================= |
||
31 | |||
32 | // Public Static Methods |
||
33 | // ========================================================================= |
||
34 | |||
35 | /** |
||
36 | * @inheritDoc |
||
37 | */ |
||
38 | public static function siteUrl(string $path = '', $params = null, string $scheme = null, int $siteId = null): string |
||
39 | { |
||
40 | try { |
||
41 | $siteUrl = self::getSiteUrlOverrideSetting($siteId); |
||
42 | } catch (Throwable $e) { |
||
43 | // That's okay |
||
44 | } |
||
45 | if (!empty($siteUrl)) { |
||
46 | $siteUrl = MetaValue::parseString($siteUrl); |
||
47 | // Extract out just the path part |
||
48 | $parts = self::decomposeUrl($path); |
||
49 | $path = $parts['path'] . $parts['suffix']; |
||
50 | $url = self::mergeUrlWithPath($siteUrl, $path); |
||
51 | // Handle trailing slashes properly for generated URLs |
||
52 | $generalConfig = Craft::$app->getConfig()->getGeneral(); |
||
53 | if ($generalConfig->addTrailingSlashesToUrls && !preg_match('/(.+\?.*)|(\.[^\/]+$)/', $url)) { |
||
54 | $url = rtrim($url, '/') . '/'; |
||
55 | } |
||
56 | if (!$generalConfig->addTrailingSlashesToUrls) { |
||
57 | $url = rtrim($url, '/'); |
||
58 | } |
||
59 | |||
60 | return DynamicMeta::sanitizeUrl(parent::urlWithParams($url, $params ?? []), false, false); |
||
61 | } |
||
62 | |||
63 | return DynamicMeta::sanitizeUrl(parent::siteUrl($path, $params, $scheme, $siteId), false, false); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Merge the $url and $path together, combining any overlapping path segments |
||
68 | * |
||
69 | * @param string $url |
||
70 | * @param string $path |
||
71 | * @return string |
||
72 | */ |
||
73 | public static function mergeUrlWithPath(string $url, string $path): string |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Return the page trigger and the value of the page trigger (null if it doesn't exist) |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | public static function pageTriggerValue(): array |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Return an absolute URL with protocol that curl will be happy with |
||
119 | * |
||
120 | * @param string $url |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public static function absoluteUrlWithProtocol($url): string |
||
125 | { |
||
126 | // Make this a full URL |
||
127 | if (!self::isAbsoluteUrl($url)) { |
||
128 | $protocol = 'http'; |
||
129 | if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1) |
||
|
|||
130 | || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0 |
||
131 | ) { |
||
132 | $protocol = 'https'; |
||
133 | } |
||
134 | if (self::isProtocolRelativeUrl($url)) { |
||
135 | try { |
||
136 | $url = self::urlWithScheme($url, $protocol); |
||
137 | } catch (SiteNotFoundException $e) { |
||
138 | Craft::error($e->getMessage(), __METHOD__); |
||
139 | } |
||
140 | } else { |
||
141 | try { |
||
142 | $url = self::siteUrl($url, null, $protocol); |
||
143 | if (self::isProtocolRelativeUrl($url)) { |
||
144 | $url = self::urlWithScheme($url, $protocol); |
||
145 | } |
||
146 | } catch (Exception $e) { |
||
147 | Craft::error($e->getMessage(), __METHOD__); |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | // Ensure that any spaces in the URL are encoded |
||
152 | $url = str_replace(' ', '%20', $url); |
||
153 | // If the incoming URL has a trailing slash, respect it by preserving it |
||
154 | $preserveTrailingSlash = false; |
||
155 | if (str_ends_with($url, '/')) { |
||
156 | $preserveTrailingSlash = true; |
||
157 | } |
||
158 | // Handle trailing slashes properly for generated URLs |
||
159 | $generalConfig = Craft::$app->getConfig()->getGeneral(); |
||
160 | if ($generalConfig->addTrailingSlashesToUrls && !preg_match('/(.+\?.*)|(\.[^\/]+$)/', $url)) { |
||
161 | $url = rtrim($url, '/') . '/'; |
||
162 | } |
||
163 | if (!$generalConfig->addTrailingSlashesToUrls && (!$preserveTrailingSlash || self::urlIsSiteIndex($url))) { |
||
164 | $url = rtrim($url, '/'); |
||
165 | } |
||
166 | |||
167 | return DynamicMeta::sanitizeUrl($url, false, false); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * urlencode() just the query parameters in the URL |
||
172 | * |
||
173 | * @param string $url |
||
174 | * @return string |
||
175 | */ |
||
176 | public static function encodeUrlQueryParams(string $url): string |
||
177 | { |
||
178 | $urlParts = parse_url($url); |
||
179 | $encodedUrl = ""; |
||
180 | if (isset($urlParts['scheme'])) { |
||
181 | $encodedUrl .= $urlParts['scheme'] . '://'; |
||
182 | } |
||
183 | if (isset($urlParts['host'])) { |
||
184 | $encodedUrl .= $urlParts['host']; |
||
185 | } |
||
186 | if (isset($urlParts['port'])) { |
||
187 | $encodedUrl .= ':' . $urlParts['port']; |
||
188 | } |
||
189 | if (isset($urlParts['path'])) { |
||
190 | $encodedUrl .= $urlParts['path']; |
||
191 | } |
||
192 | if (isset($urlParts['query'])) { |
||
193 | $query = explode('&', $urlParts['query']); |
||
194 | foreach ($query as $j => $value) { |
||
195 | $value = explode('=', $value, 2); |
||
196 | if (count($value) === 2) { |
||
197 | $query[$j] = urlencode($value[0]) . '=' . urlencode($value[1]); |
||
198 | } else { |
||
199 | $query[$j] = urlencode($value[0]); |
||
200 | } |
||
201 | } |
||
202 | $encodedUrl .= '?' . implode('&', $query); |
||
203 | } |
||
204 | if (isset($urlParts['fragment'])) { |
||
205 | $encodedUrl .= '#' . $urlParts['fragment']; |
||
206 | } |
||
207 | |||
208 | return $encodedUrl; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Return whether this URL has a sub-directory as part of it |
||
213 | * |
||
214 | * @param string $url |
||
215 | * @return bool |
||
216 | */ |
||
217 | public static function urlHasSubDir(string $url): bool |
||
218 | { |
||
219 | return !empty(parse_url(trim($url, '/'), PHP_URL_PATH)); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * See if the url is a site index, and if so, strip the trailing slash |
||
224 | * ref: https://github.com/craftcms/cms/issues/5675 |
||
225 | * |
||
226 | * @param string $url |
||
227 | * @return bool |
||
228 | */ |
||
229 | public static function urlIsSiteIndex(string $url): bool |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Return the siteUrlOverride setting, which can be a string or an array of site URLs |
||
252 | * indexed by the site handle |
||
253 | * |
||
254 | * @param int|null $siteId |
||
255 | * @return string |
||
256 | * @throws Exception |
||
257 | * @throws SiteNotFoundException |
||
258 | */ |
||
259 | public static function getSiteUrlOverrideSetting(?int $siteId = null): string |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Encodes non-alphanumeric characters in a URL, except reserved characters and already-encoded characters. |
||
283 | * |
||
284 | * @param string $url |
||
285 | * @return string |
||
286 | * @since 4.13.0 |
||
287 | */ |
||
288 | public static function encodeUrl(string $url): string |
||
289 | { |
||
290 | $parts = preg_split('/([:\/?#\[\]@!$&\'()*+,;=%])/', $url, flags: PREG_SPLIT_DELIM_CAPTURE); |
||
291 | $url = ''; |
||
292 | foreach ($parts as $i => $part) { |
||
293 | if ($i % 2 === 0) { |
||
294 | $url .= urlencode($part); |
||
295 | } else { |
||
296 | $url .= $part; |
||
297 | } |
||
298 | } |
||
299 | return $url; |
||
300 | } |
||
301 | |||
302 | // Protected Methods |
||
303 | // ========================================================================= |
||
304 | |||
305 | /** |
||
306 | * Decompose a url into a prefix, path, and suffix |
||
307 | * |
||
308 | * @param $pathOrUrl |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | protected static function decomposeUrl($pathOrUrl): array |
||
330 | } |
||
331 | } |
||
332 |