Passed
Push — 17933-use-dummy-cache-by-defau... ( b6d1bc...2371fb )
by Alexander
09:12
created

UrlManager   F

Complexity

Total Complexity 87

Size/Duplication

Total Lines 604
Duplicated Lines 0 %

Test Coverage

Coverage 95.6%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 186
c 2
b 0
f 0
dl 0
loc 604
ccs 174
cts 182
cp 0.956
rs 2
wmc 87

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setBaseUrl() 0 3 2
A canBeCached() 0 8 4
A setHostInfo() 0 3 2
A createAbsoluteUrl() 0 14 3
A addRules() 0 10 3
B buildRules() 0 34 8
C parseRequest() 0 59 14
A getHostInfo() 0 12 3
A getBuiltRulesFromCache() 0 7 2
A setScriptUrl() 0 3 1
A setBuiltRulesCache() 0 7 2
A getBaseUrl() 0 12 3
F createUrl() 0 74 24
A setRuleToCache() 0 3 1
A getScriptUrl() 0 12 3
A getUrlFromCache() 0 14 4
B init() 0 25 8

How to fix   Complexity   

Complex Class

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.

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
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\Component;
12
use yii\base\InvalidConfigException;
13
use yii\caching\CacheInterface;
14
use yii\di\Instance;
15
use yii\helpers\Url;
16
17
/**
18
 * UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.
19
 *
20
 * UrlManager is configured as an application component in [[\yii\base\Application]] by default.
21
 * You can access that instance via `Yii::$app->urlManager`.
22
 *
23
 * You can modify its configuration by adding an array to your application config under `components`
24
 * as it is shown in the following example:
25
 *
26
 * ```php
27
 * 'urlManager' => [
28
 *     'enablePrettyUrl' => true,
29
 *     'rules' => [
30
 *         // your rules go here
31
 *     ],
32
 *     // ...
33
 * ]
34
 * ```
35
 *
36
 * Rules are classes implementing the [[UrlRuleInterface]], by default that is [[UrlRule]].
37
 * For nesting rules, there is also a [[GroupUrlRule]] class.
38
 *
39
 * For more details and usage information on UrlManager, see the [guide article on routing](guide:runtime-routing).
40
 *
41
 * @property string $baseUrl The base URL that is used by [[createUrl()]] to prepend to created URLs.
42
 * @property string $hostInfo The host info (e.g. `http://www.example.com`) that is used by
43
 * [[createAbsoluteUrl()]] to prepend to created URLs.
44
 * @property string $scriptUrl The entry script URL that is used by [[createUrl()]] to prepend to created
45
 * URLs.
46
 *
47
 * @author Qiang Xue <[email protected]>
48
 * @since 2.0
49
 */
50
class UrlManager extends Component
51
{
52
    /**
53
     * @var bool whether to enable pretty URLs. Instead of putting all parameters in the query
54
     * string part of a URL, pretty URLs allow using path info to represent some of the parameters
55
     * and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of
56
     * "/index.php?r=news%2Fview&id=100".
57
     */
58
    public $enablePrettyUrl = false;
59
    /**
60
     * @var bool whether to enable strict parsing. If strict parsing is enabled, the incoming
61
     * requested URL must match at least one of the [[rules]] in order to be treated as a valid request.
62
     * Otherwise, the path info part of the request will be treated as the requested route.
63
     * This property is used only when [[enablePrettyUrl]] is `true`.
64
     */
65
    public $enableStrictParsing = false;
66
    /**
67
     * @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is `true`.
68
     * This property is used only if [[enablePrettyUrl]] is `true`. Each element in the array
69
     * is the configuration array for creating a single URL rule. The configuration will
70
     * be merged with [[ruleConfig]] first before it is used for creating the rule object.
71
     *
72
     * A special shortcut format can be used if a rule only specifies [[UrlRule::pattern|pattern]]
73
     * and [[UrlRule::route|route]]: `'pattern' => 'route'`. That is, instead of using a configuration
74
     * array, one can use the key to represent the pattern and the value the corresponding route.
75
     * For example, `'post/<id:\d+>' => 'post/view'`.
76
     *
77
     * For RESTful routing the mentioned shortcut format also allows you to specify the
78
     * [[UrlRule::verb|HTTP verb]] that the rule should apply for.
79
     * You can do that  by prepending it to the pattern, separated by space.
80
     * For example, `'PUT post/<id:\d+>' => 'post/update'`.
81
     * You may specify multiple verbs by separating them with comma
82
     * like this: `'POST,PUT post/index' => 'post/create'`.
83
     * The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE.
84
     * Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way
85
     * so you normally would not specify a verb for normal GET request.
86
     *
87
     * Here is an example configuration for RESTful CRUD controller:
88
     *
89
     * ```php
90
     * [
91
     *     'dashboard' => 'site/index',
92
     *
93
     *     'POST <controller:[\w-]+>' => '<controller>/create',
94
     *     '<controller:[\w-]+>s' => '<controller>/index',
95
     *
96
     *     'PUT <controller:[\w-]+>/<id:\d+>'    => '<controller>/update',
97
     *     'DELETE <controller:[\w-]+>/<id:\d+>' => '<controller>/delete',
98
     *     '<controller:[\w-]+>/<id:\d+>'        => '<controller>/view',
99
     * ];
100
     * ```
101
     *
102
     * Note that if you modify this property after the UrlManager object is created, make sure
103
     * you populate the array with rule objects instead of rule configurations.
104
     */
105
    public $rules = [];
106
    /**
107
     * @var string the URL suffix used when [[enablePrettyUrl]] is `true`.
108
     * For example, ".html" can be used so that the URL looks like pointing to a static HTML page.
109
     * This property is used only if [[enablePrettyUrl]] is `true`.
110
     */
111
    public $suffix;
112
    /**
113
     * @var bool whether to show entry script name in the constructed URL. Defaults to `true`.
114
     * This property is used only if [[enablePrettyUrl]] is `true`.
115
     */
116
    public $showScriptName = true;
117
    /**
118
     * @var string the GET parameter name for route. This property is used only if [[enablePrettyUrl]] is `false`.
119
     */
120
    public $routeParam = 'r';
121
    /**
122
     * @var CacheInterface|array|string the cache object or the application component ID of the cache object.
123
     * This can also be an array that is used to create a [[CacheInterface]] instance in case you do not want to use
124
     * an application component.
125
     * Compiled URL rules will be cached through this cache object, if it is available.
126
     *
127
     * After the UrlManager object is created, if you want to change this property,
128
     * you should only assign it with a cache object.
129
     * Set this property to `false` or `null` if you do not want to cache the URL rules.
130
     *
131
     * Cache entries are stored for the time set by [[\yii\caching\Cache::$defaultDuration|$defaultDuration]] in
132
     * the cache configuration, which is unlimited by default. You may want to tune this value if your [[rules]]
133
     * change frequently.
134
     */
135
    public $cache = 'cache';
136
    /**
137
     * @var array the default configuration of URL rules. Individual rule configurations
138
     * specified via [[rules]] will take precedence when the same property of the rule is configured.
139
     */
140
    public $ruleConfig = ['class' => 'yii\web\UrlRule'];
141
    /**
142
     * @var UrlNormalizer|array|string|false the configuration for [[UrlNormalizer]] used by this UrlManager.
143
     * The default value is `false`, which means normalization will be skipped.
144
     * If you wish to enable URL normalization, you should configure this property manually.
145
     * For example:
146
     *
147
     * ```php
148
     * [
149
     *     'class' => 'yii\web\UrlNormalizer',
150
     *     'collapseSlashes' => true,
151
     *     'normalizeTrailingSlash' => true,
152
     * ]
153
     * ```
154
     *
155
     * @since 2.0.10
156
     */
157
    public $normalizer = false;
158
159
    /**
160
     * @var string the cache key for cached rules
161
     * @since 2.0.8
162
     */
163
    protected $cacheKey = __CLASS__;
164
165
    private $_baseUrl;
166
    private $_scriptUrl;
167
    private $_hostInfo;
168
    private $_ruleCache;
169
170
171
    /**
172
     * Initializes UrlManager.
173
     */
174 182
    public function init()
175
    {
176 182
        parent::init();
177
178 182
        if ($this->normalizer !== false) {
179 3
            $this->normalizer = Yii::createObject($this->normalizer);
180 3
            if (!$this->normalizer instanceof UrlNormalizer) {
181
                throw new InvalidConfigException('`' . get_class($this) . '::normalizer` should be an instance of `' . UrlNormalizer::className() . '` or its DI compatible configuration.');
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

181
                throw new InvalidConfigException('`' . get_class($this) . '::normalizer` should be an instance of `' . /** @scrutinizer ignore-deprecated */ UrlNormalizer::className() . '` or its DI compatible configuration.');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
182
            }
183
        }
184
185 182
        if (!$this->enablePrettyUrl) {
186 84
            return;
187
        }
188 98
        if ($this->cache !== false && $this->cache !== null) {
189
            try {
190 3
                $this->cache = Instance::ensure($this->cache, 'yii\caching\CacheInterface');
191
            } catch (InvalidConfigException $e) {
192
                Yii::warning('Unable to use cache for URL manager: ' . $e->getMessage());
193
            }
194
        }
195 98
        if (empty($this->rules)) {
196 21
            return;
197
        }
198 78
        $this->rules = $this->buildRules($this->rules);
199 78
    }
200
201
    /**
202
     * Adds additional URL rules.
203
     *
204
     * This method will call [[buildRules()]] to parse the given rule declarations and then append or insert
205
     * them to the existing [[rules]].
206
     *
207
     * Note that if [[enablePrettyUrl]] is `false`, this method will do nothing.
208
     *
209
     * @param array $rules the new rules to be added. Each array element represents a single rule declaration.
210
     * Please refer to [[rules]] for the acceptable rule format.
211
     * @param bool $append whether to add the new rules by appending them to the end of the existing rules.
212
     */
213 3
    public function addRules($rules, $append = true)
214
    {
215 3
        if (!$this->enablePrettyUrl) {
216
            return;
217
        }
218 3
        $rules = $this->buildRules($rules);
219 3
        if ($append) {
220 2
            $this->rules = array_merge($this->rules, $rules);
221
        } else {
222 1
            $this->rules = array_merge($rules, $this->rules);
223
        }
224 3
    }
225
226
    /**
227
     * Builds URL rule objects from the given rule declarations.
228
     *
229
     * @param array $ruleDeclarations the rule declarations. Each array element represents a single rule declaration.
230
     * Please refer to [[rules]] for the acceptable rule formats.
231
     * @return UrlRuleInterface[] the rule objects built from the given rule declarations
232
     * @throws InvalidConfigException if a rule declaration is invalid
233
     */
234 78
    protected function buildRules($ruleDeclarations)
235
    {
236 78
        $builtRules = $this->getBuiltRulesFromCache($ruleDeclarations);
237 78
        if ($builtRules !== false) {
238 1
            return $builtRules;
239
        }
240
241 77
        $builtRules = [];
242 77
        $verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
243 77
        foreach ($ruleDeclarations as $key => $rule) {
244 77
            if (is_string($rule)) {
245 70
                $rule = ['route' => $rule];
246 70
                if (preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches)) {
247 1
                    $rule['verb'] = explode(',', $matches[1]);
248
                    // rules that are not applicable for GET requests should not be used to create URLs
249 1
                    if (!in_array('GET', $rule['verb'], true)) {
250 1
                        $rule['mode'] = UrlRule::PARSING_ONLY;
251
                    }
252 1
                    $key = $matches[4];
253
                }
254 70
                $rule['pattern'] = $key;
255
            }
256 77
            if (is_array($rule)) {
257 75
                $rule = Yii::createObject(array_merge($this->ruleConfig, $rule));
258
            }
259 77
            if (!$rule instanceof UrlRuleInterface) {
260
                throw new InvalidConfigException('URL rule class must implement UrlRuleInterface.');
261
            }
262 77
            $builtRules[] = $rule;
263
        }
264
265 77
        $this->setBuiltRulesCache($ruleDeclarations, $builtRules);
266
267 77
        return $builtRules;
268
    }
269
270
    /**
271
     * Stores $builtRules to cache, using $rulesDeclaration as a part of cache key.
272
     *
273
     * @param array $ruleDeclarations the rule declarations. Each array element represents a single rule declaration.
274
     * Please refer to [[rules]] for the acceptable rule formats.
275
     * @param UrlRuleInterface[] $builtRules the rule objects built from the given rule declarations.
276
     * @return bool whether the value is successfully stored into cache
277
     * @since 2.0.14
278
     */
279 77
    protected function setBuiltRulesCache($ruleDeclarations, $builtRules)
280
    {
281 77
        if (!$this->cache instanceof CacheInterface) {
282 75
            return false;
283
        }
284
285 2
        return $this->cache->set([$this->cacheKey, $this->ruleConfig, $ruleDeclarations], $builtRules);
286
    }
287
288
    /**
289
     * Provides the built URL rules that are associated with the $ruleDeclarations from cache.
290
     *
291
     * @param array $ruleDeclarations the rule declarations. Each array element represents a single rule declaration.
292
     * Please refer to [[rules]] for the acceptable rule formats.
293
     * @return UrlRuleInterface[]|false the rule objects built from the given rule declarations or boolean `false` when
294
     * there are no cache items for this definition exists.
295
     * @since 2.0.14
296
     */
297 78
    protected function getBuiltRulesFromCache($ruleDeclarations)
298
    {
299 78
        if (!$this->cache instanceof CacheInterface) {
300 75
            return false;
301
        }
302
303 3
        return $this->cache->get([$this->cacheKey, $this->ruleConfig, $ruleDeclarations]);
304
    }
305
306
    /**
307
     * Parses the user request.
308
     * @param Request $request the request component
309
     * @return array|bool the route and the associated parameters. The latter is always empty
310
     * if [[enablePrettyUrl]] is `false`. `false` is returned if the current request cannot be successfully parsed.
311
     */
312 17
    public function parseRequest($request)
313
    {
314 17
        if ($this->enablePrettyUrl) {
315
            /* @var $rule UrlRule */
316 14
            foreach ($this->rules as $rule) {
317 10
                $result = $rule->parseRequest($this, $request);
318 10
                if (YII_DEBUG) {
319 10
                    Yii::debug([
320 10
                        'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
321
                        'match' => $result !== false,
322
                        'parent' => null,
323 10
                    ], __METHOD__);
324
                }
325 10
                if ($result !== false) {
326 10
                    return $result;
327
                }
328
            }
329
330 11
            if ($this->enableStrictParsing) {
331 4
                return false;
332
            }
333
334 7
            Yii::debug('No matching URL rules. Using default URL parsing logic.', __METHOD__);
335
336 7
            $suffix = (string) $this->suffix;
337 7
            $pathInfo = $request->getPathInfo();
338 7
            $normalized = false;
339 7
            if ($this->normalizer !== false) {
340 1
                $pathInfo = $this->normalizer->normalizePathInfo($pathInfo, $suffix, $normalized);
341
            }
342 7
            if ($suffix !== '' && $pathInfo !== '') {
343 4
                $n = strlen($this->suffix);
344 4
                if (substr_compare($pathInfo, $this->suffix, -$n, $n) === 0) {
345 4
                    $pathInfo = substr($pathInfo, 0, -$n);
346 4
                    if ($pathInfo === '') {
347
                        // suffix alone is not allowed
348 4
                        return false;
349
                    }
350
                } else {
351
                    // suffix doesn't match
352 4
                    return false;
353
                }
354
            }
355
356 7
            if ($normalized) {
357
                // pathInfo was changed by normalizer - we need also normalize route
358 1
                return $this->normalizer->normalizeRoute([$pathInfo, []]);
359
            }
360
361 6
            return [$pathInfo, []];
362
        }
363
364 3
        Yii::debug('Pretty URL not enabled. Using default URL parsing logic.', __METHOD__);
365 3
        $route = $request->getQueryParam($this->routeParam, '');
366 3
        if (is_array($route)) {
367 3
            $route = '';
368
        }
369
370 3
        return [(string) $route, []];
371
    }
372
373
    /**
374
     * Creates a URL using the given route and query parameters.
375
     *
376
     * You may specify the route as a string, e.g., `site/index`. You may also use an array
377
     * if you want to specify additional query parameters for the URL being created. The
378
     * array format must be:
379
     *
380
     * ```php
381
     * // generates: /index.php?r=site%2Findex&param1=value1&param2=value2
382
     * ['site/index', 'param1' => 'value1', 'param2' => 'value2']
383
     * ```
384
     *
385
     * If you want to create a URL with an anchor, you can use the array format with a `#` parameter.
386
     * For example,
387
     *
388
     * ```php
389
     * // generates: /index.php?r=site%2Findex&param1=value1#name
390
     * ['site/index', 'param1' => 'value1', '#' => 'name']
391
     * ```
392
     *
393
     * The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL.
394
     *
395
     * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route
396
     * as an absolute route.
397
     *
398
     * @param string|array $params use a string to represent a route (e.g. `site/index`),
399
     * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
400
     * @return string the created URL
401
     */
402 136
    public function createUrl($params)
403
    {
404 136
        $params = (array) $params;
405 136
        $anchor = isset($params['#']) ? '#' . $params['#'] : '';
406 136
        unset($params['#'], $params[$this->routeParam]);
407
408 136
        $route = trim($params[0], '/');
409 136
        unset($params[0]);
410
411 136
        $baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();
412
413 136
        if ($this->enablePrettyUrl) {
414 80
            $cacheKey = $route . '?';
415 80
            foreach ($params as $key => $value) {
416 80
                if ($value !== null) {
417 80
                    $cacheKey .= $key . '&';
418
                }
419
            }
420
421 80
            $url = $this->getUrlFromCache($cacheKey, $route, $params);
422 80
            if ($url === false) {
423
                /* @var $rule UrlRule */
424 80
                foreach ($this->rules as $rule) {
425 64
                    if (in_array($rule, $this->_ruleCache[$cacheKey], true)) {
426
                        // avoid redundant calls of `UrlRule::createUrl()` for rules checked in `getUrlFromCache()`
427
                        // @see https://github.com/yiisoft/yii2/issues/14094
428 21
                        continue;
429
                    }
430 64
                    $url = $rule->createUrl($this, $route, $params);
431 64
                    if ($this->canBeCached($rule)) {
432 63
                        $this->setRuleToCache($cacheKey, $rule);
433
                    }
434 64
                    if ($url !== false) {
435 64
                        break;
436
                    }
437
                }
438
            }
439
440 80
            if ($url !== false) {
441 63
                if (strpos($url, '://') !== false) {
442 8
                    if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) {
443 3
                        return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor;
444
                    }
445
446 5
                    return $url . $baseUrl . $anchor;
447 55
                } elseif (strncmp($url, '//', 2) === 0) {
448 4
                    if ($baseUrl !== '' && ($pos = strpos($url, '/', 2)) !== false) {
449 3
                        return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor;
450
                    }
451
452 4
                    return $url . $baseUrl . $anchor;
453
                }
454
455 51
                $url = ltrim($url, '/');
456 51
                return "$baseUrl/{$url}{$anchor}";
457
            }
458
459 65
            if ($this->suffix !== null) {
460 24
                $route .= $this->suffix;
461
            }
462 65
            if (!empty($params) && ($query = http_build_query($params)) !== '') {
463 49
                $route .= '?' . $query;
464
            }
465
466 65
            $route = ltrim($route, '/');
467 65
            return "$baseUrl/{$route}{$anchor}";
468
        }
469
470 56
        $url = "$baseUrl?{$this->routeParam}=" . urlencode($route);
471 56
        if (!empty($params) && ($query = http_build_query($params)) !== '') {
472 40
            $url .= '&' . $query;
473
        }
474
475 56
        return $url . $anchor;
476
    }
477
478
    /**
479
     * Returns the value indicating whether result of [[createUrl()]] of rule should be cached in internal cache.
480
     *
481
     * @param UrlRuleInterface $rule
482
     * @return bool `true` if result should be cached, `false` if not.
483
     * @since 2.0.12
484
     * @see getUrlFromCache()
485
     * @see setRuleToCache()
486
     * @see UrlRule::getCreateUrlStatus()
487
     */
488 64
    protected function canBeCached(UrlRuleInterface $rule)
489
    {
490
        return
491
            // if rule does not provide info about create status, we cache it every time to prevent bugs like #13350
492
            // @see https://github.com/yiisoft/yii2/pull/13350#discussion_r114873476
493 64
            !method_exists($rule, 'getCreateUrlStatus') || ($status = $rule->getCreateUrlStatus()) === null
494 64
            || $status === UrlRule::CREATE_STATUS_SUCCESS
495 64
            || $status & UrlRule::CREATE_STATUS_PARAMS_MISMATCH;
496
    }
497
498
    /**
499
     * Get URL from internal cache if exists.
500
     * @param string $cacheKey generated cache key to store data.
501
     * @param string $route the route (e.g. `site/index`).
502
     * @param array $params rule params.
503
     * @return bool|string the created URL
504
     * @see createUrl()
505
     * @since 2.0.8
506
     */
507 80
    protected function getUrlFromCache($cacheKey, $route, $params)
508
    {
509 80
        if (!empty($this->_ruleCache[$cacheKey])) {
510 63
            foreach ($this->_ruleCache[$cacheKey] as $rule) {
511
                /* @var $rule UrlRule */
512 63
                if (($url = $rule->createUrl($this, $route, $params)) !== false) {
513 63
                    return $url;
514
                }
515
            }
516
        } else {
517 80
            $this->_ruleCache[$cacheKey] = [];
518
        }
519
520 80
        return false;
521
    }
522
523
    /**
524
     * Store rule (e.g. [[UrlRule]]) to internal cache.
525
     * @param $cacheKey
526
     * @param UrlRuleInterface $rule
527
     * @since 2.0.8
528
     */
529 63
    protected function setRuleToCache($cacheKey, UrlRuleInterface $rule)
530
    {
531 63
        $this->_ruleCache[$cacheKey][] = $rule;
532 63
    }
533
534
    /**
535
     * Creates an absolute URL using the given route and query parameters.
536
     *
537
     * This method prepends the URL created by [[createUrl()]] with the [[hostInfo]].
538
     *
539
     * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route
540
     * as an absolute route.
541
     *
542
     * @param string|array $params use a string to represent a route (e.g. `site/index`),
543
     * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
544
     * @param string|null $scheme the scheme to use for the URL (either `http`, `https` or empty string
545
     * for protocol-relative URL).
546
     * If not specified the scheme of the current request will be used.
547
     * @return string the created URL
548
     * @see createUrl()
549
     */
550 58
    public function createAbsoluteUrl($params, $scheme = null)
551
    {
552 58
        $params = (array) $params;
553 58
        $url = $this->createUrl($params);
554 58
        if (strpos($url, '://') === false) {
555 54
            $hostInfo = $this->getHostInfo();
556 54
            if (strncmp($url, '//', 2) === 0) {
557 4
                $url = substr($hostInfo, 0, strpos($hostInfo, '://')) . ':' . $url;
558
            } else {
559 54
                $url = $hostInfo . $url;
560
            }
561
        }
562
563 58
        return Url::ensureScheme($url, $scheme);
564
    }
565
566
    /**
567
     * Returns the base URL that is used by [[createUrl()]] to prepend to created URLs.
568
     * It defaults to [[Request::baseUrl]].
569
     * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`.
570
     * @return string the base URL that is used by [[createUrl()]] to prepend to created URLs.
571
     * @throws InvalidConfigException if running in console application and [[baseUrl]] is not configured.
572
     */
573 42
    public function getBaseUrl()
574
    {
575 42
        if ($this->_baseUrl === null) {
576 1
            $request = Yii::$app->getRequest();
577 1
            if ($request instanceof Request) {
578 1
                $this->_baseUrl = $request->getBaseUrl();
579
            } else {
580
                throw new InvalidConfigException('Please configure UrlManager::baseUrl correctly as you are running a console application.');
581
            }
582
        }
583
584 42
        return $this->_baseUrl;
585
    }
586
587
    /**
588
     * Sets the base URL that is used by [[createUrl()]] to prepend to created URLs.
589
     * This is mainly used when [[enablePrettyUrl]] is `true` and [[showScriptName]] is `false`.
590
     * @param string $value the base URL that is used by [[createUrl()]] to prepend to created URLs.
591
     */
592 113
    public function setBaseUrl($value)
593
    {
594 113
        $this->_baseUrl = $value === null ? null : rtrim(Yii::getAlias($value), '/');
0 ignored issues
show
introduced by
The condition $value === null is always false.
Loading history...
595 113
    }
596
597
    /**
598
     * Returns the entry script URL that is used by [[createUrl()]] to prepend to created URLs.
599
     * It defaults to [[Request::scriptUrl]].
600
     * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`.
601
     * @return string the entry script URL that is used by [[createUrl()]] to prepend to created URLs.
602
     * @throws InvalidConfigException if running in console application and [[scriptUrl]] is not configured.
603
     */
604 96
    public function getScriptUrl()
605
    {
606 96
        if ($this->_scriptUrl === null) {
607 18
            $request = Yii::$app->getRequest();
608 18
            if ($request instanceof Request) {
609 18
                $this->_scriptUrl = $request->getScriptUrl();
610
            } else {
611
                throw new InvalidConfigException('Please configure UrlManager::scriptUrl correctly as you are running a console application.');
612
            }
613
        }
614
615 96
        return $this->_scriptUrl;
616
    }
617
618
    /**
619
     * Sets the entry script URL that is used by [[createUrl()]] to prepend to created URLs.
620
     * This is mainly used when [[enablePrettyUrl]] is `false` or [[showScriptName]] is `true`.
621
     * @param string $value the entry script URL that is used by [[createUrl()]] to prepend to created URLs.
622
     */
623 125
    public function setScriptUrl($value)
624
    {
625 125
        $this->_scriptUrl = $value;
626 125
    }
627
628
    /**
629
     * Returns the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs.
630
     * @return string the host info (e.g. `http://www.example.com`) that is used by [[createAbsoluteUrl()]] to prepend to created URLs.
631
     * @throws InvalidConfigException if running in console application and [[hostInfo]] is not configured.
632
     */
633 56
    public function getHostInfo()
634
    {
635 56
        if ($this->_hostInfo === null) {
636 7
            $request = Yii::$app->getRequest();
637 7
            if ($request instanceof \yii\web\Request) {
638 7
                $this->_hostInfo = $request->getHostInfo();
639
            } else {
640
                throw new InvalidConfigException('Please configure UrlManager::hostInfo correctly as you are running a console application.');
641
            }
642
        }
643
644 56
        return $this->_hostInfo;
645
    }
646
647
    /**
648
     * Sets the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs.
649
     * @param string $value the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs.
650
     */
651 110
    public function setHostInfo($value)
652
    {
653 110
        $this->_hostInfo = $value === null ? null : rtrim($value, '/');
0 ignored issues
show
introduced by
The condition $value === null is always false.
Loading history...
654 110
    }
655
}
656