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 |
||
46 | class UrlManager extends Component |
||
47 | { |
||
48 | /** |
||
49 | * @var bool whether to enable pretty URLs. Instead of putting all parameters in the query |
||
50 | * string part of a URL, pretty URLs allow using path info to represent some of the parameters |
||
51 | * and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of |
||
52 | * "/index.php?r=news%2Fview&id=100". |
||
53 | */ |
||
54 | public $enablePrettyUrl = false; |
||
55 | /** |
||
56 | * @var bool whether to enable strict parsing. If strict parsing is enabled, the incoming |
||
57 | * requested URL must match at least one of the [[rules]] in order to be treated as a valid request. |
||
58 | * Otherwise, the path info part of the request will be treated as the requested route. |
||
59 | * This property is used only when [[enablePrettyUrl]] is `true`. |
||
60 | */ |
||
61 | public $enableStrictParsing = false; |
||
62 | /** |
||
63 | * @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is `true`. |
||
64 | * This property is used only if [[enablePrettyUrl]] is `true`. Each element in the array |
||
65 | * is the configuration array for creating a single URL rule. The configuration will |
||
66 | * be merged with [[ruleConfig]] first before it is used for creating the rule object. |
||
67 | * |
||
68 | * A special shortcut format can be used if a rule only specifies [[UrlRule::pattern|pattern]] |
||
69 | * and [[UrlRule::route|route]]: `'pattern' => 'route'`. That is, instead of using a configuration |
||
70 | * array, one can use the key to represent the pattern and the value the corresponding route. |
||
71 | * For example, `'post/<id:\d+>' => 'post/view'`. |
||
72 | * |
||
73 | * For RESTful routing the mentioned shortcut format also allows you to specify the |
||
74 | * [[UrlRule::verb|HTTP verb]] that the rule should apply for. |
||
75 | * You can do that by prepending it to the pattern, separated by space. |
||
76 | * For example, `'PUT post/<id:\d+>' => 'post/update'`. |
||
77 | * You may specify multiple verbs by separating them with comma |
||
78 | * like this: `'POST,PUT post/index' => 'post/create'`. |
||
79 | * The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE. |
||
80 | * Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way |
||
81 | * so you normally would not specify a verb for normal GET request. |
||
82 | * |
||
83 | * Here is an example configuration for RESTful CRUD controller: |
||
84 | * |
||
85 | * ```php |
||
86 | * [ |
||
87 | * 'dashboard' => 'site/index', |
||
88 | * |
||
89 | * 'POST <controller:[\w-]+>s' => '<controller>/create', |
||
90 | * '<controller:[\w-]+>s' => '<controller>/index', |
||
91 | * |
||
92 | * 'PUT <controller:[\w-]+>/<id:\d+>' => '<controller>/update', |
||
93 | * 'DELETE <controller:[\w-]+>/<id:\d+>' => '<controller>/delete', |
||
94 | * '<controller:[\w-]+>/<id:\d+>' => '<controller>/view', |
||
95 | * ]; |
||
96 | * ``` |
||
97 | * |
||
98 | * Note that if you modify this property after the UrlManager object is created, make sure |
||
99 | * you populate the array with rule objects instead of rule configurations. |
||
100 | */ |
||
101 | public $rules = []; |
||
102 | /** |
||
103 | * @var string the URL suffix used when [[enablePrettyUrl]] is `true`. |
||
104 | * For example, ".html" can be used so that the URL looks like pointing to a static HTML page. |
||
105 | * This property is used only if [[enablePrettyUrl]] is `true`. |
||
106 | */ |
||
107 | public $suffix; |
||
108 | /** |
||
109 | * @var bool whether to show entry script name in the constructed URL. Defaults to `true`. |
||
110 | * This property is used only if [[enablePrettyUrl]] is `true`. |
||
111 | */ |
||
112 | public $showScriptName = true; |
||
113 | /** |
||
114 | * @var string the GET parameter name for route. This property is used only if [[enablePrettyUrl]] is `false`. |
||
115 | */ |
||
116 | public $routeParam = 'r'; |
||
117 | /** |
||
118 | * @var Cache|string the cache object or the application component ID of the cache object. |
||
119 | * Compiled URL rules will be cached through this cache object, if it is available. |
||
120 | * |
||
121 | * After the UrlManager object is created, if you want to change this property, |
||
122 | * you should only assign it with a cache object. |
||
123 | * Set this property to `false` if you do not want to cache the URL rules. |
||
124 | */ |
||
125 | public $cache = 'cache'; |
||
126 | /** |
||
127 | * @var array the default configuration of URL rules. Individual rule configurations |
||
128 | * specified via [[rules]] will take precedence when the same property of the rule is configured. |
||
129 | */ |
||
130 | public $ruleConfig = ['class' => 'yii\web\UrlRule']; |
||
131 | /** |
||
132 | * @var UrlNormalizer|array|string|false the configuration for [[UrlNormalizer]] used by this UrlManager. |
||
133 | * The default value is `false`, which means normalization will be skipped. |
||
134 | * If you wish to enable URL normalization, you should configure this property manually. |
||
135 | * For example: |
||
136 | * |
||
137 | * ```php |
||
138 | * [ |
||
139 | * 'class' => 'yii\web\UrlNormalizer', |
||
140 | * 'collapseSlashes' => true, |
||
141 | * 'normalizeTrailingSlash' => true, |
||
142 | * ] |
||
143 | * ``` |
||
144 | * |
||
145 | * @since 2.0.10 |
||
146 | */ |
||
147 | public $normalizer = false; |
||
148 | |||
149 | /** |
||
150 | * @var string the cache key for cached rules |
||
151 | * @since 2.0.8 |
||
152 | */ |
||
153 | protected $cacheKey = __CLASS__; |
||
154 | |||
155 | private $_baseUrl; |
||
156 | private $_scriptUrl; |
||
157 | private $_hostInfo; |
||
158 | private $_ruleCache; |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Initializes UrlManager. |
||
163 | */ |
||
164 | 45 | public function init() |
|
165 | { |
||
166 | 45 | parent::init(); |
|
167 | |||
168 | 45 | if ($this->normalizer !== false) { |
|
169 | 2 | $this->normalizer = Yii::createObject($this->normalizer); |
|
170 | 2 | if (!$this->normalizer instanceof UrlNormalizer) { |
|
171 | throw new InvalidConfigException('`' . get_class($this) . '::normalizer` should be an instance of `' . UrlNormalizer::className() . '` or its DI compatible configuration.'); |
||
172 | } |
||
173 | 2 | } |
|
174 | |||
175 | 45 | if (!$this->enablePrettyUrl || empty($this->rules)) { |
|
176 | 38 | return; |
|
177 | } |
||
178 | 9 | if (is_string($this->cache)) { |
|
179 | 1 | $this->cache = Yii::$app->get($this->cache, false); |
|
180 | 1 | } |
|
181 | 9 | if ($this->cache instanceof Cache) { |
|
182 | $cacheKey = $this->cacheKey; |
||
183 | $hash = md5(json_encode($this->rules)); |
||
184 | if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) { |
||
185 | $this->rules = $data[0]; |
||
186 | } else { |
||
187 | $this->rules = $this->buildRules($this->rules); |
||
188 | $this->cache->set($cacheKey, [$this->rules, $hash]); |
||
189 | } |
||
190 | } else { |
||
191 | 9 | $this->rules = $this->buildRules($this->rules); |
|
192 | } |
||
193 | 9 | } |
|
194 | |||
195 | /** |
||
196 | * Adds additional URL rules. |
||
197 | * |
||
198 | * This method will call [[buildRules()]] to parse the given rule declarations and then append or insert |
||
199 | * them to the existing [[rules]]. |
||
200 | * |
||
201 | * Note that if [[enablePrettyUrl]] is `false`, this method will do nothing. |
||
202 | * |
||
203 | * @param array $rules the new rules to be added. Each array element represents a single rule declaration. |
||
204 | * Please refer to [[rules]] for the acceptable rule format. |
||
205 | * @param bool $append whether to add the new rules by appending them to the end of the existing rules. |
||
206 | */ |
||
207 | public function addRules($rules, $append = true) |
||
219 | |||
220 | /** |
||
221 | * Builds URL rule objects from the given rule declarations. |
||
222 | * @param array $rules the rule declarations. Each array element represents a single rule declaration. |
||
223 | * Please refer to [[rules]] for the acceptable rule formats. |
||
224 | * @return UrlRuleInterface[] the rule objects built from the given rule declarations |
||
225 | * @throws InvalidConfigException if a rule declaration is invalid |
||
226 | */ |
||
227 | 9 | protected function buildRules($rules) |
|
254 | |||
255 | /** |
||
256 | * Parses the user request. |
||
257 | * @param Request $request the request component |
||
258 | * @return array|bool the route and the associated parameters. The latter is always empty |
||
259 | * if [[enablePrettyUrl]] is `false`. `false` is returned if the current request cannot be successfully parsed. |
||
260 | */ |
||
261 | 3 | public function parseRequest($request) |
|
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 | 32 | public function createUrl($params) |
|
410 | |||
411 | /** |
||
412 | * Get URL from internal cache if exists |
||
413 | * @param string $cacheKey generated cache key to store data. |
||
414 | * @param string $route the route (e.g. `site/index`). |
||
415 | * @param array $params rule params. |
||
416 | * @return bool|string the created URL |
||
417 | * @see createUrl() |
||
418 | * @since 2.0.8 |
||
419 | */ |
||
420 | 7 | protected function getUrlFromCache($cacheKey, $route, $params) |
|
434 | |||
435 | /** |
||
436 | * Store rule (e.g. [[UrlRule]]) to internal cache |
||
437 | * @param $cacheKey |
||
438 | * @param UrlRuleInterface $rule |
||
439 | * @since 2.0.8 |
||
440 | */ |
||
441 | 6 | protected function setRuleToCache($cacheKey, UrlRuleInterface $rule) |
|
445 | |||
446 | /** |
||
447 | * Creates an absolute URL using the given route and query parameters. |
||
448 | * |
||
449 | * This method prepends the URL created by [[createUrl()]] with the [[hostInfo]]. |
||
450 | * |
||
451 | * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route |
||
452 | * as an absolute route. |
||
453 | * |
||
454 | * @param string|array $params use a string to represent a route (e.g. `site/index`), |
||
455 | * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
456 | * @param string|null $scheme the scheme to use for the URL (either `http`, `https` or empty string |
||
457 | * for protocol-relative URL). |
||
458 | * If not specified the scheme of the current request will be used. |
||
459 | * @return string the created URL |
||
460 | * @see createUrl() |
||
461 | */ |
||
462 | 13 | public function createAbsoluteUrl($params, $scheme = null) |
|
472 | |||
473 | /** |
||
474 | * Returns the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
475 | * It defaults to [[Request::baseUrl]]. |
||
476 | * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`. |
||
477 | * @return string the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
478 | * @throws InvalidConfigException if running in console application and [[baseUrl]] is not configured. |
||
479 | */ |
||
480 | 5 | public function getBaseUrl() |
|
493 | |||
494 | /** |
||
495 | * Sets the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
496 | * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`. |
||
497 | * @param string $value the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
498 | */ |
||
499 | 13 | public function setBaseUrl($value) |
|
503 | |||
504 | /** |
||
505 | * Returns the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
506 | * It defaults to [[Request::scriptUrl]]. |
||
507 | * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`. |
||
508 | * @return string the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
509 | * @throws InvalidConfigException if running in console application and [[scriptUrl]] is not configured. |
||
510 | */ |
||
511 | 29 | public function getScriptUrl() |
|
524 | |||
525 | /** |
||
526 | * Sets the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
527 | * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`. |
||
528 | * @param string $value the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
529 | */ |
||
530 | 23 | public function setScriptUrl($value) |
|
534 | |||
535 | /** |
||
536 | * Returns the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
537 | * @return string the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
538 | * @throws InvalidConfigException if running in console application and [[hostInfo]] is not configured. |
||
539 | */ |
||
540 | 13 | public function getHostInfo() |
|
553 | |||
554 | /** |
||
555 | * Sets the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
556 | * @param string $value the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
557 | */ |
||
558 | 9 | public function setHostInfo($value) |
|
562 | } |
||
563 |