Completed
Push — master ( eef2a4...504a1a )
by vistart
10:04
created

MultiDomainsManager::get()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 19
cts 19
cp 1
rs 6.7272
cc 7
eloc 16
nc 12
nop 1
crap 7
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\components;
14
15
use Yii;
16
17
/**
18
 * MultiDomainsManager is used to process multi-domains web application.
19
 * This class does not apply to the basic template, otherwise you know consequenses.
20
 * @property-read \yii\web\UrlManager $current
21
 * @since 2.0
22
 * @author vistart <[email protected]>
23
 */
24
class MultiDomainsManager extends \yii\base\Component
25
{
26
27
    /**
28
     * @var string the base domain.
29
     */
30
    public $baseDomain = '';
31
32
    /**
33
     * <Sub Domain Name> => [
34
     *     'component' => <URL Manager Component Configuration Array>,
35
     *     'schema' => 'http'(default) or 'https',
36
     * ]
37
     * For example:
38
     * ```php
39
     * $baseDomain = 'example.com';
40
     * $subDomains = [
41
     *    '' => [
42
     *         'component' => [
43
     *              // `class` could be ignored as it is `vistart\Models\components\MultiDomainsUrlManager`.
44
     *             'class' => 'yii\web\UrlManager',
45
     *             'enablePrettyUrl' => true,
46
     *             'showScriptName' => false,
47
     *             'suffix' => '.html',
48
     *             // the other properties...
49
     *         ],
50
     *         'schema' => 'http', // `schema` could be ignored as it is 'http'.
51
     *     ],
52
     *     'my' => [
53
     *         'component' => [
54
     *             'enablePrettyUrl' => true,
55
     *             'showScriptName' => false,
56
     *             'suffix' => '.html',
57
     *             'rules' => [
58
     *                 'posts/<year:\d{4}>/<category>' => 'post/index',
59
     *                 'posts' => 'post/index',
60
     *                 'post/<id:\d+>' => 'post/view',
61
     *             ],
62
     *         ],
63
     *         'schema' => 'https',
64
     *     ],
65
     *     'sso' => [
66
     *         'component' => [
67
     *             'enablePrettyUrl' => true,
68
     *             'showScriptName' => false,
69
     *             'rules' => [
70
     *                 '' => 'sso/login',
71
     *                 'logout' => 'sso/logout',
72
     *             ],
73
     *         ],
74
     *         'schema' => 'https',
75
     *     ],
76
     * ];
77
     * ```
78
     * and you can configure the `config/web.php` (basic template) or `config/main.php` (advanced template)
79
     * `components` section to add an element named `multiDomainsManager`:
80
     * ```php
81
     * $config = [
82
     *     ...
83
     *     'components' => [
84
     *         ...
85
     *         'multiDomainsManager' => [
86
     *             'baseDomain' => 'example.com',
87
     *             'subDomains' => [
88
     *                 'my' => [
89
     *                     'component' => require(<the path of 'my' web application UrlManager config file>),
90
     *                 ],
91
     *                 ...
92
     *             ],
93
     *         ],
94
     *         ...
95
     *     ],
96
     * ];
97
     * ```
98
     * @var array array of sub-domains.
99
     */
100
    public $subDomains = [];
101
102
    /**
103
     * @var string Current sub-domain name. If current sub-domain does not exist
104
     * in `$this->subDomains`, return `Yii::$app->urlManager` instead.
105
     */
106
    public $currentDomain = '';
107
108
    /**
109
     * Get UrlManager component of specified sub-domain application.
110
     * @param string $subdomain
111
     * @return \yii\web\UrlManager
112
     */
113 2
    public function get($subdomain = '')
114
    {
115 2
        if (!isset($this->subDomains[$subdomain])) {
116 1
            return null;
117
        }
118 2
        $subDomainConfig = $this->subDomains[$subdomain];
119 2
        if (!isset($subDomainConfig['component'])) {
120 1
            return null;
121
        }
122 2
        if (!isset($subDomainConfig['component']['class'])) {
123 2
            $subDomainConfig['component']['class'] = MultiDomainsUrlManager::className();
124 2
        }
125 2
        if (!isset($subDomainConfig['component']['hostInfo'])) {
126 2
            if (!isset($subDomainConfig['schema'])) {
127 1
                $subDomainConfig['schema'] = 'http';
128 1
            }
129 2
            $subDomainConfig['component']['hostInfo'] = $subDomainConfig['schema'] . // 'http' or 'https'
130 2
                "://" . // delimiter
131 2
                ($subdomain === '' ? '' : "$subdomain.") . // attach subdomain
132 2
                $this->baseDomain;                                               // base domain
133 2
        }
134 2
        return Yii::createObject($subDomainConfig['component']);
135
    }
136
137
    /**
138
     * Get URL Manager of current domain web application.
139
     * @return \yii\web\UrlManager
140
     */
141 1
    public function getCurrent()
142
    {
143 1
        return $this->get($this->currentDomain) ? : Yii::$app->urlManager;
144
    }
145
}
146