|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
class CallbacksRegistrar |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The callbacks registrar |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected static $registrar; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Callbacks-Constraints bindings |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected static $bindings; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Determines whether the built-in callbacks |
|
27
|
|
|
* are loaded |
|
28
|
|
|
* |
|
29
|
|
|
* @var boolean |
|
30
|
|
|
*/ |
|
31
|
|
|
protected static $loadedDefaultCallbacks= false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Determines whether the custom callbacks |
|
35
|
|
|
* are loaded |
|
36
|
|
|
* |
|
37
|
|
|
* @var boolean |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static $loadedCustomCallbacks = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Load all of the default callbacks |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
120 |
|
public static function init() |
|
47
|
|
|
{ |
|
48
|
120 |
|
if (!static::$loadedDefaultCallbacks) { |
|
49
|
10 |
|
self::loadBuiltInCallbacks(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
120 |
|
if (!static::$loadedCustomCallbacks) { |
|
53
|
10 |
|
self::loadCustomCallbacks(); |
|
54
|
|
|
} |
|
55
|
120 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Load built-in default callbacks |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
10 |
|
protected static function loadBuiltInCallbacks() |
|
63
|
|
|
{ |
|
64
|
10 |
|
$reflection = new \ReflectionClass(\LaraCrafts\GeoRoutes\Callbacks::class); |
|
65
|
|
|
|
|
66
|
10 |
|
foreach ($reflection->getMethods() as $method) { |
|
67
|
10 |
|
self::register('or' . Str::studly($method->name), $method->getClosure()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
static::$loadedDefaultCallbacks = true; |
|
71
|
10 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Load available callbacks |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
10 |
|
protected static function loadCustomCallbacks() |
|
79
|
|
|
{ |
|
80
|
10 |
|
$callbacks = config('geo-routes.routes.callbacks'); |
|
81
|
|
|
|
|
82
|
10 |
|
foreach ($callbacks as $key => $callback) { |
|
83
|
|
|
self::register('or' . Str::studly($key), $callback); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
10 |
|
static::$loadedCustomCallbacks = true; |
|
87
|
10 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Register a new callback |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @param callable $callback |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
10 |
|
public static function register(string $name, callable $callback) |
|
98
|
|
|
{ |
|
99
|
10 |
|
self::$registrar[$name] = new GeoCallback($name, $callback); |
|
100
|
10 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Determine if a callback exists in |
|
104
|
|
|
* the registrar |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $name |
|
107
|
|
|
* |
|
108
|
|
|
* @return boolean |
|
109
|
|
|
*/ |
|
110
|
70 |
|
public static function has(string $name) |
|
111
|
|
|
{ |
|
112
|
70 |
|
return Arr::has(self::$registrar, $name); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get a callback |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $name |
|
119
|
|
|
* |
|
120
|
|
|
* @return \LaraCrafts\GeoRoutes\GeoCallback |
|
121
|
|
|
* |
|
122
|
|
|
* @throws \InvalidArgumentException |
|
123
|
|
|
*/ |
|
124
|
40 |
|
public static function get(string $name) |
|
125
|
|
|
{ |
|
126
|
40 |
|
if (!self::has($name)) { |
|
127
|
|
|
throw new InvalidArgumentException(sprintf('Invalid Callback Name: "%s"', $name)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
40 |
|
return self::$registrar[$name]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get the callbacks' registrar |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function getRegistrar() |
|
139
|
|
|
{ |
|
140
|
|
|
return static::$registrar; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Set the callbacks' registrar |
|
145
|
|
|
* |
|
146
|
|
|
* @param array $registrar |
|
147
|
|
|
* |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function setRegistrar(array $registrar) |
|
151
|
|
|
{ |
|
152
|
|
|
return static::$registrar = $registrar; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Determine if a constraint has a callback |
|
157
|
|
|
* binding |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $uniqid |
|
160
|
|
|
* |
|
161
|
|
|
* @return boolean |
|
162
|
|
|
*/ |
|
163
|
40 |
|
public static function isBound(string $uniqid) |
|
164
|
|
|
{ |
|
165
|
40 |
|
return Arr::has(static::$bindings, $uniqid); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Bind a callback to a constraint |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $callback |
|
172
|
|
|
* @param array|null $arguments |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
40 |
|
public static function bind(string $callback, array $arguments = null) |
|
177
|
|
|
{ |
|
178
|
40 |
|
$uniqid = uniqid(); |
|
179
|
|
|
|
|
180
|
40 |
|
static::$bindings[$uniqid] = self::get($callback)->setArguments($arguments ?? []); |
|
181
|
|
|
|
|
182
|
40 |
|
return $uniqid; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Resolve a callback binding |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $uniqid |
|
189
|
|
|
* |
|
190
|
|
|
* @return \LaraCrafts\GeoRoutes\GeoCallback |
|
191
|
|
|
* |
|
192
|
|
|
* @throws \InvalidArgumentException |
|
193
|
|
|
*/ |
|
194
|
40 |
|
public static function resolve(string $uniqid) |
|
195
|
|
|
{ |
|
196
|
40 |
|
if (!self::isBound($uniqid)) { |
|
197
|
|
|
throw new InvalidArgumentException(sprintf('Invalid Binding Identifier: "%s"', $uniqid)); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
40 |
|
return static::$bindings[$uniqid]; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|