Complex classes like UrlManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 UrlManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class UrlManager extends Component |
||
50 | { |
||
51 | /** |
||
52 | * @var bool whether to enable pretty URLs. Instead of putting all parameters in the query |
||
53 | * string part of a URL, pretty URLs allow using path info to represent some of the parameters |
||
54 | * and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of |
||
55 | * "/index.php?r=news%2Fview&id=100". |
||
56 | */ |
||
57 | public $enablePrettyUrl = false; |
||
58 | /** |
||
59 | * @var bool whether to enable strict parsing. If strict parsing is enabled, the incoming |
||
60 | * requested URL must match at least one of the [[rules]] in order to be treated as a valid request. |
||
61 | * Otherwise, the path info part of the request will be treated as the requested route. |
||
62 | * This property is used only when [[enablePrettyUrl]] is `true`. |
||
63 | */ |
||
64 | public $enableStrictParsing = false; |
||
65 | /** |
||
66 | * @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is `true`. |
||
67 | * This property is used only if [[enablePrettyUrl]] is `true`. Each element in the array |
||
68 | * is the configuration array for creating a single URL rule. The configuration will |
||
69 | * be merged with [[ruleConfig]] first before it is used for creating the rule object. |
||
70 | * |
||
71 | * A special shortcut format can be used if a rule only specifies [[UrlRule::pattern|pattern]] |
||
72 | * and [[UrlRule::route|route]]: `'pattern' => 'route'`. That is, instead of using a configuration |
||
73 | * array, one can use the key to represent the pattern and the value the corresponding route. |
||
74 | * For example, `'post/<id:\d+>' => 'post/view'`. |
||
75 | * |
||
76 | * For RESTful routing the mentioned shortcut format also allows you to specify the |
||
77 | * [[UrlRule::verb|HTTP verb]] that the rule should apply for. |
||
78 | * You can do that by prepending it to the pattern, separated by space. |
||
79 | * For example, `'PUT post/<id:\d+>' => 'post/update'`. |
||
80 | * You may specify multiple verbs by separating them with comma |
||
81 | * like this: `'POST,PUT post/index' => 'post/create'`. |
||
82 | * The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE. |
||
83 | * Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way |
||
84 | * so you normally would not specify a verb for normal GET request. |
||
85 | * |
||
86 | * Here is an example configuration for RESTful CRUD controller: |
||
87 | * |
||
88 | * ```php |
||
89 | * [ |
||
90 | * 'dashboard' => 'site/index', |
||
91 | * |
||
92 | * 'POST <controller:[\w-]+>s' => '<controller>/create', |
||
93 | * '<controller:[\w-]+>s' => '<controller>/index', |
||
94 | * |
||
95 | * 'PUT <controller:[\w-]+>/<id:\d+>' => '<controller>/update', |
||
96 | * 'DELETE <controller:[\w-]+>/<id:\d+>' => '<controller>/delete', |
||
97 | * '<controller:[\w-]+>/<id:\d+>' => '<controller>/view', |
||
98 | * ]; |
||
99 | * ``` |
||
100 | * |
||
101 | * Note that if you modify this property after the UrlManager object is created, make sure |
||
102 | * you populate the array with rule objects instead of rule configurations. |
||
103 | */ |
||
104 | public $rules = []; |
||
105 | /** |
||
106 | * @var string the URL suffix used when [[enablePrettyUrl]] is `true`. |
||
107 | * For example, ".html" can be used so that the URL looks like pointing to a static HTML page. |
||
108 | * This property is used only if [[enablePrettyUrl]] is `true`. |
||
109 | */ |
||
110 | public $suffix; |
||
111 | /** |
||
112 | * @var bool whether to show entry script name in the constructed URL. Defaults to `true`. |
||
113 | * This property is used only if [[enablePrettyUrl]] is `true`. |
||
114 | */ |
||
115 | public $showScriptName = true; |
||
116 | /** |
||
117 | * @var string the GET parameter name for route. This property is used only if [[enablePrettyUrl]] is `false`. |
||
118 | */ |
||
119 | public $routeParam = 'r'; |
||
120 | /** |
||
121 | * @var CacheInterface|string the cache object or the application component ID of the cache object. |
||
122 | * Compiled URL rules will be cached through this cache object, if it is available. |
||
123 | * |
||
124 | * After the UrlManager object is created, if you want to change this property, |
||
125 | * you should only assign it with a cache object. |
||
126 | * Set this property to `false` if you do not want to cache the URL rules. |
||
127 | */ |
||
128 | public $cache = 'cache'; |
||
129 | /** |
||
130 | * @var array the default configuration of URL rules. Individual rule configurations |
||
131 | * specified via [[rules]] will take precedence when the same property of the rule is configured. |
||
132 | */ |
||
133 | public $ruleConfig = ['class' => UrlRule::class]; |
||
134 | /** |
||
135 | * @var UrlNormalizer|array|false the configuration for [[UrlNormalizer]] used by this UrlManager. |
||
136 | * If `false` normalization will be skipped. |
||
137 | * @since 2.0.10 |
||
138 | */ |
||
139 | public $normalizer = ['class' => UrlNormalizer::class]; |
||
140 | |||
141 | /** |
||
142 | * @var string the cache key for cached rules |
||
143 | * @since 2.0.8 |
||
144 | */ |
||
145 | protected $cacheKey = __CLASS__; |
||
146 | |||
147 | private $_baseUrl; |
||
148 | private $_scriptUrl; |
||
149 | private $_hostInfo; |
||
150 | private $_ruleCache; |
||
151 | |||
152 | |||
153 | /** |
||
154 | * Initializes UrlManager. |
||
155 | */ |
||
156 | 169 | public function init() |
|
157 | { |
||
158 | 169 | parent::init(); |
|
159 | |||
160 | 169 | if ($this->normalizer !== false) { |
|
161 | 158 | $this->normalizer = Yii::createObject($this->normalizer); |
|
162 | 158 | if (!$this->normalizer instanceof UrlNormalizer) { |
|
163 | throw new InvalidConfigException('`' . get_class($this) . '::normalizer` should be an instance of `' . UrlNormalizer::class . '` or its DI compatible configuration.'); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | 169 | if (!$this->enablePrettyUrl || empty($this->rules)) { |
|
168 | 97 | return; |
|
169 | } |
||
170 | 73 | if (is_string($this->cache)) { |
|
171 | 1 | $this->cache = Yii::$app->get($this->cache, false); |
|
172 | } |
||
173 | 73 | if ($this->cache instanceof CacheInterface) { |
|
174 | $cacheKey = $this->cacheKey; |
||
175 | $hash = md5(json_encode([$this->ruleConfig, $this->rules])); |
||
176 | if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) { |
||
177 | $this->rules = $data[0]; |
||
178 | } else { |
||
179 | $this->rules = $this->buildRules($this->rules); |
||
180 | $this->cache->set($cacheKey, [$this->rules, $hash]); |
||
181 | } |
||
182 | } else { |
||
183 | 73 | $this->rules = $this->buildRules($this->rules); |
|
184 | } |
||
185 | 73 | } |
|
186 | |||
187 | /** |
||
188 | * Adds additional URL rules. |
||
189 | * |
||
190 | * This method will call [[buildRules()]] to parse the given rule declarations and then append or insert |
||
191 | * them to the existing [[rules]]. |
||
192 | * |
||
193 | * Note that if [[enablePrettyUrl]] is `false`, this method will do nothing. |
||
194 | * |
||
195 | * @param array $rules the new rules to be added. Each array element represents a single rule declaration. |
||
196 | * Please refer to [[rules]] for the acceptable rule format. |
||
197 | * @param bool $append whether to add the new rules by appending them to the end of the existing rules. |
||
198 | */ |
||
199 | public function addRules($rules, $append = true) |
||
211 | |||
212 | /** |
||
213 | * Builds URL rule objects from the given rule declarations. |
||
214 | * @param array $rules the rule declarations. Each array element represents a single rule declaration. |
||
215 | * Please refer to [[rules]] for the acceptable rule formats. |
||
216 | * @return UrlRuleInterface[] the rule objects built from the given rule declarations |
||
217 | * @throws InvalidConfigException if a rule declaration is invalid |
||
218 | */ |
||
219 | 73 | protected function buildRules($rules) |
|
246 | |||
247 | /** |
||
248 | * Parses the user request. |
||
249 | * @param Request $request the request component |
||
250 | * @return array|bool the route and the associated parameters. The latter is always empty |
||
251 | * if [[enablePrettyUrl]] is `false`. `false` is returned if the current request cannot be successfully parsed. |
||
252 | */ |
||
253 | 16 | public function parseRequest($request) |
|
254 | { |
||
255 | 16 | if ($this->enablePrettyUrl) { |
|
256 | /* @var $rule UrlRule */ |
||
257 | 13 | foreach ($this->rules as $rule) { |
|
258 | 9 | $result = $rule->parseRequest($this, $request); |
|
259 | 9 | if (YII_DEBUG) { |
|
260 | 9 | Yii::trace([ |
|
261 | 9 | 'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule), |
|
262 | 'match' => $result !== false, |
||
263 | 'parent' => null, |
||
264 | 9 | ], __METHOD__); |
|
265 | } |
||
266 | 9 | if ($result !== false) { |
|
267 | 9 | return $result; |
|
268 | } |
||
269 | } |
||
270 | |||
271 | 11 | if ($this->enableStrictParsing) { |
|
272 | 4 | return false; |
|
273 | } |
||
274 | |||
275 | 7 | Yii::trace('No matching URL rules. Using default URL parsing logic.', __METHOD__); |
|
276 | |||
277 | 7 | $suffix = (string) $this->suffix; |
|
278 | 7 | $pathInfo = $request->getPathInfo(); |
|
279 | 7 | $normalized = false; |
|
280 | 7 | if ($this->normalizer !== false) { |
|
281 | 1 | $pathInfo = $this->normalizer->normalizePathInfo($pathInfo, $suffix, $normalized); |
|
282 | } |
||
283 | 7 | if ($suffix !== '' && $pathInfo !== '') { |
|
284 | 4 | $n = strlen($this->suffix); |
|
285 | 4 | if (substr_compare($pathInfo, $this->suffix, -$n, $n) === 0) { |
|
286 | 4 | $pathInfo = substr($pathInfo, 0, -$n); |
|
287 | 4 | if ($pathInfo === '') { |
|
288 | // suffix alone is not allowed |
||
289 | 4 | return false; |
|
290 | } |
||
291 | } else { |
||
292 | // suffix doesn't match |
||
293 | 4 | return false; |
|
294 | } |
||
295 | } |
||
296 | |||
297 | 7 | if ($normalized) { |
|
298 | // pathInfo was changed by normalizer - we need also normalize route |
||
299 | 1 | return $this->normalizer->normalizeRoute([$pathInfo, []]); |
|
300 | } |
||
301 | |||
302 | 6 | return [$pathInfo, []]; |
|
303 | } |
||
304 | |||
305 | 3 | Yii::trace('Pretty URL not enabled. Using default URL parsing logic.', __METHOD__); |
|
306 | 3 | $route = $request->getQueryParam($this->routeParam, ''); |
|
307 | 3 | if (is_array($route)) { |
|
308 | 3 | $route = ''; |
|
309 | } |
||
310 | |||
311 | 3 | return [(string) $route, []]; |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Creates a URL using the given route and query parameters. |
||
316 | * |
||
317 | * You may specify the route as a string, e.g., `site/index`. You may also use an array |
||
318 | * if you want to specify additional query parameters for the URL being created. The |
||
319 | * array format must be: |
||
320 | * |
||
321 | * ```php |
||
322 | * // generates: /index.php?r=site%2Findex¶m1=value1¶m2=value2 |
||
323 | * ['site/index', 'param1' => 'value1', 'param2' => 'value2'] |
||
324 | * ``` |
||
325 | * |
||
326 | * If you want to create a URL with an anchor, you can use the array format with a `#` parameter. |
||
327 | * For example, |
||
328 | * |
||
329 | * ```php |
||
330 | * // generates: /index.php?r=site%2Findex¶m1=value1#name |
||
331 | * ['site/index', 'param1' => 'value1', '#' => 'name'] |
||
332 | * ``` |
||
333 | * |
||
334 | * The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL. |
||
335 | * |
||
336 | * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route |
||
337 | * as an absolute route. |
||
338 | * |
||
339 | * @param string|array $params use a string to represent a route (e.g. `site/index`), |
||
340 | * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
341 | * @return string the created URL |
||
342 | */ |
||
343 | 129 | public function createUrl($params) |
|
344 | { |
||
345 | 129 | $params = (array) $params; |
|
346 | 129 | $anchor = isset($params['#']) ? '#' . $params['#'] : ''; |
|
347 | 129 | unset($params['#'], $params[$this->routeParam]); |
|
348 | |||
349 | 129 | $route = trim($params[0], '/'); |
|
350 | 129 | unset($params[0]); |
|
351 | |||
352 | 129 | $baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl(); |
|
353 | |||
354 | 129 | if ($this->enablePrettyUrl) { |
|
355 | 80 | $cacheKey = $route . '?'; |
|
356 | 80 | foreach ($params as $key => $value) { |
|
357 | 80 | if ($value !== null) { |
|
358 | 80 | $cacheKey .= $key . '&'; |
|
359 | } |
||
360 | } |
||
361 | |||
362 | 80 | $url = $this->getUrlFromCache($cacheKey, $route, $params); |
|
363 | 80 | if ($url === false) { |
|
364 | /* @var $rule UrlRule */ |
||
365 | 80 | foreach ($this->rules as $rule) { |
|
366 | 64 | if (in_array($rule, $this->_ruleCache[$cacheKey], true)) { |
|
367 | // avoid redundant calls of `UrlRule::createUrl()` for rules checked in `getUrlFromCache()` |
||
368 | // @see https://github.com/yiisoft/yii2/issues/14094 |
||
369 | 21 | continue; |
|
370 | } |
||
371 | 64 | $url = $rule->createUrl($this, $route, $params); |
|
372 | 64 | if ($this->canBeCached($rule)) { |
|
373 | 63 | $this->setRuleToCache($cacheKey, $rule); |
|
374 | } |
||
375 | 64 | if ($url !== false) { |
|
376 | 64 | break; |
|
377 | } |
||
378 | } |
||
379 | } |
||
380 | |||
381 | 80 | if ($url !== false) { |
|
382 | 63 | if (strpos($url, '://') !== false) { |
|
383 | 8 | if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) { |
|
384 | 3 | return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor; |
|
385 | } |
||
386 | |||
387 | 5 | return $url . $baseUrl . $anchor; |
|
388 | 55 | } elseif (strpos($url, '//') === 0) { |
|
389 | 4 | if ($baseUrl !== '' && ($pos = strpos($url, '/', 2)) !== false) { |
|
390 | 3 | return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor; |
|
391 | } |
||
392 | |||
393 | 4 | return $url . $baseUrl . $anchor; |
|
394 | } |
||
395 | |||
396 | 51 | $url = ltrim($url, '/'); |
|
397 | 51 | return "$baseUrl/{$url}{$anchor}"; |
|
398 | } |
||
399 | |||
400 | 65 | if ($this->suffix !== null) { |
|
401 | 24 | $route .= $this->suffix; |
|
402 | } |
||
403 | 65 | if (!empty($params) && ($query = http_build_query($params)) !== '') { |
|
404 | 49 | $route .= '?' . $query; |
|
405 | } |
||
406 | |||
407 | 65 | $route = ltrim($route, '/'); |
|
408 | 65 | return "$baseUrl/{$route}{$anchor}"; |
|
409 | } |
||
410 | |||
411 | 49 | $url = "$baseUrl?{$this->routeParam}=" . urlencode($route); |
|
412 | 49 | if (!empty($params) && ($query = http_build_query($params)) !== '') { |
|
413 | 39 | $url .= '&' . $query; |
|
414 | } |
||
415 | |||
416 | 49 | return $url . $anchor; |
|
417 | } |
||
418 | |||
419 | /** |
||
420 | * Returns the value indicating whether result of [[createUrl()]] of rule should be cached in internal cache. |
||
421 | * |
||
422 | * @param UrlRuleInterface $rule |
||
423 | * @return bool `true` if result should be cached, `false` if not. |
||
424 | * @since 2.0.12 |
||
425 | * @see getUrlFromCache() |
||
426 | * @see setRuleToCache() |
||
427 | * @see UrlRule::getCreateUrlStatus() |
||
428 | */ |
||
429 | 64 | protected function canBeCached(UrlRuleInterface $rule) |
|
438 | |||
439 | /** |
||
440 | * Get URL from internal cache if exists |
||
441 | * @param string $cacheKey generated cache key to store data. |
||
442 | * @param string $route the route (e.g. `site/index`). |
||
443 | * @param array $params rule params. |
||
444 | * @return bool|string the created URL |
||
445 | * @see createUrl() |
||
446 | * @since 2.0.8 |
||
447 | */ |
||
448 | 80 | protected function getUrlFromCache($cacheKey, $route, $params) |
|
462 | |||
463 | /** |
||
464 | * Store rule (e.g. [[UrlRule]]) to internal cache |
||
465 | * @param $cacheKey |
||
466 | * @param UrlRuleInterface $rule |
||
467 | * @since 2.0.8 |
||
468 | */ |
||
469 | 63 | protected function setRuleToCache($cacheKey, UrlRuleInterface $rule) |
|
473 | |||
474 | /** |
||
475 | * Creates an absolute URL using the given route and query parameters. |
||
476 | * |
||
477 | * This method prepends the URL created by [[createUrl()]] with the [[hostInfo]]. |
||
478 | * |
||
479 | * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route |
||
480 | * as an absolute route. |
||
481 | * |
||
482 | * @param string|array $params use a string to represent a route (e.g. `site/index`), |
||
483 | * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
484 | * @param string|null $scheme the scheme to use for the URL (either `http`, `https` or empty string |
||
485 | * for protocol-relative URL). |
||
486 | * If not specified the scheme of the current request will be used. |
||
487 | * @return string the created URL |
||
488 | * @see createUrl() |
||
489 | */ |
||
490 | 58 | public function createAbsoluteUrl($params, $scheme = null) |
|
505 | |||
506 | /** |
||
507 | * Returns the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
508 | * It defaults to [[Request::baseUrl]]. |
||
509 | * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`. |
||
510 | * @return string the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
511 | * @throws InvalidConfigException if running in console application and [[baseUrl]] is not configured. |
||
512 | */ |
||
513 | 41 | public function getBaseUrl() |
|
526 | |||
527 | /** |
||
528 | * Sets the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
529 | * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`. |
||
530 | * @param string $value the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
531 | */ |
||
532 | 112 | public function setBaseUrl($value) |
|
536 | |||
537 | /** |
||
538 | * Returns the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
539 | * It defaults to [[Request::scriptUrl]]. |
||
540 | * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`. |
||
541 | * @return string the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
542 | * @throws InvalidConfigException if running in console application and [[scriptUrl]] is not configured. |
||
543 | */ |
||
544 | 89 | public function getScriptUrl() |
|
557 | |||
558 | /** |
||
559 | * Sets the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
560 | * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`. |
||
561 | * @param string $value the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
562 | */ |
||
563 | 121 | public function setScriptUrl($value) |
|
567 | |||
568 | /** |
||
569 | * Returns the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
570 | * @return string the host info (e.g. `http://www.example.com`) that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
571 | * @throws InvalidConfigException if running in console application and [[hostInfo]] is not configured. |
||
572 | */ |
||
573 | 56 | public function getHostInfo() |
|
586 | |||
587 | /** |
||
588 | * Sets the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
589 | * @param string $value the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
590 | */ |
||
591 | 109 | public function setHostInfo($value) |
|
595 | } |
||
596 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.