|
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\Segments; |
|
10
|
|
|
|
|
11
|
|
|
use Flipbox\Relay\Helpers\RelayHelper; |
|
12
|
|
|
use Flipbox\Relay\Segments\Exceptions\InvalidResponseException; |
|
13
|
|
|
use Flipbox\Skeleton\Helpers\ArrayHelper; |
|
14
|
|
|
use Flipbox\Skeleton\Helpers\ObjectHelper; |
|
15
|
|
|
use Flipbox\Skeleton\Logger\AutoLoggerTrait; |
|
16
|
|
|
use Flipbox\Skeleton\Object\AbstractObject; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
use Relay\Runner; |
|
19
|
|
|
use Zend\Diactoros\Request; |
|
20
|
|
|
use Zend\Diactoros\Response; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Flipbox Factory <[email protected]> |
|
24
|
|
|
* @since 2.0.1 |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class AbstractSegment extends AbstractObject implements SegmentInterface |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
use AutoLoggerTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $segments; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function defaultSegments(): array |
|
40
|
|
|
{ |
|
41
|
|
|
return []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
|
|
public function init() |
|
48
|
|
|
{ |
|
49
|
|
|
if ($this->segments === null) { |
|
50
|
|
|
$this->segments = $this->defaultSegments(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
parent::init(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @param $segment |
|
59
|
|
|
* @param string|null $afterKey |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
|
|
public function addAfter(string $key, $segment, string $afterKey = null) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($afterKey === null) { |
|
65
|
|
|
$this->segments[$key] = $segment; |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (false === ($segments = ArrayHelper::insertAfter($this->segments, $afterKey, $key, $segment))) { |
|
70
|
|
|
$this->segments[$key] = $segment; |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->segments = $segments; |
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $key |
|
80
|
|
|
* @param $segment |
|
81
|
|
|
* @param string|null $beforeKey |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function addBefore(string $key, $segment, string $beforeKey = null) |
|
85
|
|
|
{ |
|
86
|
|
|
if ($beforeKey === null) { |
|
87
|
|
|
$this->segments = [$key => $segment] + $this->segments; |
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (false === ($segments = ArrayHelper::insertBefore($this->segments, $beforeKey, $key, $segment))) { |
|
92
|
|
|
$this->segments[$key] = $segment; |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->segments = $segments; |
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritdoc |
|
103
|
|
|
*/ |
|
104
|
|
|
public function run(array $config = []): ResponseInterface |
|
105
|
|
|
{ |
|
106
|
|
|
// Reconfigure? |
|
107
|
|
|
if (!empty($config)) { |
|
108
|
|
|
ObjectHelper::configure($this, $config); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
// Relay runner |
|
113
|
|
|
$runner = new Runner( |
|
114
|
|
|
$this->getSegments(), |
|
115
|
|
|
RelayHelper::createResolver() |
|
116
|
|
|
); |
|
117
|
|
|
return $runner(new Request(), new Response()); |
|
118
|
|
|
} catch (\Exception $e) { |
|
119
|
|
|
return $this->handleException($e); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function getSegments() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->segments; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param \Exception $exception |
|
134
|
|
|
* @throws InvalidResponseException |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function handleException(\Exception $exception) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->error( |
|
139
|
|
|
"Exception running relay segments: {exception}", |
|
140
|
|
|
[ |
|
141
|
|
|
'exception' => json_encode($exception) |
|
142
|
|
|
] |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
throw new InvalidResponseException( |
|
146
|
|
|
$exception->getMessage(), |
|
147
|
|
|
$exception->getCode() |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @inheritdoc |
|
153
|
|
|
*/ |
|
154
|
|
|
public function __invoke(array $config = []): ResponseInterface |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->run($config); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|