1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.5 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
use Symfony\Component\VarExporter\Exception\ExceptionInterface; |
16
|
|
|
use Quantum\Libraries\Config\Exceptions\ConfigException; |
17
|
|
|
use Quantum\Renderer\Exceptions\RendererException; |
18
|
|
|
use Quantum\Exceptions\StopExecutionException; |
19
|
|
|
use Symfony\Component\VarExporter\VarExporter; |
20
|
|
|
use Quantum\Di\Exceptions\DiException; |
21
|
|
|
use Quantum\Exceptions\BaseException; |
22
|
|
|
use Quantum\Http\Response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Compose a message |
26
|
|
|
* @param string $subject |
27
|
|
|
* @param string|array $params |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
function _message(string $subject, $params): string |
31
|
|
|
{ |
32
|
|
|
if (is_array($params)) { |
33
|
|
|
return preg_replace_callback('/{%\d+}/', function () use (&$params) { |
34
|
|
|
return array_shift($params); |
35
|
|
|
}, $subject); |
36
|
|
|
} else { |
37
|
|
|
return preg_replace('/{%\d+}/', $params, $subject); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Validates base64 string |
43
|
|
|
* @param string $string |
44
|
|
|
* @return boolean |
45
|
|
|
*/ |
46
|
|
|
function valid_base64(string $string): bool |
47
|
|
|
{ |
48
|
|
|
if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$decoded = base64_decode($string, true); |
53
|
|
|
|
54
|
|
|
return $decoded !== false && base64_encode($decoded) === $string; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generates random number sequence |
59
|
|
|
* @param int $length |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
function random_number(int $length = 10): int |
63
|
|
|
{ |
64
|
|
|
$randomString = ''; |
65
|
|
|
for ($i = 0; $i < $length; $i++) { |
66
|
|
|
$randomString .= rand(0, 9); |
67
|
|
|
} |
68
|
|
|
return (int)$randomString; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Slugify the string |
73
|
|
|
* @param string $text |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
function slugify(string $text): string |
77
|
|
|
{ |
78
|
|
|
$text = trim($text, ' '); |
79
|
|
|
$text = preg_replace('/[^\p{L}\p{N}]/u', ' ', $text); |
80
|
|
|
$text = preg_replace('/\s+/', '-', $text); |
81
|
|
|
$text = trim($text, '-'); |
82
|
|
|
$text = mb_strtolower($text); |
83
|
|
|
|
84
|
|
|
if (empty($text)) { |
85
|
|
|
return 'n-a'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $text; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Stops the execution |
93
|
|
|
* @param Closure|null $closure |
94
|
|
|
* @param int|null $code |
95
|
|
|
* @return mixed |
96
|
|
|
* @throws StopExecutionException |
97
|
|
|
*/ |
98
|
|
|
function stop(Closure $closure = null, ?int $code = 0) |
99
|
|
|
{ |
100
|
|
|
if ($closure) { |
101
|
|
|
$closure(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw StopExecutionException::executionTerminated($code); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Checks the app debug mode |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
function is_debug_mode(): bool |
112
|
|
|
{ |
113
|
|
|
return filter_var(config()->get('debug'), FILTER_VALIDATE_BOOLEAN); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Handles page not found |
118
|
|
|
* @return void |
119
|
|
|
* @throws DiException |
120
|
|
|
* @throws ReflectionException |
121
|
|
|
* @throws BaseException |
122
|
|
|
* @throws ConfigException |
123
|
|
|
* @throws RendererException |
124
|
|
|
*/ |
125
|
|
|
function page_not_found() |
126
|
|
|
{ |
127
|
|
|
$acceptHeader = Response::getHeader('Accept'); |
128
|
|
|
|
129
|
|
|
$isJson = $acceptHeader === 'application/json'; |
130
|
|
|
|
131
|
|
|
if ($isJson) { |
132
|
|
|
Response::json( |
133
|
|
|
['status' => 'error', 'message' => 'Page not found'], |
134
|
|
|
404 |
135
|
|
|
); |
136
|
|
|
} else { |
137
|
|
|
Response::html( |
138
|
|
|
partial('errors' . DS . '404'), |
139
|
|
|
404 |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Gets directory classes |
146
|
|
|
* @param string $path |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
function get_directory_classes(string $path): array |
150
|
|
|
{ |
151
|
|
|
$class_names = []; |
152
|
|
|
|
153
|
|
|
if (is_dir($path)) { |
154
|
|
|
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
155
|
|
|
$phpFiles = new RegexIterator($allFiles, '/\.php$/'); |
156
|
|
|
|
157
|
|
|
foreach ($phpFiles as $file) { |
158
|
|
|
$class = pathinfo($file->getFilename()); |
159
|
|
|
$class_names[] = $class['filename']; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $class_names; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Gets the caller class |
168
|
|
|
* @param integer $index |
169
|
|
|
* @return string|null |
170
|
|
|
*/ |
171
|
|
|
function get_caller_class(int $index = 2): ?string |
172
|
|
|
{ |
173
|
|
|
$caller = debug_backtrace(); |
174
|
|
|
$caller = $caller[$index]; |
175
|
|
|
|
176
|
|
|
return $caller['class'] ?? null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Gets the caller function |
181
|
|
|
* @param integer $index |
182
|
|
|
* @return string|null |
183
|
|
|
*/ |
184
|
|
|
function get_caller_function(int $index = 2): ?string |
185
|
|
|
{ |
186
|
|
|
$caller = debug_backtrace(); |
187
|
|
|
$caller = $caller[$index]; |
188
|
|
|
|
189
|
|
|
return $caller['function'] ?? null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Exports the variable |
194
|
|
|
* @param $var |
195
|
|
|
* @return string |
196
|
|
|
* @throws ExceptionInterface |
197
|
|
|
*/ |
198
|
|
|
function export($var): string |
199
|
|
|
{ |
200
|
|
|
return VarExporter::export($var); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Checks if the entity is closure |
205
|
|
|
* @param mixed $entity |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
function is_closure($entity): bool |
209
|
|
|
{ |
210
|
|
|
return $entity instanceof Closure; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Recursively deletes folder |
215
|
|
|
* @param string $dir |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
function deleteDirectoryWithFiles(string $dir) |
219
|
|
|
{ |
220
|
|
|
if (!is_dir($dir)) { |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$files = array_diff(scandir($dir), array('.', '..')); |
225
|
|
|
|
226
|
|
|
foreach ($files as $file) { |
227
|
|
|
$path = $dir . DS . $file; |
228
|
|
|
is_dir($path) ? deleteDirectoryWithFiles($path) : unlink($path); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return rmdir($dir); |
232
|
|
|
} |