1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipbox/relay/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipbox/relay |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Flipbox\Relay\Builder; |
10
|
|
|
|
11
|
|
|
use Flipbox\Relay\Runner\Runner; |
12
|
|
|
use Flipbox\Skeleton\Helpers\ArrayHelper; |
13
|
|
|
use Flipbox\Skeleton\Logger\AutoLoggerTrait; |
14
|
|
|
use Flipbox\Skeleton\Object\AbstractObject; |
15
|
|
|
use Psr\Log\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 3.0.0 |
20
|
|
|
*/ |
21
|
|
|
class RelayBuilder extends AbstractObject implements RelayBuilderInterface |
22
|
|
|
{ |
23
|
|
|
use AutoLoggerTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array[] |
27
|
|
|
*/ |
28
|
|
|
private $middleware = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function addAfter(string $key, array $middleware, string $afterKey = null) |
34
|
|
|
{ |
35
|
|
|
if ($afterKey === null || array_key_exists($key, $this->middleware)) { |
36
|
|
|
$this->middleware[$key] = $middleware; |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (false === ($newOrder = ArrayHelper::insertAfter($this->middleware, $afterKey, $key, $middleware))) { |
41
|
|
|
$this->middleware[$key] = $middleware; |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->middleware = $newOrder; |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function addBefore(string $key, array $middleware, string $beforeKey = null) |
53
|
|
|
{ |
54
|
|
|
if (array_key_exists($key, $this->middleware)) { |
55
|
|
|
$this->middleware[$key] = $middleware; |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($beforeKey === null) { |
60
|
|
|
$this->middleware = [$key => $middleware] + $this->middleware; |
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (false === ($newOrder = ArrayHelper::insertBefore($this->middleware, $beforeKey, $key, $middleware))) { |
65
|
|
|
$this->middleware[$key] = $middleware; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->middleware = $newOrder; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function merge(string $key, array $middleware) |
77
|
|
|
{ |
78
|
|
|
if (false === array_key_exists($key, $this->middleware)) { |
79
|
|
|
$this->middleware[$key] = $middleware; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->middleware[$key] = array_merge( |
84
|
|
|
$this->middleware[$key], |
85
|
|
|
$middleware |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function build(): callable |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
return new Runner( |
98
|
|
|
$this->middleware |
99
|
|
|
); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
return $this->handleException($e); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \Exception $exception |
107
|
|
|
* @throws InvalidArgumentException |
108
|
|
|
*/ |
109
|
|
|
protected function handleException(\Exception $exception) |
110
|
|
|
{ |
111
|
|
|
$this->error( |
112
|
|
|
"Exception building relay middleware: {exception}", |
113
|
|
|
[ |
114
|
|
|
'exception' => json_encode($exception) |
115
|
|
|
] |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
throw new InvalidArgumentException( |
119
|
|
|
$exception->getMessage(), |
120
|
|
|
$exception->getCode() |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..