Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit d97aca0

Browse files
authored
Merge pull request #39 from PhpSlides/dev
Dev
2 parents f91dab0 + 58bf200 commit d97aca0

File tree

13 files changed

+191
-218
lines changed

13 files changed

+191
-218
lines changed

Router/MapRoute.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function match(string $method, string|array $route): bool|array
3131
* ----------------------------------------------
3232
*/
3333
self::$request_uri = strtolower(
34-
preg_replace("/(^\/)|(\/$)/", '', Application::$request_uri)
34+
preg_replace("/(^\/)|(\/$)/", '', Application::$request_uri),
3535
);
3636
self::$request_uri = empty(self::$request_uri) ? '/' : self::$request_uri;
3737

@@ -128,7 +128,7 @@ public function match(string $method, string|array $route): bool|array
128128
// checks if the requested method is of the given route
129129
if (
130130
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
131-
!in_array('dynamic', self::$method)
131+
!in_array('optional', self::$method)
132132
) {
133133
http_response_code(405);
134134
exit('Method Not Allowed');
@@ -138,7 +138,7 @@ public function match(string $method, string|array $route): bool|array
138138
'method' => $_SERVER['REQUEST_METHOD'],
139139
'route' => self::$route,
140140
'params_value' => $req_value,
141-
'params' => $req
141+
'params' => $req,
142142
];
143143
}
144144

@@ -168,7 +168,7 @@ private function match_routing(): bool|array
168168
) {
169169
if (
170170
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
171-
!in_array('dynamic', self::$method)
171+
!in_array('optional', self::$method)
172172
) {
173173
http_response_code(405);
174174
exit('Method Not Allowed');
@@ -178,7 +178,7 @@ private function match_routing(): bool|array
178178

179179
return [
180180
'method' => $_SERVER['REQUEST_METHOD'],
181-
'route' => self::$route
181+
'route' => self::$route,
182182
];
183183
} else {
184184
return false;

Router/view.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpSlides;
44

5+
use component;
56
use PhpSlides\Exception;
67
use PhpSlides\Logger\Logger;
78
use PhpSlides\Traits\FileHandler;
@@ -43,6 +44,6 @@ final public static function render(string $view, mixed ...$props): mixed
4344
$file_uri = Application::$viewsDir . $file;
4445
header('Content-Type: text/html');
4546

46-
return \psl($file_uri, ...$props);
47+
return component($file_uri, ...$props);
4748
}
4849
}

src/Exception/Exception.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
<?php
2-
/**
3-
* @format
4-
*/
1+
<?php declare(strict_types=1);
52

63
namespace PhpSlides;
74

src/Exception/template/index.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
$code_values = htmlspecialchars(
33
implode('', array_values($codeSnippet['parsedCode'])),
4-
ENT_NOQUOTES
4+
ENT_NOQUOTES,
55
);
66
$code_keys = json_encode(array_keys($codeSnippet['parsedCode']));
77
$sid = session_id();
@@ -17,11 +17,7 @@
1717

1818
<script>
1919
document.head.innerHTML = ''
20-
document.title = 'Parse Error - <?php echo str_replace(
21-
__ROOT__,
22-
'/',
23-
$message
24-
); ?>'
20+
document.title = 'Parse Error - <?php echo $message; ?>'
2521
</script>
2622

2723
<style type="text/css" media="all">
@@ -121,16 +117,14 @@
121117
<body>
122118
<header>
123119
<h3>Parse Error</h3>
124-
<span><?php echo str_replace(__ROOT__, '/', $message); ?></span>
120+
<span><?php echo $message; ?></span>
125121
</header>
126122

127123
<div class="container">
128124
<span class="h">Source File »</span>
129125

130126
<div class="code-wrapper">
131-
<span><b>File: </b><?php echo str_replace(__ROOT__, '/', $file) .
132-
':' .
133-
$line; ?></span>
127+
<span><b>File: </b><?php echo $file . ':' . $line; ?></span>
134128
<pre><code class="language-php"><?php echo $code_values; ?></code></pre>
135129
</div>
136130
</div>
@@ -192,25 +186,25 @@ function highlightSpecificLines(block, linesToHighlight) {
192186
? 'https://'
193187
: 'http://';
194188

195-
$sid = session_id();
196-
$host = $protocol . $_SERVER['HTTP_HOST'] . "/hot-reload-$sid";
189+
$addr =
190+
\PhpSlides\Foundation\Application::$REMOTE_ADDR .
191+
"/hot-reload-a$sid/worker";
197192
$phpslides_version = \PhpSlides\Foundation\Application::PHPSLIDES_VERSION;
198193

199194
if (getenv('HOT_RELOAD') == 'true'): ?>
200195
<script>
201196
/**
202197
* PHPSLIDES HOT RELOAD GENERATED
198+
*
203199
* @version <?php echo $phpslides_version; ?>
200+
* @author Dave Conco <[email protected]>
201+
* @copyright 2023 - 2024 Dave Conco
204202
*/
205-
setInterval(function() {
206-
fetch('<?php echo $host; ?>', { method: 'POST' })
207-
.then(response => response.text())
208-
.then(data => {
209-
if (data === 'reload') {
210-
window.location.reload()
211-
}
212-
});
213-
}, 1000);
203+
new Worker('<?php echo $addr; ?>').addEventListener('message', (event) => {
204+
if (event.data === 'reload') {
205+
window.location.reload();
206+
}
207+
})
214208
</script>
215209
<?php endif;
216210
?>

src/Formatter/Validate.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace PhpSlides\Formatter;
44

@@ -17,7 +17,7 @@ trait Validate
1717
* If an array is passed, an array of validated values is returned.
1818
*/
1919
protected function validate(
20-
bool|float|int|string|array $data
20+
bool|float|int|string|array $data,
2121
): bool|float|int|string|array {
2222
// If the data is an array, validate each item recursively
2323
if (is_array($data)) {
@@ -45,13 +45,17 @@ protected function validate(
4545
* @return bool|float|int|string The validated and sanitized value, converted back to its original type.
4646
*/
4747
private function realValidate(
48-
bool|float|int|string $value
48+
bool|float|int|string $value,
4949
): bool|float|int|string {
5050
// Convert the value to string for sanitation
5151
$validatedValue = (string) $value;
5252

5353
// Sanitize the string to prevent potential HTML injection issues
54-
$sanitizedValue = htmlspecialchars(trim($validatedValue), ENT_NOQUOTES);
54+
$sanitizedValue = htmlspecialchars(
55+
trim($validatedValue),
56+
ENT_QUOTES,
57+
'UTF-8',
58+
);
5559

5660
// Convert the sanitized string back to its original type based on the initial value's type
5761
switch (gettype($value)) {

src/Formatter/Views/FormatIncludes.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Trait to format includes elements in PhpSlides view files.
77
*
8-
* This trait modifies the custom `<!INCLUDE>` syntax in PhpSlides view files to PHP `psl()` function calls,
8+
* This trait modifies the custom `<!INCLUDE>` syntax in PhpSlides view files to PHP `component()` function calls,
99
* handling the inclusion of files and passing attributes as parameters.
1010
*/
1111
trait FormatIncludes
@@ -25,7 +25,7 @@ public function __construct()
2525
* Replaces all includes elements in the view file contents.
2626
*
2727
* This method scans the contents for custom `<!INCLUDE>` or `<!INCLUDES>` elements,
28-
* extracts the `path` and other attributes, and converts them into PHP `psl()`
28+
* extracts the `path` and other attributes, and converts them into PHP `component()`
2929
* function calls with the appropriate parameters.
3030
*/
3131
protected function includes()
@@ -65,11 +65,11 @@ function ($matches) {
6565
// Return the formatted PHP include statement
6666
if (!empty($param)) {
6767
return '<' .
68-
"?php print_r(psl(__DIR__ . '/$path', $param)) ?" .
68+
"?php print_r(component(__DIR__ . '/$path', $param)) ?" .
6969
'>';
7070
}
7171

72-
return '<' . "?php print_r(psl(__DIR__ . '/$path')) ?" . '>';
72+
return '<' . "?php print_r(component(__DIR__ . '/$path')) ?" . '>';
7373
},
7474
$this->contents
7575
);

src/Globals/Functions.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use PhpSlides\Route;
44
use PhpSlides\Exception;
5+
use PhpSlides\Http\Request;
56
use PhpSlides\Loader\ViewLoader;
67
use PhpSlides\Loader\FileLoader;
78
use PhpSlides\Foundation\Application;
@@ -13,8 +14,9 @@
1314
const POST = 'POST';
1415
const PATCH = 'PATCH';
1516
const DELETE = 'DELETE';
16-
const RELATIVE_PATH = 'path';
17-
const ABSOLUTE_PATH = 'root_path';
17+
const REMOTE_PATH = 2;
18+
const RELATIVE_PATH = 0;
19+
const ABSOLUTE_PATH = 1;
1820

1921
/**
2022
* -----------------------------------------------------------
@@ -27,7 +29,7 @@
2729
* |
2830
* -----------------------------------------------------------
2931
*/
30-
function psl(string $filename, mixed ...$props): mixed
32+
function component(string $filename, mixed ...$props): mixed
3133
{
3234
$loaded = (new ViewLoader())->load($filename, ...$props);
3335
return $loaded->getLoad();
@@ -61,7 +63,7 @@ function add_route_name(string $name, string|array $value): void
6163
*/
6264
function route(
6365
string|null $name = null,
64-
array|null $param = null
66+
array|null $param = null,
6567
): array|object|string {
6668
$routes = $GLOBALS['__routes'] ?? [];
6769

@@ -73,7 +75,7 @@ function route(
7375
$route_class->$key = function (string ...$args) use (
7476
$routes,
7577
$value,
76-
$key
78+
$key,
7779
) {
7880
$route = '';
7981

@@ -86,14 +88,14 @@ function route(
8688
'/\{[^}]+\}/',
8789
$args[$i],
8890
$value,
89-
1
91+
1,
9092
);
9193
} else {
9294
$route = preg_replace(
9395
'/\{[^}]+\}/',
9496
$args[$i],
9597
$route,
96-
1
98+
1,
9799
);
98100
}
99101
}
@@ -121,7 +123,7 @@ function route(
121123
'/\{[^}]+\}/',
122124
$param[$i],
123125
$routes[$name],
124-
1
126+
1,
125127
);
126128
} else {
127129
$route = preg_replace('/\{[^}]+\}/', $param[$i], $route, 1);
@@ -156,7 +158,7 @@ function asset(string $filename, string $path_type = RELATIVE_PATH): string
156158
$self,
157159
'/',
158160
strrpos($self, $find),
159-
strlen($find)
161+
strlen($find),
160162
);
161163
}
162164

@@ -175,6 +177,8 @@ function asset(string $filename, string $path_type = RELATIVE_PATH): string
175177
return $path . $filename;
176178
case ABSOLUTE_PATH:
177179
return $root_path . $filename;
180+
case REMOTE_PATH:
181+
return Application::$REMOTE_ADDR . '/' . $filename;
178182
default:
179183
return $filename;
180184
}
@@ -206,7 +210,7 @@ function payload(
206210
array $data,
207211
int $expires,
208212
int $issued_at = 0,
209-
string $issuer = ''
213+
string $issuer = '',
210214
): array {
211215
$jwt = (new FileLoader())
212216
->load(__DIR__ . '/../Config/jwt.config.php')
@@ -223,9 +227,9 @@ function payload(
223227
[
224228
'iss' => $issuer,
225229
'iat' => $issued_at,
226-
'exp' => $expires
230+
'exp' => $expires,
227231
],
228-
$data
232+
$data,
229233
);
230234
}
231235

@@ -294,13 +298,21 @@ function ExceptionHandler(Throwable $exception)
294298
'Error: %s in %s on line %d',
295299
$exception->getMessage(),
296300
$file,
297-
$line
301+
$line,
298302
);
299303
}
300304

301305
// Log the detailed error message
302306
error_log($detailedMessage);
303307

308+
if ((new Request())->isAjax()) {
309+
echo json_encode([
310+
'exception' => $message,
311+
'file' => $file,
312+
'line' => $line,
313+
]);
314+
exit();
315+
}
304316
include_once __DIR__ . '/../Exception/template/index.php';
305317
}
306318

0 commit comments

Comments
 (0)