This repository has been archived by the owner on Apr 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BetterWpHooksFacade.php
336 lines (210 loc) · 8.33 KB
/
BetterWpHooksFacade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
declare(strict_types = 1);
namespace BetterWpHooks\Traits;
use BadMethodCallException;
use BetterWpHooks\BetterWpHooks;
use BetterWpHooks\Contracts\Dispatcher;
use BetterWpHooks\Dispatchers\WordpressDispatcher;
use BetterWpHooks\Exceptions\ConfigurationException;
use BetterWpHooks\ListenerFactory;
use BetterWpHooks\Mappers\WordpressEventMapper;
use BetterWpHooks\Mixin;
use BetterWpHooks\Testing\FakeDispatcher;
use BetterWpHooks\WordpressApi;
use Contracts\ContainerAdapter;
use Illuminate\Support\Arr;
use ReflectionClass;
use ReflectionPayload\ReflectionPayload;
use SniccoAdapter\BaseContainerAdapter;
use function BetterWpHooks\Functions\hasTrait;
/**
* Trait BetterWpHooksFacade
*
* @mixin Mixin
* @see Mixin for the public api provided.
*/
trait BetterWpHooksFacade
{
/**
* @var null|BetterWpHooks
*/
public static $instance = null;
public static function make(ContainerAdapter $container_adapter = null) : ?BetterWpHooks
{
static::setInstance($instance = static::createInstance($container_adapter));
return $instance;
}
public static function getInstance() : ?BetterWpHooks
{
return static::$instance;
}
public static function setInstance(?BetterWpHooks $better_wp_hooks)
{
static::$instance = $better_wp_hooks;
}
/**
* Invoke any matching instance method for the static method being called.
*
* @param string $method
* @param array $parameters
*
* @return mixed
* @throws ConfigurationException
*/
public static function __callStatic(string $method, array $parameters)
{
self::checkConfiguration();
$instance = self::getInstance();
$callable = [$instance, $method];
if ( ! is_callable($callable)) {
$callable = [$instance->dispatcher(), $method];
if (is_callable($callable)) {
return call_user_func_array($callable, $parameters);
}
if (self::isTestAssertion($method)) {
throw new ConfigurationException(
'Did you forget to set up the FakeDispatcher with {YourFacadeClass}::fake()?'
);
}
throw new BadMethodCallException(
'Method '.get_class($instance).'::'.$method.'() does not exist.'
);
}
return call_user_func_array($callable, $parameters);
}
/**
* @throws ConfigurationException
*/
public static function dispatch()
{
self::checkConfiguration();
$arguments = func_get_args();
$dispatcher = self::$instance->dispatcher();
if (self::isEventObject(static::class)) {
return static::tryAsObject($arguments, $dispatcher);
}
if ( is_array($arguments[0])) {
throw new ConfigurationException(
'You are trying to dispatch an event with the facade but the first argument is not a string.'
);
}
return $dispatcher->dispatch(
array_shift($arguments),
self::unwrapIfMultidimensional($arguments)
);
}
public static function tryAsObject(array $arguments, Dispatcher $dispatcher)
{
$class = new ReflectionClass(static::class);
$constructor_args = ($constructor = $class->getConstructor()) ? $constructor->getNumberOfParameters() : null;
if ( ! $constructor_args) {
return $dispatcher->dispatch($class->newInstanceArgs());
}
if ( is_object($arguments[0]) ) {
return $dispatcher->dispatch($arguments[0]);
}
if (is_array($arguments[0])) {
return $dispatcher->dispatch($class->newInstanceArgs($arguments[0]));
}
throw new ConfigurationException('event is not instantiable as object');
}
public static function mapEvent(...$args_from_wp)
{
if ($args_from_wp[0] === '') {
array_values(Arr::wrap(array_shift($args_from_wp)));
}
$reflection_payload = new ReflectionPayload(static::class, $args_from_wp);
$payload = $reflection_payload->build();
$event_object = self::$instance->container()->make(static::class, $payload);
return self::dispatcher()->dispatch($event_object);
}
/**
* Accepts a boolean as the first parameter and only
* dispatches the event when the truth test evaluates to true.
*
* @param boolean $condition
* @param mixed ...$args
*
* @return mixed
*/
public static function dispatchIf(bool $condition, ...$args)
{
if ($condition === true) {
$args = self::unwrapIfMultidimensional($args);
$event_object = new static(...$args);
if ( ! self::isEventObject($event_object)) {
throw new BadMethodCallException(
'Doing it wrong: The Plugin facade is not meant to be used for conditional dispatching. You should use event objects instead'
);
}
return static::dispatcher()->dispatch($event_object);
}
}
/**
* Accepts a boolean or a closure as the first parameter and only
* dispatches the event unless the truth test evaluates to true.
*
* @param boolean $condition
* @param mixed ...$args
*
* @return mixed
*/
public static function dispatchUnless(bool $condition, ...$args)
{
return self::dispatchIf( ! $condition, self::unwrapIfMultidimensional($args));
}
/**
* Replace the bound instance with a fake.
*
* @param array|string $events_to_fake
* @param bool $swap_in_container
*
* @return FakeDispatcher
*/
public static function fake( $events_to_fake = [], bool $swap_in_container = true ) : FakeDispatcher
{
$events_to_fake = Arr::wrap($events_to_fake);
$fake_dispatcher = new FakeDispatcher(self::$instance->dispatcher(), $events_to_fake);
self::$instance->swapDispatcher($fake_dispatcher, $swap_in_container);
return $fake_dispatcher;
}
private static function createInstance(ContainerAdapter $container_adapter = null) : BetterWpHooks
{
$container_adapter = $container_adapter ?? new BaseContainerAdapter();
return new BetterWpHooks(
$container_adapter,
$dispatcher = new WordpressDispatcher(
new ListenerFactory($container_adapter),
$wp_api = new WordpressApi()
),
new WordpressEventMapper($container_adapter ,$dispatcher, $wp_api)
);
}
private static function checkConfiguration()
{
if ( ! self::getInstance()) {
throw new ConfigurationException(
'BetterWpHooks instance not created in '.static::class.'. '.
'Did you miss to call '.static::class.'::make()?'
);
}
}
private static function isTestAssertion(string $method) : bool
{
$methods = [
'assertDispatched',
'assertDispatchedTimes',
'assertNotDispatched',
'assertNothingDispatched',
];
return in_array($method, $methods);
}
private static function unwrapIfMultidimensional(array $array) : array
{
return ( ! empty($array) && is_array($array[0])) ? $array[0] : $array;
}
private static function isEventObject($event_object) : bool
{
return ! hasTrait(BetterWpHooksFacade::class, $event_object);
}
}