1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Pollux\Application; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
abstract class Facade |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The application instance being facaded. |
12
|
|
|
* |
13
|
|
|
* @var Application |
14
|
|
|
*/ |
15
|
|
|
protected static $app; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The resolved object instances. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected static $resolvedInstance; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Handle dynamic, static calls to the object. |
26
|
|
|
* |
27
|
|
|
* @param string $method |
28
|
|
|
* @param array $args |
29
|
|
|
* |
30
|
|
|
* @return mixed |
31
|
|
|
* @throws \RuntimeException |
32
|
|
|
*/ |
33
|
|
|
public static function __callStatic( $method, $args ) |
34
|
|
|
{ |
35
|
|
|
$instance = static::getFacadeRoot(); |
36
|
|
|
|
37
|
|
|
if( !$instance ) { |
38
|
|
|
throw new RuntimeException( 'A facade root has not been set.' ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $instance->$method( ...$args ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Clear all of the resolved instances. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public static function clearResolvedInstances() |
50
|
|
|
{ |
51
|
|
|
static::$resolvedInstance = []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the application instance behind the facade. |
56
|
|
|
* |
57
|
|
|
* @return Application |
58
|
|
|
*/ |
59
|
|
|
public static function getFacadeApplication() |
60
|
|
|
{ |
61
|
|
|
return static::$app; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the root object behind the facade. |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public static function getFacadeRoot() |
70
|
|
|
{ |
71
|
|
|
return static::resolveFacadeInstance( static::getFacadeAccessor() ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the application instance. |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public static function setFacadeApplication( Application $app ) |
80
|
|
|
{ |
81
|
|
|
static::$app = $app; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the registered name of the component. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
* |
89
|
|
|
* @throws \RuntimeException |
90
|
|
|
*/ |
91
|
|
|
protected static function getFacadeAccessor() |
92
|
|
|
{ |
93
|
|
|
throw new RuntimeException( 'Facade does not implement getFacadeAccessor method.' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Resolve the facade root instance from the container. |
98
|
|
|
* |
99
|
|
|
* @param string|object $name |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
protected static function resolveFacadeInstance( $name ) |
104
|
|
|
{ |
105
|
|
|
if( is_object( $name )) { |
106
|
|
|
return $name; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if( isset( static::$resolvedInstance[$name] )) { |
110
|
|
|
return static::$resolvedInstance[$name]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return static::$resolvedInstance[$name] = static::$app->make( $name ); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|