Skip to content

Commit 3d58e7e

Browse files
author
MrAnyx
committed
[fix] Fixed if package timephp/rest exists
1 parent 17cc782 commit 3d58e7e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/Foundation/Router.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,18 @@ public function run() {
136136
public function initialize(array $routes): self{
137137
foreach($routes as $route){
138138
$type = $route["type"] ?? "web";
139-
$tmpPrefix = $route["prefix"] ?? "";
140-
if(!preg_match("/^\/[a-zA-Z0-9_-]+$/", $tmpPrefix) && $tmpPrefix !== ""){
141-
throw new RouterException("$tmpPrefix isn't a valid prefix. Prefixes must be like /prefix_name_561 or /prefix-name-120", 3004);
139+
if(!class_exists(\TimePHP\Rest\AbstractRestController::class)) {
140+
throw new RouterException("You can not use the api type in your routes without installing the following package : timephp/rest", 3004);
141+
} else {
142+
$tmpPrefix = $route["prefix"] ?? "";
143+
if(!preg_match("/^\/[a-zA-Z0-9_-]+$/", $tmpPrefix) && $tmpPrefix !== ""){
144+
throw new RouterException("$tmpPrefix isn't a valid prefix. Prefixes must be like /prefix_name_561 or /prefix-name-120", 3005);
145+
}
146+
$prefix = ($route["type"] === "api" && !array_key_exists("prefix", $route)) ? "/api" : $tmpPrefix;
147+
$method = $route["method"];
148+
$function = (array_key_exists("controller", $route) && array_key_exists("function", $route)) ? sprintf("%s#%s", $route["controller"], $route["function"]) : $route["function"];
149+
$this->$method($prefix.$route["url"], $function, $route["name"]);
142150
}
143-
$prefix = ($route["type"] === "api" && !array_key_exists("prefix", $route)) ? "/api" : $tmpPrefix;
144-
$method = $route["method"];
145-
$function = (array_key_exists("controller", $route) && array_key_exists("function", $route)) ? sprintf("%s#%s", $route["controller"], $route["function"]) : $route["function"];
146-
$this->$method($prefix.$route["url"], $function, $route["name"]);
147151
}
148152
return $this;
149153
}

0 commit comments

Comments
 (0)