This repository was archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTRouter.php
More file actions
72 lines (64 loc) · 1.79 KB
/
STRouter.php
File metadata and controls
72 lines (64 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Represents the shamtool API router.
*/
class STRouter extends \Bramus\Router\Router {
/** @var callable */
private $badCallback = null;
/** @var callable */
private $forbiddenCallback = null;
private static ?STRouter $instance = null;
private function __construct() {}
/**
* Set the 403 (Bad Request) handling function.
*
* @param callable $fn The function to be executed
*/
public function set400(callable $fn) {
$this->badCallback = $fn;
}
/**
* Set the 403 (Forbidden) handling function.
*
* @param callable $fn The function to be executed
*/
public function set403(callable $fn) {
$this->forbiddenCallback = $fn;
}
/**
* Triggers 403 (Forbidden) response
*
* @param boolean? $quit Whether to additionally exit serving the request.
*/
public function trigger400(?bool $quit = false) {
if ($this->badCallback) {
call_user_func($this->badCallback);
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
}
if ($quit == true) {
exit(403);
}
}
/**
* Triggers 403 (Forbidden) response
*
* @param boolean? $quit Whether to additionally exit serving the request.
*/
public function trigger403(?bool $quit = false) {
if ($this->forbiddenCallback) {
call_user_func($this->forbiddenCallback);
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
}
if ($quit == true) {
exit(403);
}
}
public static function getInstance() : STRouter {
if (self::$instance == null) {
self::$instance = new STRouter();
}
return self::$instance;
}
}