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
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $baseUrl; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $basePath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EasyDB|null |
29
|
|
|
*/ |
30
|
|
|
private $db; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array<string, self> |
39
|
|
|
*/ |
40
|
|
|
private static $requestpair = []; |
41
|
|
|
|
42
|
96 |
|
public function __construct(string $baseUrl, string $basePath, array $config = []) |
43
|
|
|
{ |
44
|
96 |
|
if ( ! is_dir($basePath)) { |
45
|
2 |
|
throw new InvalidArgumentException('Base path must be a directory!'); |
46
|
94 |
|
} elseif (realpath($basePath) !== $basePath) { |
47
|
2 |
|
throw new InvalidArgumentException('Path should be explicitly set to via realpath!'); |
48
|
|
|
} |
49
|
|
|
|
50
|
92 |
|
$this->baseUrl = static::NormaliseUrl($baseUrl); |
51
|
92 |
|
$this->basePath = $basePath; |
52
|
|
|
|
53
|
92 |
|
$this->config = $this->ValidateConfig($config); |
54
|
68 |
|
} |
55
|
|
|
|
56
|
92 |
|
public static function NormaliseUrl(string $baseUrl) : string |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* @var array<string, string> $parsed |
60
|
|
|
*/ |
61
|
92 |
|
$parsed = parse_url($baseUrl); |
62
|
|
|
|
63
|
92 |
|
$baseUrl = $parsed['scheme'] . '://' . $parsed['host']; |
64
|
|
|
|
65
|
92 |
|
if (isset($parsed['port'])) { |
66
|
8 |
|
$baseUrl .= ':' . $parsed['port']; |
67
|
|
|
} |
68
|
|
|
|
69
|
92 |
|
return $baseUrl . str_replace('//', '/', $parsed['path']); |
70
|
|
|
} |
71
|
|
|
|
72
|
12 |
|
public function ObtainDatabaseConnection() : EasyDB |
73
|
|
|
{ |
74
|
12 |
|
if ( ! ($this->db instanceof EasyDB)) { |
75
|
4 |
|
throw new BadMethodCallException('Database Connection not available!'); |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
return $this->db; |
79
|
|
|
} |
80
|
|
|
|
81
|
32 |
|
public function ConfigureDatabaseConnection( |
82
|
|
|
string $dsn, |
83
|
|
|
string $username = null, |
84
|
|
|
string $password = null, |
85
|
|
|
array $options = [] |
86
|
|
|
) : void { |
87
|
32 |
|
if ($this->db instanceof EasyDB) { |
88
|
8 |
|
throw new BadMethodCallException('Database Connection already made!'); |
89
|
|
|
} |
90
|
|
|
|
91
|
32 |
|
$this->db = Factory::create($dsn, $username, $password, $options); |
92
|
32 |
|
} |
93
|
|
|
|
94
|
40 |
|
public function ObtainBaseUrl() : string |
95
|
|
|
{ |
96
|
40 |
|
return $this->baseUrl; |
97
|
|
|
} |
98
|
|
|
|
99
|
14 |
|
public function ObtainBasePath() : string |
100
|
|
|
{ |
101
|
14 |
|
return $this->basePath; |
102
|
|
|
} |
103
|
|
|
|
104
|
32 |
|
public function ObtainConfig() : array |
105
|
|
|
{ |
106
|
32 |
|
return $this->config; |
107
|
|
|
} |
108
|
|
|
|
109
|
36 |
|
public function FileIsUnderBasePath(string $filename, bool $requireFileExists = true) : bool |
110
|
|
|
{ |
111
|
|
|
return |
112
|
36 |
|
( ! $requireFileExists && ! file_exists($filename)) || |
113
|
36 |
|
(is_file($filename) && 0 === mb_strpos(realpath($filename), $this->basePath)); |
114
|
|
|
} |
115
|
|
|
|
116
|
50 |
|
public static function PairWithRequest(self $framework, Request $request) : void |
117
|
|
|
{ |
118
|
50 |
|
self::$requestpair[spl_object_hash($request)] = $framework; |
119
|
50 |
|
} |
120
|
|
|
|
121
|
54 |
|
public static function ObtainFrameworkForRequest(Request $request) : self |
122
|
|
|
{ |
123
|
54 |
|
$framework = self::$requestpair[spl_object_hash($request)] ?? null; |
124
|
|
|
|
125
|
54 |
|
if ( ! ($framework instanceof self)) { |
126
|
36 |
|
throw new InvalidArgumentException( |
127
|
36 |
|
'No framework instance has been paired with the provided request!' |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
42 |
|
return $framework; |
132
|
|
|
} |
133
|
|
|
|
134
|
24 |
|
public static function DisposeOfFrameworkReferences(self ...$frameworks) : void |
135
|
|
|
{ |
136
|
24 |
|
foreach (array_keys(self::$requestpair) as $hash) { |
137
|
24 |
|
if (in_array(self::$requestpair[$hash], $frameworks, true)) { |
138
|
24 |
|
unset(self::$requestpair[$hash]); |
139
|
|
|
} |
140
|
|
|
} |
141
|
24 |
|
} |
142
|
|
|
|
143
|
12 |
|
public static function DisposeOfRequestReferences(Request ...$requests) : void |
144
|
|
|
{ |
145
|
12 |
|
foreach ($requests as $request) { |
146
|
12 |
|
$hash = spl_object_hash($request); |
147
|
|
|
|
148
|
12 |
|
if (isset(self::$requestpair[$hash])) { |
149
|
12 |
|
unset(self::$requestpair[$hash]); |
150
|
|
|
} |
151
|
|
|
} |
152
|
12 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @throws InvalidArgumentException if $config contains something not valid |
156
|
|
|
*/ |
157
|
68 |
|
protected function ValidateConfig(array $config) : array |
158
|
|
|
{ |
159
|
68 |
|
return $config; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|