1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magister\Services\Support\Surrogates; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Surrogate. |
7
|
|
|
*/ |
8
|
|
|
abstract class Surrogate |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The application instance. |
12
|
|
|
* |
13
|
|
|
* @var \Magister\Magister |
14
|
|
|
*/ |
15
|
|
|
protected static $app; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The resolved object instances. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected static $resolvedInstance; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Resolve the surrogate instance from the container. |
26
|
|
|
* |
27
|
|
|
* @param string $name |
28
|
|
|
* |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
|
|
protected static function resolveSurrogateInstance($name) |
32
|
|
|
{ |
33
|
|
|
if (is_object($name)) { |
34
|
|
|
return $name; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (isset(static::$resolvedInstance[$name])) { |
38
|
|
|
return static::$resolvedInstance[$name]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return static::$resolvedInstance[$name] = static::$app[$name]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the object behind the surrogate. |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public static function getSurrogateRoot() |
50
|
|
|
{ |
51
|
|
|
return static::resolveSurrogateInstance(static::getSurrogateAccessor()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Clear all of the instances. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public static function clearResolvedInstances() |
60
|
|
|
{ |
61
|
|
|
static::$resolvedInstance = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return the name of the service. |
66
|
|
|
* |
67
|
|
|
* @throws \RuntimeException |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
protected static function getSurrogateAccessor() |
72
|
|
|
{ |
73
|
|
|
throw new \RuntimeException('The Surrogate class does not implement a getSurrogateAccessor method.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the application instance. |
78
|
|
|
* |
79
|
|
|
* @param \Magister\Magister $app |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public static function setSurrogateApplication($app) |
84
|
|
|
{ |
85
|
|
|
static::$app = $app; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the application instance. |
90
|
|
|
* |
91
|
|
|
* @return \Magister\Magister |
92
|
|
|
*/ |
93
|
|
|
public static function getSurrogateApplication() |
94
|
|
|
{ |
95
|
|
|
return static::$app; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Dynamically handle incoming requests. |
100
|
|
|
* |
101
|
|
|
* @param string $method |
102
|
|
|
* @param array $args |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public static function __callStatic($method, $args) |
107
|
|
|
{ |
108
|
|
|
$instance = static::getSurrogateRoot(); |
109
|
|
|
|
110
|
|
|
switch (count($args)) { |
111
|
|
|
case 0: |
112
|
|
|
return $instance->$method(); |
113
|
|
|
case 1: |
114
|
|
|
return $instance->$method($args[0]); |
115
|
|
|
case 2: |
116
|
|
|
return $instance->$method($args[0], $args[1]); |
117
|
|
|
case 3: |
118
|
|
|
return $instance->$method($args[0], $args[1], $args[2]); |
119
|
|
|
case 4: |
120
|
|
|
return $instance->$method($args[0], $args[1], $args[2], $args[3]); |
121
|
|
|
default: |
122
|
|
|
return call_user_func_array([$instance, $method], $args); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|