1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionMethod; |
9
|
|
|
|
10
|
|
|
class CallbacksRegistrar |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The callbacks' proxies. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $proxies; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a new CallbacksRegistrar instance. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->proxies = []; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Add a callback proxy from a given name and callable. |
29
|
|
|
* |
30
|
|
|
* @param string $name |
31
|
|
|
* @param callable $callback |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function addCallback(string $name, callable $callback) |
36
|
|
|
{ |
37
|
|
|
$this->proxies['or' . Str::studly($name)] = $callback; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Load callbacks proxies from a given associative array. |
42
|
|
|
* |
43
|
|
|
* @param array $callbacks |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function loadCallbacks(array $callbacks) |
48
|
|
|
{ |
49
|
|
|
foreach ($callbacks as $key => $callback) { |
50
|
|
|
$this->addCallback($key, $callback); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get or Load callbacks. |
56
|
|
|
* |
57
|
|
|
* If the callbacks parameter is present the callbacks |
58
|
|
|
* will be loaded, otherwise the current callbacks array |
59
|
|
|
* will be returned. |
60
|
|
|
* |
61
|
|
|
* @param array|null $callbacks |
62
|
|
|
* |
63
|
|
|
* @return array|null |
64
|
|
|
*/ |
65
|
|
|
public function callbacks(array $callbacks = null) |
66
|
|
|
{ |
67
|
|
|
if ($callbacks) { |
68
|
|
|
return $this->loadCallbacks($callbacks); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->proxies; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Parse callbacks from a given class. |
76
|
|
|
* |
77
|
|
|
* This method will use reflection to loop through all of the static |
78
|
|
|
* methods. |
79
|
|
|
* |
80
|
|
|
* @param string $class |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function parseCallbacks(string $class) |
85
|
|
|
{ |
86
|
|
|
$reflection = new ReflectionClass($class); |
87
|
|
|
$callbacks = $reflection->getMethods(ReflectionMethod::IS_STATIC); |
88
|
|
|
|
89
|
|
|
foreach ($callbacks as $callback) { |
90
|
|
|
$this->addCallback($callback->getName(), $callback->getClosure()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get/Set the callable for a given callback name. |
96
|
|
|
* |
97
|
|
|
* @param string $name |
98
|
|
|
* @param callable|null $callable |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
* |
102
|
|
|
* @throws \Exception |
103
|
|
|
*/ |
104
|
|
|
public function callback(string $name, callable $callable = null) |
105
|
|
|
{ |
106
|
|
|
if (is_callable($callable)) { |
107
|
|
|
return $this->addCallback($name, $callable); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->hasProxy($name)) { |
111
|
|
|
return $this->proxies[$name]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->hasCallback($name)) { |
115
|
|
|
return $this->proxies['or' . Str::ucfirst($name)]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
throw new Exception("Undefined callback [$name]"); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Determine if a given callback exists. |
123
|
|
|
* |
124
|
|
|
* @param string $name |
125
|
|
|
* |
126
|
|
|
* @return boolean |
127
|
|
|
*/ |
128
|
|
|
public function hasCallback(string $name) |
129
|
|
|
{ |
130
|
|
|
return array_key_exists('or' . Str::ucfirst($name), $this->proxies); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Determine if a given proxy exists. |
135
|
|
|
* |
136
|
|
|
* @param string $proxy |
137
|
|
|
* |
138
|
|
|
* @return boolean |
139
|
|
|
*/ |
140
|
|
|
public function hasProxy(string $proxy) |
141
|
|
|
{ |
142
|
|
|
return array_key_exists($proxy, $this->proxies); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|