-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractKernel.php
More file actions
209 lines (188 loc) · 5.92 KB
/
AbstractKernel.php
File metadata and controls
209 lines (188 loc) · 5.92 KB
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
<?php
/**
* Unit — Part of the MaplePHP Unitary Kernel/Dispatcher,
* A simple and fast dispatcher, will work great for this solution
*
* @package: MaplePHP\Unitary
* @author: Daniel Ronkainen
* @licence: Apache-2.0 license, Copyright © Daniel Ronkainen
* Don't delete this comment, it's part of the license.
*/
declare(strict_types=1);
namespace MaplePHP\Emitron;
use MaplePHP\Container\Reflection;
use MaplePHP\Emitron\Contracts\AppInterface;
use MaplePHP\Emitron\Contracts\DispatchConfigInterface;
use MaplePHP\Emitron\Contracts\EmitterInterface;
use MaplePHP\Emitron\Contracts\KernelInterface;
use MaplePHP\Emitron\Emitters\CliEmitter;
use MaplePHP\Emitron\Emitters\HttpEmitter;
use MaplePHP\Http\Interfaces\PathInterface;
use MaplePHP\Http\ResponseFactory;
use MaplePHP\Http\Stream;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Server\RequestHandlerInterface;
abstract class AbstractKernel implements KernelInterface
{
public const CONFIG_FILE_PATH = __DIR__ . '/../emitron.config.php';
protected static ?string $configFilePath = null;
protected static ?string $routerFilePath = null;
protected ContainerInterface $container;
protected array $userMiddlewares;
protected ?DispatchConfigInterface $dispatchConfig = null;
protected array $config = [];
/**
* @param ContainerInterface $container
* @param array $userMiddlewares
* @param DispatchConfigInterface|null $dispatchConfig
* @throws \Exception
*/
public function __construct(
ContainerInterface $container,
array $userMiddlewares = [],
?DispatchConfigInterface $dispatchConfig = null,
) {
$this->userMiddlewares = $userMiddlewares;
$this->container = $container;
$this->dispatchConfig = ($dispatchConfig === null) ?
new DispatchConfig(static::getConfigFilePath()) : $dispatchConfig;
}
/**
* Makes it easy to specify a config file inside a custom kernel file
*
* @param string|null $path
* @return void
*/
public static function setConfigFilePath(?string $path): void
{
static::$configFilePath = $path;
}
/**
* Get expected config file
*
* @return string
*/
public static function getConfigFilePath(): string
{
if (static::$configFilePath === null) {
return static::CONFIG_FILE_PATH;
}
return static::$configFilePath;
}
/**
* Set router path
*
* @param string|null $path
* @return void
*/
public static function setRouterFilePath(?string $path): void
{
static::$routerFilePath = $path;
}
/**
* Get router path
*
* @return string
*/
public static function getRouterFilePath(): string
{
if(static::$routerFilePath === null) {
return realpath(dirname(self::getConfigFilePath()));
}
return static::$routerFilePath;
}
/**
* Get config instance for configure dispatch result
*
* @return DispatchConfigInterface
*/
public function getDispatchConfig(): DispatchConfigInterface
{
return $this->dispatchConfig;
}
/**
* Will initialize the request handler with default
* functionality that you would want.
*
* @param ServerRequestInterface $request
* @param StreamInterface $stream
* @param RequestHandlerInterface $finalHandler
* @param array $middlewares
* @return ResponseInterface
* @throws \ReflectionException
*/
protected function initRequestHandler(
ServerRequestInterface $request,
StreamInterface $stream,
PathInterface $path,
RequestHandlerInterface $finalHandler,
array $middlewares = []
): ResponseInterface {
$this->bindInterfaces([
"ContainerInterface" => $this->container,
"RequestInterface" => $request,
"ServerRequestInterface" => $request,
"StreamInterface" => $stream,
"PathInterface" => $path
]);
$middlewares = array_merge($this->userMiddlewares, $middlewares);
$handler = new RequestHandler($middlewares, $finalHandler);
$app = $this->container->has("app") ? $this->container->get("app") : null;
ob_start();
$response = $handler->handle($request);
$output = ob_get_clean();
if((string)$output !== "" && ($app instanceof AppInterface && !$app->isProd())) {
throw new \RuntimeException(
'Unexpected output detected during request dispatch. Controllers must write to the response body instead of using echo.'
);
}
return $response;
}
/**
* Bind instances (singletons) to interface classes so they can be resolved
* through the dependency injector.
*
* @param array<string, object> $bindings
* @return void
*/
protected function bindInterfaces(array $bindings): void
{
Reflection::interfaceFactory(function (string $className) use ($bindings) {
return $bindings[$className] ?? null;
});
}
/**
* Get the expected body (stream)
*
* @param StreamInterface|null $stream
* @return StreamInterface
*/
protected function getBody(?StreamInterface $stream): StreamInterface
{
if($stream === null) {
return new Stream($this->isCli() ? Stream::STDOUT : Stream::TEMP);
}
return $stream;
}
/**
* Check if is inside a command line interface (CLI)
*
* @return bool
*/
protected function isCli(): bool
{
return PHP_SAPI === 'cli';
}
/**
* Get emitter based on a platform
*
* @return EmitterInterface
*/
protected function createEmitter(): EmitterInterface
{
return $this->isCli() ? new CliEmitter() : new HttpEmitter();
}
}