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 CallbackRegistrar |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The callbacks' proxies. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $proxies; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a new CallbacksRegistrar instance. |
21
|
|
|
*/ |
22
|
128 |
|
public function __construct() |
23
|
|
|
{ |
24
|
128 |
|
$this->proxies = []; |
25
|
128 |
|
} |
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
|
80 |
|
public function addCallback(string $name, callable $callback) |
36
|
|
|
{ |
37
|
80 |
|
$this->proxies['or' . Str::studly($name)] = $callback; |
38
|
80 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Load callbacks proxies from a given associative array. |
42
|
|
|
* |
43
|
|
|
* @param array $callbacks |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
80 |
|
public function loadCallbacks(array $callbacks) |
48
|
|
|
{ |
49
|
80 |
|
foreach ($callbacks as $key => $callback) { |
50
|
64 |
|
$this->addCallback($key, $callback); |
51
|
|
|
} |
52
|
80 |
|
} |
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
|
48 |
|
public function callbacks(array $callbacks = null) |
66
|
|
|
{ |
67
|
48 |
|
if ($callbacks) { |
68
|
16 |
|
return $this->loadCallbacks($callbacks); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
32 |
|
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
|
16 |
|
public function parseCallbacks(string $class) |
85
|
|
|
{ |
86
|
16 |
|
$reflection = new ReflectionClass($class); |
87
|
16 |
|
$callbacks = $reflection->getMethods(ReflectionMethod::IS_STATIC); |
88
|
|
|
|
89
|
16 |
|
foreach ($callbacks as $callback) { |
90
|
16 |
|
$this->addCallback($callback->getName(), $callback->getClosure()); |
91
|
|
|
} |
92
|
16 |
|
} |
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
|
16 |
|
public function callback(string $name, callable $callable = null) |
105
|
|
|
{ |
106
|
16 |
|
if (is_callable($callable)) { |
107
|
|
|
return $this->addCallback($name, $callable); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
16 |
|
if ($this->hasProxy($name)) { |
111
|
16 |
|
return $this->proxies[$name]; |
112
|
|
|
} |
113
|
|
|
|
114
|
16 |
|
if ($this->hasCallback($name)) { |
115
|
16 |
|
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
|
48 |
|
public function hasCallback(string $name) |
129
|
|
|
{ |
130
|
48 |
|
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
|
48 |
|
public function hasProxy(string $proxy) |
141
|
|
|
{ |
142
|
48 |
|
return array_key_exists($proxy, $this->proxies); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.