main
Robert 4 months ago
parent 6282694da2
commit faf188c951
  1. 52
      src/classes/app.php
  2. 4
      src/classes/http/response.php
  3. 69
      src/classes/http/route_service_provider.php
  4. 11
      src/classes/page_not_found.php
  5. 5
      src/classes/router.php

@ -26,7 +26,7 @@ class app {
$offset = strlen($route) - $dot_position; $offset = strlen($route) - $dot_position;
$ext = common::get_string_right($route, $offset); $ext = common::get_string_right($route, $offset);
if ($ext === ".php") { if ($ext === ".php") {
$this->local404(); return $this->local404();
} }
return substr($route, 0, $dot_position); return substr($route, 0, $dot_position);
} }
@ -48,7 +48,7 @@ class app {
/** /**
* Do not declare a return type here, as it will Error out!! * Do not declare a return type here, as it will Error out!!
*/ */
public function __construct() { public function __construct(private Request $request, private Response $response) {
$full_route = bootstrap\site_helper::get_route(); $full_route = bootstrap\site_helper::get_route();
$no_file_ext = $this->get_without_file_extension($full_route); $no_file_ext = $this->get_without_file_extension($full_route);
@ -91,26 +91,26 @@ class app {
try { try {
$filtered_uri = security::filter_uri($uri); $filtered_uri = security::filter_uri($uri);
} catch (Exception $ex) { } catch (Exception $ex) {
$this->local404(); // Route Un-Safe URI to Local 404 Page return $this->local404(); // Route Un-Safe URI to Local 404 Page
} }
$safe_folders = bootstrap\requires::filter_dir_path( $safe_folders = bootstrap\requires::filter_dir_path(
$this->get_first_chunks($filtered_uri) $this->get_first_chunks($filtered_uri)
); );
if (bootstrap\requires::is_dangerous($safe_folders)) { if (bootstrap\requires::is_dangerous($safe_folders)) {
$this->local404(); return $this->local404();
} }
$safe_folders = rtrim($safe_folders, '/'); $safe_folders = rtrim($safe_folders, '/');
if (empty($ROOT)) { if (empty($ROOT)) {
$this->local404(); return $this->local404();
} }
$safe_file = bootstrap\requires::filter_dir_path( $safe_file = bootstrap\requires::filter_dir_path(
$this->get_last_part($filtered_uri) $this->get_last_part($filtered_uri)
); );
if (bootstrap\requires::is_dangerous($safe_file)) { if (bootstrap\requires::is_dangerous($safe_file)) {
$this->local404(); return $this->local404();
} }
$test = $this->get_ctrl_dir(); $test = $this->get_ctrl_dir();
@ -118,13 +118,13 @@ class app {
//check for default site controller first //check for default site controller first
if ($dir === false) { if ($dir === false) {
$this->local404(); return $this->local404();
} else { } else {
$file = bootstrap\requires::safer_file_exists(basename($safe_file) . '_ctrl.php', $dir); $file = bootstrap\requires::safer_file_exists(basename($safe_file) . '_ctrl.php', $dir);
if ($file !== false) { if ($file !== false) {
$class = security::filter_class($safe_folders) . "\\" . security::filter_class($safe_file . "_ctrl"); $class = security::filter_class($safe_folders) . "\\" . security::filter_class($safe_file . "_ctrl");
} else { } else {
$this->local404(); return $this->local404();
} }
} }
@ -146,8 +146,11 @@ class app {
} }
} }
private function local404() { private function local404(): string {
page_not_found::error404(); $this->response->set_status_code(404);
$content = page_not_found::error404();
$this->response->set_content($content);
return $this->response;
} }
/** /**
@ -157,20 +160,23 @@ class app {
* @param string $method * @param string $method
* @retval type * @retval type
*/ */
private function action(Request $request, Response $response, string $file, string $class, string $method, $params) { private function action(string $file, string $class, string $method, $params) {
$safer_file = bootstrap\requires::safer_file_exists($file); $safer_file = bootstrap\requires::safer_file_exists($file);
if (! $safer_file) { if (! $safer_file) {
$this->local404(); return ['data'=>$this->local404()];
} }
if (empty($class)) { if (empty($class)) {
$this->local404(); return ['data'=>$this->local404()];
} }
$use_api = misc::is_api(); $use_api = misc::is_api();
$test = $this->get_ctrl_dir(); $test = $this->get_ctrl_dir();
$call_class = "\\Project\\" . $test . 'controllers\\' . $class; $call_class = "\\Project\\" . $test . 'controllers\\' . $class;
$controller = new $call_class($request, $response); $controller = new $call_class($this->request, $this->response);
// Collect controller-level middleware
$controller_middleware = $controller::$middleware ?? [];
if ($method === "error" && str_contains($class, "app") && if ($method === "error" && str_contains($class, "app") &&
method_exists($controller, $method) === false method_exists($controller, $method) === false
@ -185,18 +191,24 @@ class app {
} }
$method .= "_api"; $method .= "_api";
if (method_exists($controller, $method)) { if (method_exists($controller, $method)) {
return $controller->$method($params); return [
'data'=>$controller->$method($params), 'middleware'=>$controller_middleware
];
} else { } else {
page_not_found::error404_cli(); page_not_found::error404_cli();
} }
} else { } else {
if (!empty($method) && method_exists($controller, $method)) { if (!empty($method) && method_exists($controller, $method)) {
return $controller->$method($params); return [
'data'=>$controller->$method($params), 'middleware'=>$controller_middleware
];
} else { } else {
if (empty($method) && method_exists($controller, 'index')) { if (empty($method) && method_exists($controller, 'index')) {
return $controller->index($params); return [
'data'=>$controller->index($params), 'middleware'=>$controller_middleware
];
} else { } else {
$this->local404(); return ['data'=>$this->local404()];
} }
} }
} }
@ -205,8 +217,8 @@ class app {
/** /**
* Does load controller by calling action * Does load controller by calling action
*/ */
public function load_controller(Request $request, Response $response) { public function load_controller() {
return $this->action($request, $response, $this->file, $this->class, $this->method, $this->params); return $this->action($this->file, $this->class, $this->method, $this->params);
} }
} // end of app } // end of app

@ -35,6 +35,10 @@ class response
echo $this->content; echo $this->content;
} }
public function get_content(): string {
return $this->content;
}
public function set_content(string $content): self { public function set_content(string $content): self {
$this->content = $content; $this->content = $content;
return $this; return $this;

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
namespace CodeHydrater\http;
use \CodeHydrater\http\service_provider as ServiceProvider;
use \CodeHydrater\http\kernel as Kernel;
use \CodeHydrater\http\request as Request;
use \CodeHydrater\http\response as Response;
use \CodeHydrater\router as Router;
use \CodeHydrater\app as App;
/**
* Description of route_service_provider
* Setup Router and Controllers
*
* @author Robert Strutts <Bob_586@Yahoo.com>
*/
class route_service_provider extends ServiceProvider {
protected Kernel $kernel;
public function __construct(Kernel $kernel) {
$this->kernel = $kernel;
}
private function build($handler, $response, $request, $next, $controller_middleware) {
// Build middleware stack
$middleware_stack = array_reduce(
array_reverse($controller_middleware),
function ($next, $middleware) {
return function ($request, $response) use ($next, $middleware) {
if ($middleware !== null ) {
$instance = new $middleware();
return $instance($request, $response, $next);
}
};
},
function ($request, $response) use ($handler) {
$response->set_content($handler->get_content());
return $response;
}
);
return $middleware_stack($request, $response);
}
public function register(): void {
// Add router middleware
$this->kernel->add_middleware(function (Request $request, Response $response, $next) {
$returned_route = Router::execute($request, $response);
if ($returned_route["found"] === false) {
$app = new App($request, $response);
$returned = $app->load_controller();
$a_middleware = $returned['middleware'] ?? [];
$data = $returned['data'] ?? "";
return $this->build($data, $response, $request, $next, $a_middleware);
} else {
return $this->build($returned_route['returned'], $response, $request, $next, $returned_route['middleware']);
}
});
}
}

@ -37,7 +37,7 @@ class page_not_found {
/** /**
* Displays 404 Page not Found * Displays 404 Page not Found
*/ */
public static function error404(): void { public static function error404(): string {
if (console_app::is_cli()) { if (console_app::is_cli()) {
self::error404_cli(); self::error404_cli();
} else { } else {
@ -47,14 +47,17 @@ class page_not_found {
if ($use_api === true) { if ($use_api === true) {
self::api_method_not_found(); self::api_method_not_found();
} }
$loadA = bootstrap\requires::secure_include('404_page.php', bootstrap\UseDir::ONERROR);
// Show 404, Page Not Found Error Page! // Show 404, Page Not Found Error Page!
if (bootstrap\requires::secure_include('404_page.php', bootstrap\UseDir::ONERROR) === false) { if ($loadA === false) {
$loaded = bootstrap\requires::secure_include('views/on_error/404_page.php', bootstrap\UseDir::FRAMEWORK); $loaded = bootstrap\requires::secure_include('views/on_error/404_page.php', bootstrap\UseDir::FRAMEWORK);
if ($loaded === false) { if ($loaded === false) {
echo "<h1>404 Page Not Found!</h1>"; return "<h1>404 Page Not Found!</h1>";
} }
return $loaded;
} }
exit(1); return $loadA;
} }
/** /**

@ -431,6 +431,9 @@ class router
// init new controller // init new controller
$controller = new $controller($my_request, $my_response); $controller = new $controller($my_request, $my_response);
// Collect controller-level middleware
$controller_middleware = $controller::$middleware ?? [];
// Check if class has parent // Check if class has parent
$parentControllers = class_parents($controller); $parentControllers = class_parents($controller);
if (!empty($parentControllers)) { if (!empty($parentControllers)) {
@ -447,7 +450,7 @@ class router
//Call method //Call method
if (method_exists($controller, $method)) { if (method_exists($controller, $method)) {
$returned = call_user_func_array([$controller, $method], $params); $returned = call_user_func_array([$controller, $method], $params);
return ["found"=> true, "returned"=> $returned]; return ["found"=> true, "returned"=> $returned, "middleware"=>$controller_middlewares];
} }
} }
} }

Loading…
Cancel
Save