1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Testing; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Closure; |
22
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
23
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
24
|
|
|
use Psr\Http\Message\RequestInterface; |
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
26
|
|
|
use function array_merge; |
27
|
|
|
use function assert; |
28
|
|
|
use function call_user_func_array; |
29
|
|
|
use function in_array; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @package Limoncello\Testing |
33
|
|
|
*/ |
34
|
|
|
trait ApplicationWrapperTrait |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $events = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var PsrContainerInterface |
43
|
|
|
*/ |
44
|
|
|
private $container; |
45
|
|
|
|
46
|
|
|
/** |
47
|
1 |
|
* @param Closure $handler |
48
|
|
|
* |
49
|
1 |
|
* @return ApplicationWrapperInterface |
50
|
|
|
*/ |
51
|
|
|
public function addOnHandleRequest(Closure $handler): ApplicationWrapperInterface |
52
|
|
|
{ |
53
|
|
|
return $this->addEventHandler(ApplicationWrapperInterface::EVENT_ON_HANDLE_REQUEST, $handler); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
1 |
|
* @param Closure $handler |
58
|
|
|
* |
59
|
1 |
|
* @return ApplicationWrapperInterface |
60
|
|
|
*/ |
61
|
|
|
public function addOnHandleResponse(Closure $handler): ApplicationWrapperInterface |
62
|
|
|
{ |
63
|
|
|
return $this->addEventHandler(ApplicationWrapperInterface::EVENT_ON_HANDLE_RESPONSE, $handler); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
1 |
|
* @param Closure $handler |
68
|
|
|
* |
69
|
1 |
|
* @return ApplicationWrapperInterface |
70
|
|
|
*/ |
71
|
|
|
public function addOnContainerCreated(Closure $handler): ApplicationWrapperInterface |
72
|
|
|
{ |
73
|
|
|
return $this->addEventHandler(ApplicationWrapperInterface::EVENT_ON_CONTAINER_CREATED, $handler); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
1 |
|
* @param Closure $handler |
78
|
|
|
* |
79
|
1 |
|
* @return ApplicationWrapperInterface |
80
|
|
|
*/ |
81
|
|
|
public function addOnContainerLastConfigurator(Closure $handler): ApplicationWrapperInterface |
82
|
|
|
{ |
83
|
|
|
return $this->addEventHandler(ApplicationWrapperInterface::EVENT_ON_CONTAINER_LAST_CONFIGURATOR, $handler); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int $eventId |
88
|
1 |
|
* @param Closure $handler |
89
|
|
|
* |
90
|
1 |
|
* @return ApplicationWrapperInterface |
91
|
1 |
|
*/ |
92
|
1 |
|
protected function addEventHandler(int $eventId, Closure $handler): ApplicationWrapperInterface |
93
|
1 |
|
{ |
94
|
1 |
|
assert(in_array($eventId, [ |
95
|
1 |
|
ApplicationWrapperInterface::EVENT_ON_HANDLE_REQUEST, |
96
|
|
|
ApplicationWrapperInterface::EVENT_ON_HANDLE_RESPONSE, |
97
|
1 |
|
ApplicationWrapperInterface::EVENT_ON_CONTAINER_CREATED, |
98
|
|
|
ApplicationWrapperInterface::EVENT_ON_CONTAINER_LAST_CONFIGURATOR, |
99
|
|
|
]) === true); |
100
|
1 |
|
|
101
|
1 |
|
$this->events[$eventId][] = $handler; |
102
|
|
|
|
103
|
1 |
|
/** @var ApplicationWrapperInterface $self */ |
104
|
|
|
assert($this instanceof ApplicationWrapperInterface); |
105
|
|
|
$self = $this; |
106
|
|
|
|
107
|
|
|
return $self; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $eventId |
112
|
1 |
|
* @param array $arguments |
113
|
|
|
* |
114
|
1 |
|
* @return void |
115
|
1 |
|
*/ |
116
|
1 |
|
protected function dispatchEvent($eventId, array $arguments): void |
117
|
|
|
{ |
118
|
|
|
$appAndArgs = array_merge([$this], $arguments); |
119
|
|
|
foreach ($this->events[$eventId] ?? [] as $handler) { |
120
|
|
|
call_user_func_array($handler, $appAndArgs); |
121
|
|
|
} |
122
|
|
|
} |
123
|
1 |
|
|
124
|
|
|
/** |
125
|
|
|
* @return LimoncelloContainerInterface |
126
|
1 |
|
*/ |
127
|
|
|
protected function createContainerInstance(): LimoncelloContainerInterface |
128
|
1 |
|
{ |
129
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
130
|
1 |
|
$this->container = parent::createContainerInstance(); |
131
|
|
|
|
132
|
|
|
$this->dispatchEvent(ApplicationWrapperInterface::EVENT_ON_CONTAINER_CREATED, [$this->getContainer()]); |
133
|
|
|
|
134
|
|
|
return $this->container; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param LimoncelloContainerInterface $container |
139
|
|
|
* @param array|null $globalConfigurators |
140
|
1 |
|
* @param array|null $routeConfigurators |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
protected function configureContainer( |
145
|
|
|
LimoncelloContainerInterface $container, |
146
|
1 |
|
array $globalConfigurators = null, |
147
|
|
|
array $routeConfigurators = null |
148
|
1 |
|
): void { |
149
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
150
|
|
|
parent::configureContainer($container, $globalConfigurators, $routeConfigurators); |
151
|
|
|
|
152
|
|
|
$this->dispatchEvent(ApplicationWrapperInterface::EVENT_ON_CONTAINER_LAST_CONFIGURATOR, [$container]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param Closure $handler |
157
|
1 |
|
* @param RequestInterface|null $request |
158
|
|
|
* |
159
|
1 |
|
* @return ResponseInterface |
160
|
|
|
*/ |
161
|
|
|
protected function handleRequest(Closure $handler, RequestInterface $request = null): ResponseInterface |
162
|
1 |
|
{ |
163
|
|
|
$this->dispatchEvent(ApplicationWrapperInterface::EVENT_ON_HANDLE_REQUEST, [$request]); |
164
|
1 |
|
|
165
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
166
|
1 |
|
$response = parent::handleRequest($handler, $request); |
167
|
|
|
|
168
|
|
|
$this->dispatchEvent(ApplicationWrapperInterface::EVENT_ON_HANDLE_RESPONSE, [$response]); |
169
|
|
|
|
170
|
|
|
return $response; |
171
|
|
|
} |
172
|
1 |
|
|
173
|
|
|
/** |
174
|
1 |
|
* @return PsrContainerInterface |
175
|
|
|
*/ |
176
|
|
|
protected function getContainer(): PsrContainerInterface |
177
|
|
|
{ |
178
|
|
|
return $this->container; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|