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 |
||
| 21 | class UrlManager extends \yii\web\UrlManager |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var boolean Pretty urls are enabled by default and can not be turned off in luya cms context. |
||
| 25 | */ |
||
| 26 | public $enablePrettyUrl = true; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var boolean As mod rewrite is required by a LUYA cms instance the script name must be turned off by default. |
||
| 30 | */ |
||
| 31 | public $showScriptName = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array The default url rule configuration uses the {{\luya\web\UrlRule}} class. |
||
| 35 | */ |
||
| 36 | public $ruleConfig = ['class' => 'luya\web\UrlRule']; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var integer In order to build urls, the nav item id from cms module can be stored in the UrlManager as `$contextNavItemId`. |
||
| 40 | * |
||
| 41 | * This context setter is called in {{luya\cms\frontend\base\Controller::renderItem()}} method and is used when calling {{\luya\web\UrlManager::createUrl}} method. |
||
| 42 | */ |
||
| 43 | public $contextNavItemId; |
||
| 44 | |||
| 45 | private $_menu; |
||
| 46 | |||
| 47 | private $_composition; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Ensure whether a route starts with a language short key or not. |
||
| 51 | * |
||
| 52 | * @param string $route The route to check `en/module/controller/action` or without `module/controller/action` |
||
| 53 | * @param string $language The language to check whether it exists or not `en`. |
||
| 54 | * @return boolean |
||
| 55 | */ |
||
| 56 | public function routeHasLanguageCompositionPrefix($route, $language) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Extend functionality of parent::parseRequest() by verify and resolve the composition informations. |
||
| 68 | * |
||
| 69 | * @inheritDoc |
||
| 70 | * |
||
| 71 | * @see \yii\web\UrlManager::parseRequest() |
||
| 72 | * @param \luya\web\Request $request The request component. |
||
| 73 | */ |
||
| 74 | public function parseRequest($request) |
||
| 75 | { |
||
| 76 | // extra data from request to composition, which changes the pathInfo of the Request-Object. |
||
| 77 | $resolver = $this->getComposition()->getResolvedPathInfo($request); |
||
| 78 | |||
| 79 | try { |
||
| 80 | $request->setPathInfo($resolver->resolvedPath); |
||
| 81 | } catch (NotFoundHttpException $error) { |
||
| 82 | // the resolver has thrown an 404 excpetion, stop parsing request and return false (which is: page not found) |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | |||
| 86 | $parsedRequest = parent::parseRequest($request); |
||
| 87 | |||
| 88 | // if [[enablePrettyUrl]] is `false`. `false` is returned if the current request cannot be successfully parsed. |
||
| 89 | if ($parsedRequest === false) { |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | // ensure if the parsed route first match equals the composition pattern. |
||
| 94 | // This can be the case when composition is hidden, but not default language is loaded and a |
||
| 95 | // url composition route is loaded! |
||
| 96 | // @see https://github.com/luyadev/luya/issues/1146 |
||
| 97 | $res = $this->routeHasLanguageCompositionPrefix($parsedRequest[0], $resolver->getResolvedKeyValue(Composition::VAR_LANG_SHORT_CODE)); |
||
| 98 | |||
| 99 | // set the application language based from the parsed composition request: |
||
| 100 | Yii::$app->setLocale($this->composition->langShortCode); |
||
| 101 | Yii::$app->language = $this->composition->langShortCode; |
||
| 102 | |||
| 103 | // if enableStrictParsing is enabled and the route is not found, $parsedRequest will return `false`. |
||
| 104 | if ($res === false && ($this->composition->hidden || $parsedRequest === false)) { |
||
| 105 | return $parsedRequest; |
||
| 106 | } |
||
| 107 | |||
| 108 | $composition = $this->composition->createRoute(); |
||
| 109 | $length = strlen($composition); |
||
| 110 | $route = $parsedRequest[0]; |
||
| 111 | |||
| 112 | if (substr($route, 0, $length+1) == $composition.'/') { |
||
| 113 | $parsedRequest[0] = substr($parsedRequest[0], $length); |
||
| 114 | } |
||
| 115 | |||
| 116 | // remove start trailing slashes from route. |
||
| 117 | $parsedRequest[0] = ltrim($parsedRequest[0], '/'); |
||
| 118 | |||
| 119 | return $parsedRequest; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Extend functionality of parent::addRules by the ability to add composition routes. |
||
| 124 | * |
||
| 125 | * @see \yii\web\UrlManager::addRules() |
||
| 126 | * @param array $rules An array wil rules |
||
| 127 | * @param boolean $append Append to the end of the rules or not. |
||
| 128 | */ |
||
| 129 | public function addRules($rules, $append = true) |
||
| 130 | { |
||
| 131 | foreach ($rules as $key => $rule) { |
||
| 132 | if (is_array($rule) && isset($rule['composition'])) { |
||
| 133 | foreach ($rule['composition'] as $composition => $pattern) { |
||
| 134 | $rules[] = [ |
||
| 135 | 'pattern' => $pattern, |
||
| 136 | 'route' => $composition.'/'.$rule['route'], |
||
| 137 | ]; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return parent::addRules($rules, $append); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the menu component if its registered in the current applications. |
||
| 147 | * |
||
| 148 | * The menu component is only registered when the cms module is registered. |
||
| 149 | * |
||
| 150 | * @return boolean|\luya\cms\Menu The menu component object or false if not available. |
||
| 151 | */ |
||
| 152 | public function getMenu() |
||
| 153 | { |
||
| 154 | if ($this->_menu === null) { |
||
| 155 | $menu = Yii::$app->get('menu', false); |
||
| 156 | if ($menu) { |
||
| 157 | $this->_menu = $menu; |
||
| 158 | } else { |
||
| 159 | $this->_menu = false; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | return $this->_menu; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Setter method for the composition component. |
||
| 168 | * |
||
| 169 | * @param \luya\web\Composition $composition |
||
| 170 | */ |
||
| 171 | public function setComposition(Composition $composition) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the composition component |
||
| 178 | * |
||
| 179 | * @return \luya\web\Composition Get the composition component to resolve multi lingual handling. |
||
| 180 | */ |
||
| 181 | public function getComposition() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Prepand the base url to an existing route |
||
| 192 | * |
||
| 193 | * @param string $route The route where the base url should be prepend to. |
||
| 194 | * @return string The route with prepanded baseUrl. |
||
| 195 | */ |
||
| 196 | public function prependBaseUrl($route) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Remove the base url from a route |
||
| 203 | * |
||
| 204 | * @param string $route The route where the baseUrl should be removed from. |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | public function removeBaseUrl($route) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Extend createUrl method by verify its context implementation to add cms urls prepand to the requested createurl params. |
||
| 214 | * |
||
| 215 | * From the original create url function of Yii: |
||
| 216 | * |
||
| 217 | * You may specify the route as a string, e.g., `site/index`. You may also use an array |
||
| 218 | * if you want to specify additional query parameters for the URL being created. The |
||
| 219 | * array format must be: |
||
| 220 | * |
||
| 221 | * ```php |
||
| 222 | * // generates: /index.php?r=site%2Findex¶m1=value1¶m2=value2 |
||
| 223 | * ['site/index', 'param1' => 'value1', 'param2' => 'value2'] |
||
| 224 | * ``` |
||
| 225 | * |
||
| 226 | * If you want to create a URL with an anchor, you can use the array format with a `#` parameter. |
||
| 227 | * For example, |
||
| 228 | * |
||
| 229 | * ```php |
||
| 230 | * // generates: /index.php?r=site%2Findex¶m1=value1#name |
||
| 231 | * ['site/index', 'param1' => 'value1', '#' => 'name'] |
||
| 232 | * ``` |
||
| 233 | * |
||
| 234 | * The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL. |
||
| 235 | * |
||
| 236 | * Note that unlike {{luya\helpers\Url::toRoute()}}, this method always treats the given route |
||
| 237 | * as an absolute route. |
||
| 238 | * |
||
| 239 | * @see \yii\web\UrlManager::createUrl() |
||
| 240 | * @param string|array $params use a string to represent a route (e.g. `site/index`), |
||
| 241 | * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
| 242 | * @return string the created URL. |
||
| 243 | */ |
||
| 244 | public function createUrl($params) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Create an url for a menu item. |
||
| 257 | * |
||
| 258 | * @param string|array $params Use a string to represent a route (e.g. `site/index`), or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
||
| 259 | * @param integer $navItemId The nav item Id |
||
| 260 | * @param null|\luya\web\Composition $composition Optional other composition config instead of using the default composition |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function createMenuItemUrl($params, $navItemId, $composition = null) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Yii2 createUrl base implementation extends the prepand of the comosition |
||
| 277 | * |
||
| 278 | * @param string|array $params An array with params or not (e.g. `['module/controller/action', 'param1' => 'value1']`) |
||
| 279 | * @param null|\luya\web\Composition $composition Composition instance to change the route behavior |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function internalCreateUrl($params, $composition = null) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Create absolute url from the given route params. |
||
| 310 | * |
||
| 311 | * @param string|array $params The see createUrl |
||
| 312 | * @param boolean $scheme Whether to use absolute scheme path or not. |
||
| 313 | * @return string The created url |
||
| 314 | */ |
||
| 315 | public function internalCreateAbsoluteUrl($params, $scheme = null) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * See if the module of a provided route exists in the luya application list. |
||
| 330 | * |
||
| 331 | * The module to test must be an instance of `luya\base\Module`. |
||
| 332 | * |
||
| 333 | * @param string $route |
||
| 334 | * @return boolean|string |
||
| 335 | */ |
||
| 336 | private function findModuleInRoute($route) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Replace the url with the current module context. |
||
| 351 | * |
||
| 352 | * @param string $url The url to replace |
||
| 353 | * @param integer $navItemId The navigation item where the context url to be found. |
||
| 354 | * @param \luya\web\Composition $composition Composition component object to resolve language context. |
||
| 355 | * @throws \yii\web\BadRequestHttpException |
||
| 356 | * @return string The replaced string. |
||
| 357 | */ |
||
| 358 | private function urlReplaceModule($url, $navItemId, Composition $composition) |
||
| 392 | } |
||
| 393 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.