1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework; |
8
|
|
|
|
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use SignpostMarv\DaftRouter\DaftSource; |
11
|
|
|
use SignpostMarv\DaftRouter\Router\Compiler; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
|
15
|
|
|
class HttpHandler extends Framework |
16
|
|
|
{ |
17
|
|
|
const ERROR_SOURCE_CONFIG = DaftSource::class . ' config does not specify "%s" correctly.'; |
18
|
|
|
|
19
|
|
|
const ERROR_ROUTER_CACHE_FILE_PATH = |
20
|
|
|
DaftSource::class . |
21
|
|
|
' config property cacheFile does not exist under the framework base path.'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $routerCacheFile; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array<int, string> |
30
|
|
|
*/ |
31
|
|
|
private $routerSources; |
32
|
|
|
|
33
|
58 |
|
public function __construct(string $baseUrl, string $basePath, array $config = []) |
34
|
|
|
{ |
35
|
58 |
|
parent::__construct($baseUrl, $basePath, $config); |
36
|
|
|
|
37
|
34 |
|
$this->routerCacheFile = (string) ((array) $config[DaftSource::class])['cacheFile']; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string[] $sources |
41
|
|
|
*/ |
42
|
34 |
|
$sources = (array) ((array) $config[DaftSource::class])['sources']; |
43
|
|
|
|
44
|
34 |
|
$this->routerSources = array_values(array_filter($sources, 'is_string')); |
45
|
34 |
|
} |
46
|
|
|
|
47
|
26 |
|
public function handle(Request $request) : Response |
48
|
|
|
{ |
49
|
26 |
|
self::PairWithRequest($this, $request); |
50
|
|
|
|
51
|
26 |
|
$dispatcher = Compiler::ObtainDispatcher( |
52
|
|
|
[ |
53
|
26 |
|
'cacheFile' => $this->routerCacheFile, |
54
|
|
|
], |
55
|
26 |
|
...$this->routerSources |
56
|
|
|
); |
57
|
|
|
|
58
|
26 |
|
return $dispatcher->handle( |
59
|
26 |
|
$request, |
60
|
26 |
|
(string) parse_url($this->ObtainBaseUrl(), PHP_URL_PATH) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
58 |
|
protected function ValidateConfig(array $config) : array |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* @var array|null |
68
|
|
|
*/ |
69
|
58 |
|
$subConfig = $config[DaftSource::class] ?? null; |
70
|
|
|
|
71
|
58 |
|
if ( ! is_array($subConfig) || ! isset($subConfig['cacheFile'], $subConfig['sources'])) { |
72
|
18 |
|
throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class)); |
73
|
40 |
|
} elseif ( ! is_string($subConfig['cacheFile'])) { |
74
|
2 |
|
throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'cacheFile')); |
75
|
38 |
|
} elseif ( ! is_array($subConfig['sources'])) { |
76
|
2 |
|
throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'sources')); |
77
|
36 |
|
} elseif ( ! $this->FileIsUnderBasePath($subConfig['cacheFile'], false)) { |
78
|
2 |
|
throw new InvalidArgumentException(self::ERROR_ROUTER_CACHE_FILE_PATH); |
79
|
|
|
} |
80
|
|
|
|
81
|
34 |
|
return parent::ValidateConfig($config); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|