1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Facades for Yii 2 |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/sergeymakinen/yii2-facades |
6
|
|
|
* @copyright Copyright (c) 2016-2017 Sergey Makinen (https://makinen.ru) |
7
|
|
|
* @license https://github.com/sergeymakinen/yii2-facades/blob/master/LICENSE The MIT License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace sergeymakinen\facades; |
11
|
|
|
|
12
|
|
|
use yii\base\Application; |
13
|
|
|
use yii\base\InvalidConfigException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base facade class. |
17
|
|
|
*/ |
18
|
|
|
abstract class Facade |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Facaded component property accessors. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $_accessors = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The facaded application. |
29
|
|
|
* |
30
|
|
|
* @var Application |
31
|
|
|
*/ |
32
|
|
|
private static $_app; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Facaded components. |
36
|
|
|
* |
37
|
|
|
* @var object[] |
38
|
|
|
*/ |
39
|
|
|
private static $_components = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Prevents the class to be instantiated. |
43
|
|
|
*/ |
44
|
|
|
private function __construct() |
45
|
|
|
{ |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Prevents the class to be serialized. |
50
|
|
|
*/ |
51
|
|
|
private function __wakeup() |
52
|
|
|
{ |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Prevents the class to be cloned. |
57
|
|
|
*/ |
58
|
|
|
private function __clone() |
59
|
|
|
{ |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Redirects static calls to component instance calls. |
64
|
|
|
* |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
11 |
|
public static function __callStatic($name, $arguments) |
68
|
|
|
{ |
69
|
11 |
|
$id = static::getFacadeComponentId(); |
70
|
11 |
|
if (!isset(self::$_accessors[$id])) { |
71
|
11 |
|
self::$_accessors[$id] = []; |
72
|
11 |
|
foreach ((new \ReflectionClass(static::getFacadeComponent()))->getProperties( |
73
|
11 |
|
\ReflectionProperty::IS_PUBLIC & ~\ReflectionProperty::IS_STATIC |
74
|
11 |
|
) as $property) { |
75
|
11 |
|
$accessor = ucfirst($property->getName()); |
76
|
11 |
|
self::$_accessors[$id]['get' . $accessor] = $property->getName(); |
77
|
11 |
|
self::$_accessors[$id]['set' . $accessor] = $property->getName(); |
78
|
11 |
|
} |
79
|
11 |
|
} |
80
|
11 |
|
if (isset(self::$_accessors[$id][$name])) { |
81
|
3 |
|
if ($name[0] === 'g') { |
82
|
3 |
|
return static::getFacadeComponent()->{self::$_accessors[$id][$name]}; |
83
|
|
|
} else { |
84
|
1 |
|
static::getFacadeComponent()->{self::$_accessors[$id][$name]} = reset($arguments); |
85
|
1 |
|
return null; |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
11 |
|
return call_user_func_array([ |
89
|
11 |
|
static::getFacadeComponent(), |
90
|
11 |
|
$name, |
91
|
11 |
|
], $arguments); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Clears a resolved facade component. |
97
|
|
|
* |
98
|
|
|
* @param string $id |
99
|
|
|
*/ |
100
|
1 |
|
public static function clearResolvedFacadeComponent($id) |
101
|
|
|
{ |
102
|
1 |
|
unset(self::$_accessors[$id], self::$_components[$id]); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Clears all resolved facade components. |
107
|
|
|
*/ |
108
|
16 |
|
public static function clearResolvedFacadeComponents() |
109
|
|
|
{ |
110
|
16 |
|
self::$_accessors = []; |
111
|
16 |
|
self::$_components = []; |
112
|
16 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns a component ID being facaded. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
* @throws \yii\base\InvalidConfigException |
119
|
|
|
*/ |
120
|
1 |
|
public static function getFacadeComponentId() |
121
|
|
|
{ |
122
|
1 |
|
throw new InvalidConfigException('Facade must implement getFacadeComponentId method.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns a component being facaded. |
127
|
|
|
* |
128
|
|
|
* @return object |
129
|
|
|
*/ |
130
|
12 |
|
public static function getFacadeComponent() |
131
|
|
|
{ |
132
|
12 |
|
$id = static::getFacadeComponentId(); |
133
|
12 |
|
if (!isset(self::$_components[$id])) { |
134
|
12 |
|
self::$_components[$id] = static::getFacadeApplication()->get($id); |
135
|
12 |
|
} |
136
|
12 |
|
return self::$_components[$id]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the facaded application. |
141
|
|
|
* |
142
|
|
|
* @return Application |
143
|
|
|
*/ |
144
|
13 |
|
public static function getFacadeApplication() |
145
|
|
|
{ |
146
|
13 |
|
if (!isset(self::$_app)) { |
147
|
1 |
|
self::$_app = \Yii::$app; |
148
|
1 |
|
} |
149
|
13 |
|
return self::$_app; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Sets the facaded application. |
154
|
|
|
* |
155
|
|
|
* @param Application $value |
156
|
|
|
*/ |
157
|
16 |
|
public static function setFacadeApplication(Application $value) |
158
|
|
|
{ |
159
|
16 |
|
self::$_app = $value; |
160
|
16 |
|
self::clearResolvedFacadeComponents(); |
161
|
16 |
|
} |
162
|
|
|
} |
163
|
|
|
|