|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author SignpostMarv |
|
4
|
|
|
*/ |
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework; |
|
8
|
|
|
|
|
9
|
|
|
use BadMethodCallException; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use ParagonIE\EasyDB\EasyDB; |
|
12
|
|
|
use ParagonIE\EasyDB\Factory; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
|
|
15
|
|
|
class Framework |
|
16
|
|
|
{ |
|
17
|
|
|
const DEFAULT_BOOL_REQUIRE_FILE_EXISTS = true; |
|
18
|
|
|
|
|
19
|
|
|
const BOOL_IN_ARRAY_STRICT = true; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $baseUrl; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $basePath; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var EasyDB|null |
|
33
|
|
|
*/ |
|
34
|
|
|
private $db; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $config; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array<string, self> |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $requestpair = []; |
|
45
|
|
|
|
|
46
|
58 |
|
public function __construct(string $baseUrl, string $basePath, array $config = []) |
|
47
|
|
|
{ |
|
48
|
58 |
|
if ( ! is_dir($basePath)) { |
|
49
|
1 |
|
throw new InvalidArgumentException('Base path must be a directory!'); |
|
50
|
57 |
|
} elseif (realpath($basePath) !== $basePath) { |
|
51
|
1 |
|
throw new InvalidArgumentException('Path should be explicitly set to via realpath!'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
56 |
|
$this->baseUrl = static::NormaliseUrl($baseUrl); |
|
55
|
56 |
|
$this->basePath = $basePath; |
|
56
|
|
|
|
|
57
|
56 |
|
$this->config = $this->ValidateConfig($config); |
|
58
|
34 |
|
} |
|
59
|
|
|
|
|
60
|
62 |
|
public static function NormaliseUrl(string $baseUrl) : string |
|
61
|
|
|
{ |
|
62
|
62 |
|
$parsed = (array) parse_url($baseUrl); |
|
63
|
|
|
|
|
64
|
62 |
|
if ( ! isset($parsed['scheme'], $parsed['host'], $parsed['path'])) { |
|
65
|
6 |
|
throw new InvalidArgumentException( |
|
66
|
6 |
|
'Base URL must have at least a scheme, host & path in order to be normalised!' |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
56 |
|
$scheme = $parsed['scheme'] ?? ''; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
56 |
|
$host = $parsed['host'] ?? ''; |
|
79
|
|
|
|
|
80
|
56 |
|
$baseUrl = $scheme . '://' . $host; |
|
81
|
|
|
|
|
82
|
56 |
|
if (isset($parsed['port'])) { |
|
83
|
4 |
|
$baseUrl .= ':' . (string) $parsed['port']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var string |
|
88
|
|
|
*/ |
|
89
|
56 |
|
$path = $parsed['path'] ?? ''; |
|
90
|
|
|
|
|
91
|
56 |
|
return $baseUrl . str_replace('//', '/', $path); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
public function ObtainDatabaseConnection() : EasyDB |
|
95
|
|
|
{ |
|
96
|
6 |
|
if ( ! ($this->db instanceof EasyDB)) { |
|
97
|
2 |
|
throw new BadMethodCallException('Database Connection not available!'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
return $this->db; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
16 |
|
public function ConfigureDatabaseConnection( |
|
104
|
|
|
string $dsn, |
|
105
|
|
|
string $username = null, |
|
106
|
|
|
string $password = null, |
|
107
|
|
|
array $options = [] |
|
108
|
|
|
) : void { |
|
109
|
16 |
|
if ($this->db instanceof EasyDB) { |
|
110
|
4 |
|
throw new BadMethodCallException('Database Connection already made!'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
16 |
|
$this->db = Factory::create($dsn, $username, $password, $options); |
|
114
|
16 |
|
} |
|
115
|
|
|
|
|
116
|
34 |
|
public function ObtainBaseUrl() : string |
|
117
|
|
|
{ |
|
118
|
34 |
|
return $this->baseUrl; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
7 |
|
public function ObtainBasePath() : string |
|
122
|
|
|
{ |
|
123
|
7 |
|
return $this->basePath; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
16 |
|
public function ObtainConfig() : array |
|
127
|
|
|
{ |
|
128
|
16 |
|
return $this->config; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
18 |
|
public function FileIsUnderBasePath( |
|
132
|
|
|
string $filename, |
|
133
|
|
|
bool $requireFileExists = self::DEFAULT_BOOL_REQUIRE_FILE_EXISTS |
|
134
|
|
|
) : bool { |
|
135
|
18 |
|
$realpath = realpath($filename); |
|
136
|
|
|
|
|
137
|
|
|
return |
|
138
|
18 |
|
( ! $requireFileExists && ! file_exists($filename)) || |
|
139
|
|
|
( |
|
140
|
9 |
|
is_file($filename) && |
|
141
|
9 |
|
is_string($realpath) && |
|
142
|
18 |
|
0 === mb_strpos($realpath, $this->basePath) |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
39 |
|
public static function PairWithRequest(self $framework, Request $request) : void |
|
147
|
|
|
{ |
|
148
|
39 |
|
self::$requestpair[spl_object_hash($request)] = $framework; |
|
149
|
39 |
|
} |
|
150
|
|
|
|
|
151
|
27 |
|
public static function ObtainFrameworkForRequest(Request $request) : self |
|
152
|
|
|
{ |
|
153
|
27 |
|
$framework = self::$requestpair[spl_object_hash($request)] ?? null; |
|
154
|
|
|
|
|
155
|
27 |
|
if ( ! ($framework instanceof self)) { |
|
156
|
18 |
|
throw new InvalidArgumentException( |
|
157
|
18 |
|
'No framework instance has been paired with the provided request!' |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
21 |
|
return $framework; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
12 |
|
public static function DisposeOfFrameworkReferences(self ...$frameworks) : void |
|
165
|
|
|
{ |
|
166
|
12 |
|
foreach (array_keys(self::$requestpair) as $hash) { |
|
167
|
12 |
|
if (in_array(self::$requestpair[$hash], $frameworks, self::BOOL_IN_ARRAY_STRICT)) { |
|
168
|
12 |
|
unset(self::$requestpair[$hash]); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
12 |
|
} |
|
172
|
|
|
|
|
173
|
6 |
|
public static function DisposeOfRequestReferences(Request ...$requests) : void |
|
174
|
|
|
{ |
|
175
|
6 |
|
foreach ($requests as $request) { |
|
176
|
6 |
|
$hash = spl_object_hash($request); |
|
177
|
|
|
|
|
178
|
6 |
|
if (isset(self::$requestpair[$hash])) { |
|
179
|
6 |
|
unset(self::$requestpair[$hash]); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
6 |
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @throws InvalidArgumentException if $config contains something not valid |
|
186
|
|
|
*/ |
|
187
|
34 |
|
protected function ValidateConfig(array $config) : array |
|
188
|
|
|
{ |
|
189
|
34 |
|
return $config; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|