|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace sergeymakinen\facades; |
|
4
|
|
|
|
|
5
|
|
|
use yii\base\Application; |
|
6
|
|
|
use yii\base\InvalidConfigException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Base facade class. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Facade |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Facaded component property accessors. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $_accessors; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The facaded application. |
|
22
|
|
|
* |
|
23
|
|
|
* @var Application |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $_app; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Facaded components. |
|
29
|
|
|
* |
|
30
|
|
|
* @var object[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $_components; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Returns a component ID being facaded. |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function getFacadeComponentId() |
|
40
|
|
|
{ |
|
41
|
|
|
throw new InvalidConfigException('Facade must impelemnt getFacadeComponentId method.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns a component being facaded. |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function getFacadeComponent() |
|
50
|
|
|
{ |
|
51
|
|
|
$id = static::getFacadeComponentId(); |
|
52
|
|
|
if (!isset(self::$_components[$id])) { |
|
53
|
|
|
self::$_components[$id] = static::getFacadeApplication()->get($id); |
|
54
|
|
|
} |
|
55
|
|
|
return self::$_components[$id]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns the facaded application. |
|
60
|
|
|
* |
|
61
|
|
|
* @return Application |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function getFacadeApplication() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!isset(self::$_app)) { |
|
66
|
|
|
self::$_app = \Yii::$app; |
|
67
|
|
|
} |
|
68
|
|
|
return self::$_app; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sets the facaded application. |
|
73
|
|
|
* |
|
74
|
|
|
* @param Application $value |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function setFacadeApplication(Application $value) |
|
79
|
|
|
{ |
|
80
|
|
|
self::$_app = $value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Redirects static calls to component instance calls. |
|
85
|
|
|
* |
|
86
|
|
|
* @inheritDoc |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function __callStatic($name, $arguments) |
|
89
|
|
|
{ |
|
90
|
|
|
$id = static::getFacadeComponentId(); |
|
91
|
|
|
if (!isset(self::$_accessors[$id])) { |
|
92
|
|
|
self::$_accessors[$id] = []; |
|
93
|
|
|
foreach ((new \ReflectionClass(static::getFacadeComponent()))->getProperties( |
|
94
|
|
|
\ReflectionProperty::IS_PUBLIC & ~\ReflectionProperty::IS_STATIC |
|
95
|
|
|
) as $property) { |
|
96
|
|
|
$accessor = ucfirst($property->getName()); |
|
97
|
|
|
self::$_accessors[$id]['get' . $accessor] = $property->getName(); |
|
98
|
|
|
self::$_accessors[$id]['set' . $accessor] = $property->getName(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
if (isset(self::$_accessors[$id][$name])) { |
|
102
|
|
|
if ($name[0] === 'g') { |
|
103
|
|
|
return static::getFacadeComponent()->{self::$_accessors[$id][$name]}; |
|
104
|
|
|
} else { |
|
105
|
|
|
static::getFacadeComponent()->{self::$_accessors[$id][$name]} = reset($arguments); |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
} else { |
|
109
|
|
|
return call_user_func_array([ |
|
110
|
|
|
static::getFacadeComponent(), |
|
111
|
|
|
$name |
|
112
|
|
|
], $arguments); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritDoc |
|
118
|
|
|
*/ |
|
119
|
|
|
private function __construct() |
|
120
|
|
|
{} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritDoc |
|
124
|
|
|
*/ |
|
125
|
|
|
private function __wakeup() |
|
126
|
|
|
{} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @inheritDoc |
|
130
|
|
|
*/ |
|
131
|
|
|
private function __clone() |
|
132
|
|
|
{} |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|