Skip to content

Commit 859c5a5

Browse files
authored
fix and fast
1 parent 8dba311 commit 859c5a5

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

App/Core.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Core
6767
// The parameters extracted from the request URI when this route item is matched
6868
private $params;
6969

70-
// The regular expression used to match the request URI against the path of this route item
70+
// Theregular expression used to match the request URI against the path of this route item
7171
private $pathRegex;
7272

7373
public function __construct($method, $path, $controller)
@@ -143,32 +143,38 @@ public function match($request_method, $request_uri)
143143

144144
public function execute()
145145
{
146-
list($controller, $method) = explode('@', $this->controller);
147-
$controllerClass = "Monster\\App\\Controllers\\" . $controller;
146+
if (is_callable($this->controller)) {
147+
// If the controller is a closure, execute it directly
148+
call_user_func_array($this->controller, $this->params);
149+
} else {
150+
// If the controller is a string, parse it into a controller and method to execute
151+
list($controller, $method) = explode('@', $this->controller);
152+
$controllerClass = "Monster\\App\\Controllers\\" . $controller;
148153

149-
// Cache the value of class_exists and call_user_func_array
150-
static $classExists = [];
151-
static $callUserFuncArray = [];
154+
// Cache the value of class_exists and call_user_func_array
155+
static $classExists = [];
156+
static $callUserFuncArray = [];
152157

153-
if (!isset($classExists[$controllerClass])) {
154-
$classExists[$controllerClass] = class_exists($controllerClass);
155-
}
158+
if (!isset($classExists[$controllerClass])) {
159+
$classExists[$controllerClass] = class_exists($controllerClass);
160+
}
156161

157-
if ($classExists[$controllerClass]) {
158-
if (!isset($callUserFuncArray[$controllerClass])) {
159-
$callUserFuncArray[$controllerClass] = function ($controllerInstance, $method, $params) {
160-
if ($params) {
161-
$controllerInstance->$method(...$params);
162-
} else {
163-
$controllerInstance->$method();
164-
}
165-
};
162+
if ($classExists[$controllerClass]) {
163+
if (!isset($callUserFuncArray[$controllerClass])) {
164+
$callUserFuncArray[$controllerClass] = function ($controllerInstance, $method, $params) {
165+
if ($params) {
166+
$controllerInstance->$method(...$params);
167+
} else {
168+
$controllerInstance->$method();
169+
}
170+
};
171+
}
172+
$controllerInstance = new $controllerClass;
173+
$callUserFuncArray[$controllerClass]($controllerInstance, $method, $this->params);
174+
} else {
175+
http_response_code(500);
176+
echo "Internal Server Error: Controller class not found";
166177
}
167-
$controllerInstance = new $controllerClass;
168-
$callUserFuncArray[$controllerClass]($controllerInstance, $method, $this->params);
169-
} else {
170-
http_response_code(500);
171-
echo "Internal Server Error: Controller class not found";
172178
}
173179
}
174180
}

App/Route.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public static function group($prefix, $callback)
6363
self::setPrefix($previousPrefix);
6464
}
6565

66+
public static function closure($method, $path, $closure, $prefix = null)
67+
{
68+
// Create a new route item with the specified HTTP method, path, and closure as the controller
69+
self::$routes[] = new Core($method, self::prefixPath($path, $prefix), $closure);
70+
}
71+
6672
public static function run()
6773
{
6874
$request_method = $_SERVER['REQUEST_METHOD'];

0 commit comments

Comments
 (0)