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 Cache|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' => 'yii\web\UrlRule']; |
||
134 | /** |
||
135 | * @var UrlNormalizer|array|string|false the configuration for [[UrlNormalizer]] used by this UrlManager. |
||
136 | * The default value is `false`, which means normalization will be skipped. |
||
137 | * If you wish to enable URL normalization, you should configure this property manually. |
||
138 | * For example: |
||
139 | * |
||
140 | * ```php |
||
141 | * [ |
||
142 | * 'class' => 'yii\web\UrlNormalizer', |
||
143 | * 'collapseSlashes' => true, |
||
144 | * 'normalizeTrailingSlash' => true, |
||
145 | * ] |
||
146 | * ``` |
||
147 | * |
||
148 | * @since 2.0.10 |
||
149 | */ |
||
150 | public $normalizer = false; |
||
151 | |||
152 | /** |
||
153 | * @var string the cache key for cached rules |
||
154 | * @since 2.0.8 |
||
155 | */ |
||
156 | protected $cacheKey = __CLASS__; |
||
157 | |||
158 | private $_baseUrl; |
||
159 | private $_scriptUrl; |
||
160 | private $_hostInfo; |
||
161 | private $_ruleCache; |
||
162 | |||
163 | |||
164 | /** |
||
165 | * Initializes UrlManager. |
||
166 | */ |
||
167 | public function init() |
||
197 | |||
198 | /** |
||
199 | * Adds additional URL rules. |
||
200 | * |
||
201 | * This method will call [[buildRules()]] to parse the given rule declarations and then append or insert |
||
202 | * them to the existing [[rules]]. |
||
203 | * |
||
204 | * Note that if [[enablePrettyUrl]] is `false`, this method will do nothing. |
||
205 | * |
||
206 | * @param array $rules the new rules to be added. Each array element represents a single rule declaration. |
||
207 | * Please refer to [[rules]] for the acceptable rule format. |
||
208 | * @param bool $append whether to add the new rules by appending them to the end of the existing rules. |
||
209 | */ |
||
210 | public function addRules($rules, $append = true) |
||
222 | |||
223 | /** |
||
224 | * Builds URL rule objects from the given rule declarations. |
||
225 | * @param array $rules the rule declarations. Each array element represents a single rule declaration. |
||
226 | * Please refer to [[rules]] for the acceptable rule formats. |
||
227 | * @return UrlRuleInterface[] the rule objects built from the given rule declarations |
||
228 | * @throws InvalidConfigException if a rule declaration is invalid |
||
229 | */ |
||
230 | protected function buildRules($rules) |
||
257 | |||
258 | /** |
||
259 | * Parses the user request. |
||
260 | * @param Request $request the request component |
||
261 | * @return array|bool the route and the associated parameters. The latter is always empty |
||
262 | * if [[enablePrettyUrl]] is `false`. `false` is returned if the current request cannot be successfully parsed. |
||
263 | */ |
||
264 | public function parseRequest($request) |
||
324 | |||
325 | /** |
||
326 | * Creates a URL using the given route and query parameters. |
||
327 | * |
||
328 | * You may specify the route as a string, e.g., `site/index`. You may also use an array |
||
329 | * if you want to specify additional query parameters for the URL being created. The |
||
330 | * array format must be: |
||
331 | * |
||
332 | * ```php |
||
333 | * // generates: /index.php?r=site%2Findex¶m1=value1¶m2=value2 |
||
334 | * ['site/index', 'param1' => 'value1', 'param2' => 'value2'] |
||
335 | * ``` |
||
336 | * |
||
337 | * If you want to create a URL with an anchor, you can use the array format with a `#` parameter. |
||
338 | * For example, |
||
339 | * |
||
340 | * ```php |
||
341 | * // generates: /index.php?r=site%2Findex¶m1=value1#name |
||
342 | * ['site/index', 'param1' => 'value1', '#' => 'name'] |
||
343 | * ``` |
||
344 | * |
||
345 | * The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL. |
||
346 | * |
||
347 | * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route |
||
348 | * as an absolute route. |
||
349 | * |
||
350 | * @param string|array $params use a string to represent a route (e.g. `site/index`), |
||
351 | * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
352 | * @return string the created URL |
||
353 | */ |
||
354 | public function createUrl($params) |
||
429 | |||
430 | /** |
||
431 | * Get URL from internal cache if exists |
||
432 | * @param string $cacheKey generated cache key to store data. |
||
433 | * @param string $route the route (e.g. `site/index`). |
||
434 | * @param array $params rule params. |
||
435 | * @return bool|string the created URL |
||
436 | * @see createUrl() |
||
437 | * @since 2.0.8 |
||
438 | */ |
||
439 | protected function getUrlFromCache($cacheKey, $route, $params) |
||
453 | |||
454 | /** |
||
455 | * Store rule (e.g. [[UrlRule]]) to internal cache |
||
456 | * @param $cacheKey |
||
457 | * @param UrlRuleInterface $rule |
||
458 | * @since 2.0.8 |
||
459 | */ |
||
460 | protected function setRuleToCache($cacheKey, UrlRuleInterface $rule) |
||
464 | |||
465 | /** |
||
466 | * Creates an absolute URL using the given route and query parameters. |
||
467 | * |
||
468 | * This method prepends the URL created by [[createUrl()]] with the [[hostInfo]]. |
||
469 | * |
||
470 | * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route |
||
471 | * as an absolute route. |
||
472 | * |
||
473 | * @param string|array $params use a string to represent a route (e.g. `site/index`), |
||
474 | * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
475 | * @param string|null $scheme the scheme to use for the URL (either `http`, `https` or empty string |
||
476 | * for protocol-relative URL). |
||
477 | * If not specified the scheme of the current request will be used. |
||
478 | * @return string the created URL |
||
479 | * @see createUrl() |
||
480 | */ |
||
481 | public function createAbsoluteUrl($params, $scheme = null) |
||
496 | |||
497 | /** |
||
498 | * Returns the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
499 | * It defaults to [[Request::baseUrl]]. |
||
500 | * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`. |
||
501 | * @return string the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
502 | * @throws InvalidConfigException if running in console application and [[baseUrl]] is not configured. |
||
503 | */ |
||
504 | public function getBaseUrl() |
||
517 | |||
518 | /** |
||
519 | * Sets the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
520 | * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`. |
||
521 | * @param string $value the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
522 | */ |
||
523 | public function setBaseUrl($value) |
||
527 | |||
528 | /** |
||
529 | * Returns the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
530 | * It defaults to [[Request::scriptUrl]]. |
||
531 | * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`. |
||
532 | * @return string the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
533 | * @throws InvalidConfigException if running in console application and [[scriptUrl]] is not configured. |
||
534 | */ |
||
535 | public function getScriptUrl() |
||
548 | |||
549 | /** |
||
550 | * Sets the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
551 | * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`. |
||
552 | * @param string $value the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
553 | */ |
||
554 | public function setScriptUrl($value) |
||
558 | |||
559 | /** |
||
560 | * Returns the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
561 | * @return string the host info (e.g. `http://www.example.com`) that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
562 | * @throws InvalidConfigException if running in console application and [[hostInfo]] is not configured. |
||
563 | */ |
||
564 | public function getHostInfo() |
||
577 | |||
578 | /** |
||
579 | * Sets the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
580 | * @param string $value the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
581 | */ |
||
582 | public function setHostInfo($value) |
||
586 | } |
||
587 |