29 public static function load(
string $source): void
31 if (!file_exists($source)) {
35 $entries = file($source, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
37 foreach ($entries as $line) {
38 $line = preg_replace(
'/#.*$/',
'', $line);
40 preg_match(
'/^\s*([a-z][a-z,]+[a-z]):\s*([^\s]+)\s*->\s*([^\s]+)\s*$/', $line, $elements);
41 if (count($elements) > 3) {
42 $methods = explode(
',', $elements[1]);
44 foreach ($methods as $method) {
45 $method = strtolower($method);
46 if (
'get' == $method ||
'all' == $method) {
47 $route[
'get'] = $elements[3];
49 if (
'post' == $method ||
'all' == $method) {
50 $route[
'post'] = $elements[3];
52 if (
'put' == $method ||
'all' == $method) {
53 $route[
'put'] = $elements[3];
55 if (
'delete' == $method ||
'all' == $method) {
56 $route[
'delete'] = $elements[3];
59 if (!array_key_exists($elements[2], self::$specificRoutes)) {
60 self::$specificRoutes[$elements[2]] = [];
62 self::$specificRoutes[$elements[2]] = array_merge(self::$specificRoutes[$elements[2]], $route);
107 $protocol = array_key_exists(
'HTTPS', $_SERVER) &&
'on' === $_SERVER[
'HTTPS'] ?
'https://' :
'http://';
108 $fullUrl = $protocol . $_SERVER[
'HTTP_HOST'] . $_SERVER[
'REQUEST_URI'];
110 if (
null !== $siteUrl && 0 === strpos($fullUrl, $siteUrl)) {
111 $uri = substr($fullUrl, strlen($siteUrl));
113 $uri = $_SERVER[
'REQUEST_URI'];
116 $parts = array_filter(explode(
'/', $uri),
'strlen');
119 $className =
'\\' . self::$namespace .
'\\Controllers\\' . ucfirst(array_shift($parts));
120 $class =
new $className();
121 $method = array_shift($parts) ??
'index';
123 call_user_func_array([$class, $method], $parts);
125 if (preg_match(
'#^https?://#', $e->getMessage())) {
126 header(
'Location: ' . $e->getMessage());
128 header(
'Location: ' . $siteUrl . $e->getMessage());
164 protected static function getUri(
string $uri): string
166 $uri = explode(
'?', $uri)[0];
171 $method = strtolower($_SERVER[
'REQUEST_METHOD']);
172 foreach (self::$specificRoutes as $route => $methods) {
173 $route = preg_replace(
'/:segment/',
'[^/]+', $route);
174 $route = preg_replace(
'/:any/',
'.*', $route);
175 $route =
'#^' . $route .
'$#';
177 if (preg_match($route, $uri)) {
178 if (!array_key_exists($method, $methods)) {
182 return preg_replace($route, $methods[$method], $uri);