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 |
||
45 | class UrlManager extends Component |
||
46 | { |
||
47 | /** |
||
48 | * @var bool whether to enable pretty URLs. Instead of putting all parameters in the query |
||
49 | * string part of a URL, pretty URLs allow using path info to represent some of the parameters |
||
50 | * and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of |
||
51 | * "/index.php?r=news%2Fview&id=100". |
||
52 | */ |
||
53 | public $enablePrettyUrl = false; |
||
54 | /** |
||
55 | * @var bool whether to enable strict parsing. If strict parsing is enabled, the incoming |
||
56 | * requested URL must match at least one of the [[rules]] in order to be treated as a valid request. |
||
57 | * Otherwise, the path info part of the request will be treated as the requested route. |
||
58 | * This property is used only when [[enablePrettyUrl]] is `true`. |
||
59 | */ |
||
60 | public $enableStrictParsing = false; |
||
61 | /** |
||
62 | * @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is `true`. |
||
63 | * This property is used only if [[enablePrettyUrl]] is `true`. Each element in the array |
||
64 | * is the configuration array for creating a single URL rule. The configuration will |
||
65 | * be merged with [[ruleConfig]] first before it is used for creating the rule object. |
||
66 | * |
||
67 | * A special shortcut format can be used if a rule only specifies [[UrlRule::pattern|pattern]] |
||
68 | * and [[UrlRule::route|route]]: `'pattern' => 'route'`. That is, instead of using a configuration |
||
69 | * array, one can use the key to represent the pattern and the value the corresponding route. |
||
70 | * For example, `'post/<id:\d+>' => 'post/view'`. |
||
71 | * |
||
72 | * For RESTful routing the mentioned shortcut format also allows you to specify the |
||
73 | * [[UrlRule::verb|HTTP verb]] that the rule should apply for. |
||
74 | * You can do that by prepending it to the pattern, separated by space. |
||
75 | * For example, `'PUT post/<id:\d+>' => 'post/update'`. |
||
76 | * You may specify multiple verbs by separating them with comma |
||
77 | * like this: `'POST,PUT post/index' => 'post/create'`. |
||
78 | * The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE. |
||
79 | * Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way |
||
80 | * so you normally would not specify a verb for normal GET request. |
||
81 | * |
||
82 | * Here is an example configuration for RESTful CRUD controller: |
||
83 | * |
||
84 | * ```php |
||
85 | * [ |
||
86 | * 'dashboard' => 'site/index', |
||
87 | * |
||
88 | * 'POST <controller:[\w-]+>s' => '<controller>/create', |
||
89 | * '<controller:[\w-]+>s' => '<controller>/index', |
||
90 | * |
||
91 | * 'PUT <controller:[\w-]+>/<id:\d+>' => '<controller>/update', |
||
92 | * 'DELETE <controller:[\w-]+>/<id:\d+>' => '<controller>/delete', |
||
93 | * '<controller:[\w-]+>/<id:\d+>' => '<controller>/view', |
||
94 | * ]; |
||
95 | * ``` |
||
96 | * |
||
97 | * Note that if you modify this property after the UrlManager object is created, make sure |
||
98 | * you populate the array with rule objects instead of rule configurations. |
||
99 | */ |
||
100 | public $rules = []; |
||
101 | /** |
||
102 | * @var string the URL suffix used when [[enablePrettyUrl]] is `true`. |
||
103 | * For example, ".html" can be used so that the URL looks like pointing to a static HTML page. |
||
104 | * This property is used only if [[enablePrettyUrl]] is `true`. |
||
105 | */ |
||
106 | public $suffix; |
||
107 | /** |
||
108 | * @var bool whether to show entry script name in the constructed URL. Defaults to `true`. |
||
109 | * This property is used only if [[enablePrettyUrl]] is `true`. |
||
110 | */ |
||
111 | public $showScriptName = true; |
||
112 | /** |
||
113 | * @var string the GET parameter name for route. This property is used only if [[enablePrettyUrl]] is `false`. |
||
114 | */ |
||
115 | public $routeParam = 'r'; |
||
116 | /** |
||
117 | * @var Cache|string the cache object or the application component ID of the cache object. |
||
118 | * Compiled URL rules will be cached through this cache object, if it is available. |
||
119 | * |
||
120 | * After the UrlManager object is created, if you want to change this property, |
||
121 | * you should only assign it with a cache object. |
||
122 | * Set this property to `false` if you do not want to cache the URL rules. |
||
123 | */ |
||
124 | public $cache = 'cache'; |
||
125 | /** |
||
126 | * @var array the default configuration of URL rules. Individual rule configurations |
||
127 | * specified via [[rules]] will take precedence when the same property of the rule is configured. |
||
128 | */ |
||
129 | public $ruleConfig = ['class' => UrlRule::class]; |
||
130 | /** |
||
131 | * @var UrlNormalizer|array|false the configuration for [[UrlNormalizer]] used by this UrlManager. |
||
132 | * If `false` normalization will be skipped. |
||
133 | * @since 2.0.10 |
||
134 | */ |
||
135 | public $normalizer = ['class' => UrlNormalizer::class]; |
||
136 | |||
137 | /** |
||
138 | * @var string the cache key for cached rules |
||
139 | * @since 2.0.8 |
||
140 | */ |
||
141 | protected $cacheKey = __CLASS__; |
||
142 | |||
143 | private $_baseUrl; |
||
144 | private $_scriptUrl; |
||
145 | private $_hostInfo; |
||
146 | private $_ruleCache; |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Initializes UrlManager. |
||
151 | */ |
||
152 | 43 | public function init() |
|
153 | { |
||
154 | 43 | parent::init(); |
|
155 | |||
156 | 43 | if ($this->normalizer !== false) { |
|
157 | 42 | $this->normalizer = Yii::createObject($this->normalizer); |
|
158 | 42 | if (!$this->normalizer instanceof UrlNormalizer) { |
|
159 | throw new InvalidConfigException('`' . get_class($this) . '::normalizer` should be an instance of `' . UrlNormalizer::class . '` or its DI compatible configuration.'); |
||
160 | } |
||
161 | 42 | } |
|
162 | |||
163 | 43 | if (!$this->enablePrettyUrl || empty($this->rules)) { |
|
164 | 36 | return; |
|
165 | } |
||
166 | 9 | if (is_string($this->cache)) { |
|
167 | 1 | $this->cache = Yii::$app->get($this->cache, false); |
|
168 | 1 | } |
|
169 | 9 | if ($this->cache instanceof Cache) { |
|
170 | $cacheKey = $this->cacheKey; |
||
171 | $hash = md5(json_encode($this->rules)); |
||
172 | if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) { |
||
173 | $this->rules = $data[0]; |
||
174 | } else { |
||
175 | $this->rules = $this->buildRules($this->rules); |
||
176 | $this->cache->set($cacheKey, [$this->rules, $hash]); |
||
177 | } |
||
178 | } else { |
||
179 | 9 | $this->rules = $this->buildRules($this->rules); |
|
180 | } |
||
181 | 9 | } |
|
182 | |||
183 | /** |
||
184 | * Adds additional URL rules. |
||
185 | * |
||
186 | * This method will call [[buildRules()]] to parse the given rule declarations and then append or insert |
||
187 | * them to the existing [[rules]]. |
||
188 | * |
||
189 | * Note that if [[enablePrettyUrl]] is `false`, this method will do nothing. |
||
190 | * |
||
191 | * @param array $rules the new rules to be added. Each array element represents a single rule declaration. |
||
192 | * Please refer to [[rules]] for the acceptable rule format. |
||
193 | * @param bool $append whether to add the new rules by appending them to the end of the existing rules. |
||
194 | */ |
||
195 | public function addRules($rules, $append = true) |
||
207 | |||
208 | /** |
||
209 | * Builds URL rule objects from the given rule declarations. |
||
210 | * @param array $rules the rule declarations. Each array element represents a single rule declaration. |
||
211 | * Please refer to [[rules]] for the acceptable rule formats. |
||
212 | * @return UrlRuleInterface[] the rule objects built from the given rule declarations |
||
213 | * @throws InvalidConfigException if a rule declaration is invalid |
||
214 | */ |
||
215 | 9 | protected function buildRules($rules) |
|
242 | |||
243 | /** |
||
244 | * Parses the user request. |
||
245 | * @param Request $request the request component |
||
246 | * @return array|bool the route and the associated parameters. The latter is always empty |
||
247 | * if [[enablePrettyUrl]] is `false`. `false` is returned if the current request cannot be successfully parsed. |
||
248 | */ |
||
249 | 3 | public function parseRequest($request) |
|
301 | |||
302 | /** |
||
303 | * Creates a URL using the given route and query parameters. |
||
304 | * |
||
305 | * You may specify the route as a string, e.g., `site/index`. You may also use an array |
||
306 | * if you want to specify additional query parameters for the URL being created. The |
||
307 | * array format must be: |
||
308 | * |
||
309 | * ```php |
||
310 | * // generates: /index.php?r=site%2Findex¶m1=value1¶m2=value2 |
||
311 | * ['site/index', 'param1' => 'value1', 'param2' => 'value2'] |
||
312 | * ``` |
||
313 | * |
||
314 | * If you want to create a URL with an anchor, you can use the array format with a `#` parameter. |
||
315 | * For example, |
||
316 | * |
||
317 | * ```php |
||
318 | * // generates: /index.php?r=site%2Findex¶m1=value1#name |
||
319 | * ['site/index', 'param1' => 'value1', '#' => 'name'] |
||
320 | * ``` |
||
321 | * |
||
322 | * The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL. |
||
323 | * |
||
324 | * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route |
||
325 | * as an absolute route. |
||
326 | * |
||
327 | * @param string|array $params use a string to represent a route (e.g. `site/index`), |
||
328 | * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
329 | * @return string the created URL |
||
330 | */ |
||
331 | 30 | public function createUrl($params) |
|
398 | |||
399 | /** |
||
400 | * Get URL from internal cache if exists |
||
401 | * @param string $cacheKey generated cache key to store data. |
||
402 | * @param string $route the route (e.g. `site/index`). |
||
403 | * @param array $params rule params. |
||
404 | * @return bool|string the created URL |
||
405 | * @see createUrl() |
||
406 | * @since 2.0.8 |
||
407 | */ |
||
408 | 7 | protected function getUrlFromCache($cacheKey, $route, $params) |
|
422 | |||
423 | /** |
||
424 | * Store rule (e.g. [[UrlRule]]) to internal cache |
||
425 | * @param $cacheKey |
||
426 | * @param UrlRuleInterface $rule |
||
427 | * @since 2.0.8 |
||
428 | */ |
||
429 | 6 | protected function setRuleToCache($cacheKey, UrlRuleInterface $rule) |
|
433 | |||
434 | /** |
||
435 | * Creates an absolute URL using the given route and query parameters. |
||
436 | * |
||
437 | * This method prepends the URL created by [[createUrl()]] with the [[hostInfo]]. |
||
438 | * |
||
439 | * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route |
||
440 | * as an absolute route. |
||
441 | * |
||
442 | * @param string|array $params use a string to represent a route (e.g. `site/index`), |
||
443 | * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
444 | * @param string $scheme the scheme to use for the url (either `http` or `https`). If not specified |
||
445 | * the scheme of the current request will be used. |
||
446 | * @return string the created URL |
||
447 | * @see createUrl() |
||
448 | */ |
||
449 | 13 | public function createAbsoluteUrl($params, $scheme = null) |
|
462 | |||
463 | /** |
||
464 | * Returns the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
465 | * It defaults to [[Request::baseUrl]]. |
||
466 | * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`. |
||
467 | * @return string the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
468 | * @throws InvalidConfigException if running in console application and [[baseUrl]] is not configured. |
||
469 | */ |
||
470 | 5 | public function getBaseUrl() |
|
483 | |||
484 | /** |
||
485 | * Sets the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
486 | * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`. |
||
487 | * @param string $value the base URL that is used by [[createUrl()]] to prepend to created URLs. |
||
488 | */ |
||
489 | 13 | public function setBaseUrl($value) |
|
493 | |||
494 | /** |
||
495 | * Returns the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
496 | * It defaults to [[Request::scriptUrl]]. |
||
497 | * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`. |
||
498 | * @return string the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
499 | * @throws InvalidConfigException if running in console application and [[scriptUrl]] is not configured. |
||
500 | */ |
||
501 | 27 | public function getScriptUrl() |
|
514 | |||
515 | /** |
||
516 | * Sets the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
517 | * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`. |
||
518 | * @param string $value the entry script URL that is used by [[createUrl()]] to prepend to created URLs. |
||
519 | */ |
||
520 | 21 | public function setScriptUrl($value) |
|
524 | |||
525 | /** |
||
526 | * Returns the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
527 | * @return string the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
528 | * @throws InvalidConfigException if running in console application and [[hostInfo]] is not configured. |
||
529 | */ |
||
530 | 13 | public function getHostInfo() |
|
543 | |||
544 | /** |
||
545 | * Sets the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
546 | * @param string $value the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs. |
||
547 | */ |
||
548 | 9 | public function setHostInfo($value) |
|
552 | } |
||
553 |