Passed
Push — fix-php-74 ( f6eb65...a1ad6b )
by Alexander
38:08 queued 12:19
created

UrlManager::buildRules()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.006

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 18
nop 1
dl 0
loc 34
ccs 21
cts 22
cp 0.9545
crap 8.006
rs 8.4444
c 0
b 0
f 0
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\helpers\Url;
15
16
/**
17
 * UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.
18
 *
19
 * UrlManager is configured as an application component in [[\yii\base\Application]] by default.
20
 * You can access that instance via `Yii::$app->urlManager`.
21
 *
22
 * You can modify its configuration by adding an array to your application config under `components`
23
 * as it is shown in the following example:
24
 *
25
 * ```php
26
 * 'urlManager' => [
27
 *     'enablePrettyUrl' => true,
28
 *     'rules' => [
29
 *         // your rules go here
30
 *     ],
31
 *     // ...
32
 * ]
33
 * ```
34
 *
35
 * Rules are classes implementing the [[UrlRuleInterface]], by default that is [[UrlRule]].
36
 * For nesting rules, there is also a [[GroupUrlRule]] class.
37
 *
38
 * For more details and usage information on UrlManager, see the [guide article on routing](guide:runtime-routing).
39
 *
40
 * @property string $baseUrl The base URL that is used by [[createUrl()]] to prepend to created URLs.
41
 * @property string $hostInfo The host info (e.g. `http://www.example.com`) that is used by
42
 * [[createAbsoluteUrl()]] to prepend to created URLs.
43
 * @property string $scriptUrl The entry script URL that is used by [[createUrl()]] to prepend to created
44
 * URLs.
45
 *
46
 * @author Qiang Xue <[email protected]>
47
 * @since 2.0
48
 */
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-]+>' => '<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 CacheInterface|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
     * Cache entries are stored for the time set by [[\yii\caching\Cache::$defaultDuration|$defaultDuration]] in
129
     * the cache configuration, which is unlimited by default. You may want to tune this value if your [[rules]]
130
     * change frequently.
131
     */
132
    public $cache = 'cache';
133
    /**
134
     * @var array the default configuration of URL rules. Individual rule configurations
135
     * specified via [[rules]] will take precedence when the same property of the rule is configured.
136
     */
137
    public $ruleConfig = ['class' => 'yii\web\UrlRule'];
138
    /**
139
     * @var UrlNormalizer|array|string|false the configuration for [[UrlNormalizer]] used by this UrlManager.
140
     * The default value is `false`, which means normalization will be skipped.
141
     * If you wish to enable URL normalization, you should configure this property manually.
142
     * For example:
143
     *
144
     * ```php
145
     * [
146
     *     'class' => 'yii\web\UrlNormalizer',
147
     *     'collapseSlashes' => true,
148
     *     'normalizeTrailingSlash' => true,
149
     * ]
150
     * ```
151
     *
152
     * @since 2.0.10
153
     */
154
    public $normalizer = false;
155
156
    /**
157
     * @var string the cache key for cached rules
158
     * @since 2.0.8
159
     */
160
    protected $cacheKey = __CLASS__;
161
162
    private $_baseUrl;
163
    private $_scriptUrl;
164
    private $_hostInfo;
165
    private $_ruleCache;
166
167
168
    /**
169
     * Initializes UrlManager.
170
     */
171 182
    public function init()
172
    {
173 182
        parent::init();
174
175 182
        if ($this->normalizer !== false) {
176 3
            $this->normalizer = Yii::createObject($this->normalizer);
177 3
            if (!$this->normalizer instanceof UrlNormalizer) {
178
                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

178
                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...
179
            }
180
        }
181
182 182
        if (!$this->enablePrettyUrl) {
183 84
            return;
184
        }
185 98
        if (is_string($this->cache)) {
186 1
            $this->cache = Yii::$app->get($this->cache, false);
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->get($this->cache, false) can also be of type mixed. However, the property $cache is declared as type string|yii\caching\CacheInterface. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

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