utilities-php /
router
| 1 | <?php |
||||||
| 2 | declare(strict_types=1); |
||||||
| 3 | |||||||
| 4 | namespace Utilities\Router; |
||||||
| 5 | |||||||
| 6 | use Utilities\Router\Interfaces\ApplicationRouteInterface; |
||||||
| 7 | use Utilities\Router\Traits\ControllerTrait; |
||||||
| 8 | use Utilities\Router\Traits\DirectoryTrait; |
||||||
| 9 | use Utilities\Router\Traits\WatcherTrait; |
||||||
| 10 | use Utilities\Router\Utils\StatusCode; |
||||||
| 11 | |||||||
| 12 | /** |
||||||
| 13 | * Application class |
||||||
| 14 | * |
||||||
| 15 | * @link https://github.com/utilities-php/router |
||||||
| 16 | * @author Shahrad Elahi (https://github.com/shahradelahi) |
||||||
| 17 | * @license https://github.com/utilities-php/router/blob/master/LICENSE (MIT License) |
||||||
| 18 | */ |
||||||
| 19 | abstract class Application implements ApplicationRouteInterface |
||||||
| 20 | { |
||||||
| 21 | |||||||
| 22 | use ControllerTrait; |
||||||
| 23 | use WatcherTrait; |
||||||
| 24 | use DirectoryTrait; |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * Reserved words in router. |
||||||
| 28 | * |
||||||
| 29 | * @var array |
||||||
| 30 | */ |
||||||
| 31 | public static array $reservedWords = [ |
||||||
| 32 | 'execute-watchers', |
||||||
| 33 | ]; |
||||||
| 34 | |||||||
| 35 | /** |
||||||
| 36 | * for handle the not found path exception, implement this method |
||||||
| 37 | * |
||||||
| 38 | * @return void |
||||||
| 39 | */ |
||||||
| 40 | public function __notFound(): void |
||||||
| 41 | { |
||||||
| 42 | Response::send(StatusCode::NOT_FOUND, [ |
||||||
| 43 | 'description' => "Not Found", |
||||||
| 44 | ]); |
||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | /** |
||||||
| 48 | * Find the directories, route and controller by defined values |
||||||
| 49 | * |
||||||
| 50 | * @param array $options ["insensitive"] |
||||||
| 51 | * @return void |
||||||
| 52 | */ |
||||||
| 53 | protected function findPath(array $options = []): void |
||||||
| 54 | { |
||||||
| 55 | if (!Origin::validate()) { |
||||||
| 56 | Response::send(StatusCode::FORBIDDEN, [ |
||||||
| 57 | 'error_message' => 'Origin not allowed', |
||||||
| 58 | ]); |
||||||
| 59 | } |
||||||
| 60 | |||||||
| 61 | Router::any('/execute-watchers', function () { |
||||||
| 62 | $this->runWatchers(); |
||||||
| 63 | }); |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | /** |
||||||
| 67 | * Resolve the route |
||||||
| 68 | * |
||||||
| 69 | * @param array $options (optional) [insensitive] |
||||||
| 70 | * @return void |
||||||
| 71 | */ |
||||||
| 72 | public function resolve(array $options = []): void |
||||||
| 73 | { |
||||||
| 74 | set_exception_handler(function (\Throwable $throwable) { |
||||||
| 75 | $this->__exception($throwable); |
||||||
| 76 | die(500); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 77 | }); |
||||||
| 78 | |||||||
| 79 | $this->__process(Router::createRequest()); |
||||||
| 80 | |||||||
| 81 | self::findPath($options); |
||||||
|
0 ignored issues
–
show
The method
Utilities\Router\Application::findPath() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 82 | } |
||||||
| 83 | |||||||
| 84 | /** |
||||||
| 85 | * to handle the exceptions, implement this method in your class |
||||||
| 86 | * |
||||||
| 87 | * @param \Throwable $throwable |
||||||
| 88 | * @return void |
||||||
| 89 | */ |
||||||
| 90 | public function __exception(\Throwable $throwable): void |
||||||
| 91 | { |
||||||
| 92 | Response::send(StatusCode::INTERNAL_SERVER_ERROR, [ |
||||||
| 93 | 'description' => "Internal Server Error", |
||||||
| 94 | ]); |
||||||
| 95 | } |
||||||
| 96 | |||||||
| 97 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.