|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2020. |
|
4
|
|
|
* @author Paweł Antosiak <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace Gorynych\Http; |
|
10
|
|
|
|
|
11
|
|
|
use Gorynych\Exception\KernelNotBootedException; |
|
12
|
|
|
use Gorynych\Http\Dto\ProblemDetails; |
|
13
|
|
|
use Gorynych\Http\Exception\HttpException; |
|
14
|
|
|
use Gorynych\Http\Exception\NotAcceptableHttpException; |
|
15
|
|
|
use Gorynych\Http\Formatter\FormatterFactory; |
|
16
|
|
|
use Gorynych\Http\Routing\Router; |
|
17
|
|
|
use Gorynych\Resource\ResourceLoader; |
|
18
|
|
|
use Symfony\Component\Config\FileLocator; |
|
19
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
24
|
|
|
|
|
25
|
|
|
abstract class Kernel |
|
26
|
|
|
{ |
|
27
|
|
|
protected ?ContainerBuilder $container; |
|
28
|
|
|
protected string $env; |
|
29
|
|
|
protected bool $booted = false; |
|
30
|
|
|
|
|
31
|
|
|
public function boot(string $env = 'dev'): self |
|
32
|
|
|
{ |
|
33
|
|
|
$this->env = $env; |
|
34
|
|
|
|
|
35
|
|
|
$this->initializeContainer(); |
|
36
|
|
|
$this->booted = true; |
|
37
|
|
|
|
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function reboot(): self |
|
42
|
|
|
{ |
|
43
|
|
|
$this->shutdown()->boot($this->env ?? 'dev'); |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function shutdown(): self |
|
49
|
|
|
{ |
|
50
|
|
|
$this->container = null; |
|
51
|
|
|
$this->booted = false; |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @throws KernelNotBootedException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getContainer(): ContainerBuilder |
|
60
|
|
|
{ |
|
61
|
|
|
$this->ensureIsBooted(); |
|
62
|
|
|
|
|
63
|
|
|
return $this->container; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @throws KernelNotBootedException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getRouter(): Router |
|
70
|
|
|
{ |
|
71
|
|
|
$this->ensureIsBooted(); |
|
72
|
|
|
|
|
73
|
|
|
return $this->container->get('http.router'); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @throws KernelNotBootedException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getFormatterFactory(): FormatterFactory |
|
80
|
|
|
{ |
|
81
|
|
|
$this->ensureIsBooted(); |
|
82
|
|
|
|
|
83
|
|
|
return $this->container->get('http.formatter_factory'); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @throws KernelNotBootedException |
|
88
|
|
|
* @throws \Throwable |
|
89
|
|
|
*/ |
|
90
|
|
|
public function handleRequest(Request $request): Response |
|
91
|
|
|
{ |
|
92
|
|
|
$this->ensureIsBooted(); |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
|
|
$formatter = $this->getFormatterFactory()->create(...$request->getAcceptableContentTypes()); |
|
96
|
|
|
} catch (NotAcceptableHttpException $e) { |
|
97
|
|
|
return new Response($e->getMessage(), $e->getStatusCode()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
try { |
|
101
|
|
|
$operation = $this->getRouter()->findOperation($request); |
|
102
|
|
|
$output = $operation->handle($request); |
|
103
|
|
|
} catch (\Throwable $throwable) { |
|
104
|
|
|
if ( |
|
105
|
|
|
'dev' === $this->env || |
|
106
|
|
|
('test' === $this->env && !($throwable instanceof HttpException)) |
|
107
|
|
|
) { |
|
108
|
|
|
throw $throwable; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$problemDetails = ProblemDetails::fromThrowable($throwable); |
|
112
|
|
|
|
|
113
|
|
|
return $formatter->format($problemDetails, $problemDetails->status); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $formatter->format($output, $operation->getResponseStatus()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns FileLocator for project config directory |
|
121
|
|
|
*/ |
|
122
|
|
|
abstract public function getConfigLocator(): FileLocatorInterface; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Loads project configuration files |
|
126
|
|
|
*/ |
|
127
|
|
|
abstract protected function loadConfiguration(): void; |
|
128
|
|
|
|
|
129
|
|
|
private function initializeContainer(): void |
|
130
|
|
|
{ |
|
131
|
|
|
$this->container = new ContainerBuilder(); |
|
132
|
|
|
|
|
133
|
|
|
$loader = new YamlFileLoader($this->container, new FileLocator(dirname(__DIR__, 2) . '/config')); |
|
134
|
|
|
$loader->load('services.yaml'); |
|
135
|
|
|
|
|
136
|
|
|
$this->container->set('kernel.config_locator', $this->getConfigLocator()); |
|
137
|
|
|
$this->container->set('http.router', new Router(new ResourceLoader($this->container, $this->getConfigLocator()))); |
|
138
|
|
|
$this->container->set('http.formatter_factory', new FormatterFactory($this->getConfigLocator())); |
|
139
|
|
|
|
|
140
|
|
|
$this->loadConfiguration(); |
|
141
|
|
|
$this->container->compile(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @throws KernelNotBootedException |
|
146
|
|
|
*/ |
|
147
|
|
|
private function ensureIsBooted(): void |
|
148
|
|
|
{ |
|
149
|
|
|
if (false === $this->booted) { |
|
150
|
|
|
throw new KernelNotBootedException(); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|