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