Robert 4 months ago
parent 14b14bcc46
commit 14524359d7
  1. 24
      protected/src/classes/example_middleware.php
  2. 44
      protected/src/classes/route_service_provider.php
  3. 16
      protected/src/controllers/app/cookie_ctrl.php
  4. 62
      protected/src/controllers/app/home_ctrl.php
  5. 8
      protected/src/controllers/app/jwt_ctrl.php
  6. 14
      protected/src/controllers/app/rss_ctrl.php
  7. 22
      protected/src/services/on_sessions.php
  8. 12
      public/index.php

@ -0,0 +1,24 @@
<?php
declare(strict_types = 1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
namespace Project\classes;
use \CodeHydrater\http\request as Request;
use \CodeHydrater\http\response as Response;
class example_middleware {
public function __invoke(Request $request, Response $response, $next) {
$response = $next($request, $response);
// After controller
$response->set_content($response->get_content() . "\n<!-- Served by ExampleMiddleware -->");
return $response;
}
}

@ -0,0 +1,44 @@
<?php
declare(strict_types = 1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
namespace Project\classes;
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;
}
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();
return $app->load_controller($request, $response);
} else {
return $returned_route['returned'];
}
});
}
}

@ -9,21 +9,27 @@ declare(strict_types = 1);
*/ */
namespace Project\controllers\app; namespace Project\controllers\app;
use \CodeHydrater\http\request as Request;
use \CodeHydrater\http\response as Response;
class cookie_ctrl { class cookie_ctrl {
public function __construct() { public function __construct(private Request $request, private Response $response) {
\CodeHydrater\security::init(); // Init Sessions \CodeHydrater\security::init_sessions(); // Init Sessions
} }
public function save() { public function save(): Response {
$_SESSION['testing_user'] = "Bob"; $_SESSION['testing_user'] = "Bob";
$_SESSION['awesome'] = true; $_SESSION['awesome'] = true;
echo "Saved...";
$this->response->set_content('Saved!');
return $this->response;
} }
public function read() { public function read(): Response {
var_dump($_SESSION); var_dump($_SESSION);
// var_dump($_SESSION['admin'] ?? ""); // var_dump($_SESSION['admin'] ?? "");
return $this->response;
} }
} }

@ -9,7 +9,9 @@ declare(strict_types = 1);
*/ */
namespace Project\controllers\app; namespace Project\controllers\app;
use CodeHydrater\enums\view_type as ViewType; use \CodeHydrater\enums\view_type as ViewType;
use \CodeHydrater\http\request as Request;
use \CodeHydrater\http\response as Response;
/** /**
* Description of home_ctrl URL Route: /FOLDER/FILE/METHOD * Description of home_ctrl URL Route: /FOLDER/FILE/METHOD
@ -17,7 +19,8 @@ use CodeHydrater\enums\view_type as ViewType;
*/ */
class home_ctrl { class home_ctrl {
private $footer; private $footer;
public function __construct() {
public function __construct(private Request $request, private Response $response) {
// FROM on_footer_banner.php in the configs folder... // FROM on_footer_banner.php in the configs folder...
if (function_exists("\Project\get_footer")) { if (function_exists("\Project\get_footer")) {
$this->footer = \Project\get_footer(); $this->footer = \Project\get_footer();
@ -26,7 +29,7 @@ class home_ctrl {
} }
} }
public function index() { public function index(): Response {
$html = new \CodeHydrater\html_document(); $html = new \CodeHydrater\html_document();
$html->set_footer($this->footer); $html->set_footer($this->footer);
$html->set_author("Robert Strutts, plus ME!"); $html->set_author("Robert Strutts, plus ME!");
@ -36,18 +39,24 @@ class home_ctrl {
$view->set_template('main'); $view->set_template('main');
$view->include("app/header", ViewType::PHP); $view->include("app/header", ViewType::PHP);
$view->include("app/footer", ViewType::PHP); $view->include("app/footer", ViewType::PHP);
$view->render($this, "app/home_index", ViewType::PHP); $content = $view->fetch($this, "app/home_index", ViewType::PHP);
$this->response->set_content($content);
return $this->response;
} }
public function name_demo() { public function name_demo(): Response {
echo $this->footer; echo $this->footer;
$view = new \CodeHydrater\view(); $view = new \CodeHydrater\view();
$view->set('twig_data', ['name' => "John Doe"]); $view->set('twig_data', ['name' => "John Doe"]);
$view->render($this, "app/test", ViewType::TWIG); $content = $view->fetch($this, "app/test", ViewType::TWIG);
$this->response->set_content($content);
return $this->response;
} }
public function liquid() { public function liquid(): Response {
echo $this->footer; echo $this->footer;
$view = new \CodeHydrater\view(); $view = new \CodeHydrater\view();
@ -55,32 +64,49 @@ class home_ctrl {
'name' => "James Smith", 'name' => "James Smith",
'date' => date('Y-m-d') 'date' => date('Y-m-d')
]); ]);
$view->render($this, "app/liquid", ViewType::LIQUID); $content = $view->fetch($this, "app/liquid", ViewType::LIQUID);
$this->response->set_content($content);
return $this->response;
} }
public function extra() { public function extra(): Response {
$loaded = \CodeHydrater\extras_booter::tryToEnableFeature("testing", refresh: true); $loaded = \CodeHydrater\extras_booter::tryToEnableFeature("testing", refresh: true);
if ($loaded === false) { if ($loaded === false) {
echo "Unable to load Feature..."; $content = "Unable to load Feature...";
} else {
$content = "";
} }
$this->response->set_content($content);
return $this->response;
} }
public function make_hash() { public function make_hash(): Response {
echo \CodeHydrater\security::do_password_hash("Hello, World"); $content = \CodeHydrater\security::do_password_hash("Hello, World");
$this->response->set_content($content);
return $this->response;
} }
/** /**
* from Routes * from Routes
* *
*/ */
public function test(int $id) { public function test(int $id): Response {
echo "The ID for Test Example is # $id"; $content = "The ID for Test Example is # $id";
$this->response->set_content($content);
return $this->response;
} }
public function demo(?string $name="Test", ?int $limit=10, ?int $page=1) { public function demo(?string $name="Test", ?int $limit=10, ?int $page=1): Response {
echo "DEMO Name: ". \CodeHydrater\bootstrap\safer_io::p($name); $content = "DEMO Name: ". \CodeHydrater\bootstrap\safer_io::p($name);
echo "LIMIT = ". $limit; $content .= "LIMIT = ". $limit;
echo "PAGE = ". $page; $content .= "PAGE = ". $page;
$this->response->set_content($content);
return $this->response;
} }
} }

@ -9,10 +9,14 @@ declare(strict_types = 1);
*/ */
namespace Project\controllers\app; namespace Project\controllers\app;
use CodeHydrater\bootstrap\registry as Reg; use CodeHydrater\bootstrap\registry as Reg;
use \CodeHydrater\http\request as Request;
use \CodeHydrater\http\response as Response;
class jwt_ctrl { class jwt_ctrl {
public function index() {
public function __construct(private Request $request, private Response $response) { }
public function index() {
// Payload data (customize as needed) // Payload data (customize as needed)
$payload = [ $payload = [
'iss' => 'your-issuer', // Issuer 'iss' => 'your-issuer', // Issuer
@ -29,6 +33,7 @@ class jwt_ctrl {
} catch (\Exception $e) { } catch (\Exception $e) {
echo "Error generating token: " . $e->getMessage(); echo "Error generating token: " . $e->getMessage();
} }
return $this->response;
} }
public function read(array $p) { public function read(array $p) {
@ -43,5 +48,6 @@ class jwt_ctrl {
} catch (\Exception $e) { } catch (\Exception $e) {
echo "Invalid token: " . $e->getMessage(); echo "Invalid token: " . $e->getMessage();
} }
return $this->response;
} }
} }

@ -8,9 +8,14 @@ declare(strict_types = 1);
* @license MIT * @license MIT
*/ */
namespace Project\controllers\app; namespace Project\controllers\app;
use \CodeHydrater\http\request as Request;
use \CodeHydrater\http\response as Response;
class rss_ctrl { class rss_ctrl {
public function __construct(private Request $request, private Response $response) { }
public function feed() { public function feed() {
$root = [ $root = [
"feed_link" => "https://example.com/app/rss/feed", "feed_link" => "https://example.com/app/rss/feed",
@ -41,7 +46,14 @@ class rss_ctrl {
] ]
]; ];
$rss = \CodeHydrater\rss_feed::generate_rss_feed($root, $items); $rss = \CodeHydrater\rss_feed::generate_rss_feed($root, $items);
\CodeHydrater\rss_feed::output_rss($rss); $content = \CodeHydrater\rss_feed::get_rss($rss);
// \CodeHydrater\rss_feed::save_to_xml_file($rss); // this would make feed.xml in the Public folder... // \CodeHydrater\rss_feed::save_to_xml_file($rss); // this would make feed.xml in the Public folder...
$this->response->add_header('Content-Type', 'application/rss+xml; charset=utf-8');
$this->response->set_content($content);
return $this->response;
} }
} }

@ -0,0 +1,22 @@
<?php
declare(strict_types = 1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
use CodeHydrater\bootstrap\registry as Reg;
use CodeHydrater\bootstrap\configure as Config;
Reg::get('di')->register('sessions', function() {
$running = Reg::get('sessions_started') ?? false;
if ($running === false) {
$options = Config::get('sessions') ?? [];
\CodeHydrater\session_management::start($options);
Reg::set('sessions_started', true);
}
});

@ -22,3 +22,15 @@ require_once CodeHydrater_FRAMEWORK . "bootstrap/main.php";
function dd($var = 'nothing', $end = true) { function dd($var = 'nothing', $end = true) {
\CodeHydrater\common::dump($var, $end); \CodeHydrater\common::dump($var, $end);
} }
// Boot kernel
$kernel = new \CodeHydrater\http\kernel();
// Register service providers
$kernel->register_service_provider(Project\classes\route_service_provider::class);
// Register global middleware
//$kernel->add_middleware(Project\classes\example_middleware::class);
// Run
$kernel->run();
Loading…
Cancel
Save