-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernel.php
More file actions
92 lines (84 loc) · 3 KB
/
Kernel.php
File metadata and controls
92 lines (84 loc) · 3 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
<?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\Unitary\Console;
use Exception;
use MaplePHP\Emitron\Contracts\DispatchConfigInterface;
use MaplePHP\Emitron\DispatchConfig;
use MaplePHP\Unitary\Config\ConfigProps;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use MaplePHP\Unitary\Support\Router;
use Psr\Container\ContainerInterface;
use MaplePHP\Emitron\Kernel as EmitronKernel;
class Kernel
{
private ContainerInterface $container;
private array $userMiddlewares;
private ?DispatchConfig $config;
/**
* Unitary kernel file
*
* @param ContainerInterface $container
* @param array $userMiddlewares
* @param DispatchConfig|null $dispatchConfig
*/
public function __construct(
ContainerInterface $container,
array $userMiddlewares = [],
?DispatchConfig $dispatchConfig = null,
) {
$this->container = $container;
$this->userMiddlewares = $userMiddlewares;
$this->config = $dispatchConfig;
}
/**
* This will run Emitron kernel with Unitary configuration
*
* @param ServerRequestInterface $request
* @param StreamInterface|null $stream
* @return void
* @throws Exception
*/
public function run(ServerRequestInterface $request, ?StreamInterface $stream = null): void
{
if ($this->config === null) {
$this->config = $this->configuration($request);
}
$kernel = new EmitronKernel($this->container, $this->userMiddlewares, $this->config);
$kernel->run($request, $stream);
}
/**
* This is the default unitary configuration
*
* @param ServerRequestInterface $request
* @return DispatchConfigInterface
* @throws Exception
*/
private function configuration(ServerRequestInterface $request): DispatchConfigInterface
{
$config = new DispatchConfig(EmitronKernel::getConfigFilePath(), ConfigProps::class);
return $config
->setRouter(function ($routerFile) use ($request) {
$router = new Router($request->getCliKeyword(), $request->getCliArgs());
if (!is_file($routerFile)) {
throw new Exception('The routes file (' . $routerFile . ') is missing.');
}
$newRouterInst = require_once $routerFile;
if (!($newRouterInst instanceof Router)) {
throw new \RuntimeException('You need to return the router instance ' .
'at the end of the router file (' . $routerFile . ').');
}
return $newRouterInst;
})
->setProp('exitCode', 0);
}
}