1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework\Symfony\HttpKernel; |
8
|
|
|
|
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use SignpostMarv\DaftFramework\Framework; |
11
|
|
|
use SignpostMarv\DaftRouter\DaftSource; |
12
|
|
|
use SignpostMarv\DaftRouter\Router\Compiler; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
16
|
|
|
|
17
|
|
|
class HttpKernel extends Framework implements HttpKernelInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $routerCacheFile; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array<int, string> |
26
|
|
|
*/ |
27
|
|
|
private $routerSources; |
28
|
|
|
|
29
|
16 |
|
public function __construct(string $baseUrl, string $basePath, array $config = []) |
30
|
|
|
{ |
31
|
16 |
|
parent::__construct($baseUrl, $basePath, $config); |
32
|
|
|
|
33
|
8 |
|
$this->routerCacheFile = $config[DaftSource::class]['cacheFile']; |
34
|
|
|
|
35
|
8 |
|
$this->routerSources = array_values(array_filter( |
36
|
8 |
|
$config[DaftSource::class]['sources'], |
37
|
8 |
|
'is_string' |
38
|
|
|
)); |
39
|
8 |
|
} |
40
|
|
|
|
41
|
8 |
|
public function handle( |
42
|
|
|
Request $request, |
43
|
|
|
$type = HttpKernelInterface::MASTER_REQUEST, |
44
|
|
|
$catch = true |
45
|
|
|
) : Response { |
46
|
8 |
|
return $this->handleStrict($request, $type, $catch); |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
public function handleStrict( |
50
|
|
|
Request $request, |
51
|
|
|
int $type = HttpKernelInterface::MASTER_REQUEST, |
|
|
|
|
52
|
|
|
bool $catch = true |
|
|
|
|
53
|
|
|
) : Response { |
54
|
8 |
|
self::PairWithRequest($this, $request); |
55
|
|
|
|
56
|
8 |
|
$dispatcher = Compiler::ObtainDispatcher( |
57
|
|
|
[ |
58
|
8 |
|
'cacheFile' => $this->routerCacheFile, |
59
|
|
|
], |
60
|
8 |
|
...$this->routerSources |
61
|
|
|
); |
62
|
|
|
|
63
|
8 |
|
return $dispatcher->handle($request, parse_url($this->ObtainBaseUrl(), PHP_URL_PATH)); |
64
|
|
|
} |
65
|
|
|
|
66
|
16 |
|
protected function ValidateConfig(array $config) : void |
67
|
|
|
{ |
68
|
|
|
if ( |
69
|
|
|
! isset( |
70
|
16 |
|
$config[DaftSource::class], |
71
|
16 |
|
$config[DaftSource::class]['cacheFile'], |
72
|
16 |
|
$config[DaftSource::class]['sources'] |
73
|
|
|
) |
74
|
|
|
) { |
75
|
2 |
|
throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class)); |
76
|
14 |
|
} elseif ( ! is_string($config[DaftSource::class]['cacheFile'])) { |
77
|
2 |
|
throw new InvalidArgumentException(sprintf( |
78
|
2 |
|
'%s config does not specify "%s" correctly.', |
79
|
2 |
|
DaftSource::class, |
80
|
2 |
|
'cacheFile' |
81
|
|
|
)); |
82
|
12 |
|
} elseif ( ! is_array($config[DaftSource::class]['sources'])) { |
83
|
2 |
|
throw new InvalidArgumentException(sprintf( |
84
|
2 |
|
'%s config does not specify "%s" correctly.', |
85
|
2 |
|
DaftSource::class, |
86
|
2 |
|
'sources' |
87
|
|
|
)); |
88
|
|
|
} elseif ( |
89
|
10 |
|
file_exists($config[DaftSource::class]['cacheFile']) && |
90
|
10 |
|
! $this->FileIsUnderBasePath($config[DaftSource::class]['cacheFile']) |
91
|
|
|
) { |
92
|
2 |
|
throw new InvalidArgumentException(sprintf( |
93
|
2 |
|
'%s config property cacheFile does not exist under the framework base path.', |
94
|
2 |
|
DaftSource::class |
95
|
|
|
)); |
96
|
|
|
} |
97
|
8 |
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.