Bladeren bron

Improved routing and different action types

Lukas Angerer 4 jaren geleden
bovenliggende
commit
8ec6560b85

+ 6 - 8
src/bootstrap.php

@@ -9,12 +9,10 @@ AutoLoader::registerLoader();
 Router::scan("routes/*.php");
 $route = Router::findRoute($_SERVER["REQUEST_URI"], $_SERVER["REQUEST_METHOD"]);
 
-var_dump($route);
+ob_start();
+//var_dump($route);
 
-// Since we now have an autoloader, we can just "use" the PageContext class and the autloader will actually
-// find and load it. The page context represents all the contextual information that we have in our current
-// request and we use it to define our page rendering mechanism. This mechanism is mostly based on some
-// very simple conventions for where certain types of files are placed.
-$context = new PageContext($_SERVER["REQUEST_URI"]);
-// We just call "render" which will do the rest for us.
-$context->render();
+$route->execute();
+$output = ob_get_clean();
+
+print($output);

+ 18 - 0
src/lib/LogicAction.php

@@ -0,0 +1,18 @@
+<?php
+
+class LogicAction extends Action
+{
+    private $actionFunction;
+
+    public function __construct(string $path, string $method, callable $actionFunction)
+    {
+        parent::__construct($path, $method);
+        $this->actionFunction = $actionFunction;
+    }
+
+    public function execute(): void
+    {
+        http_response_code($this->statusCode);
+        ($this->actionFunction)();
+    }
+}

+ 53 - 0
src/lib/Page.php

@@ -0,0 +1,53 @@
+<?php
+
+class Page
+{
+    public $requestUri;
+    public $targetPage;
+    public $title;
+    // This holds the "main content" of the page that will be wrapped in a simple HTML wrapper
+    public $content;
+    public $user;
+
+    public function __construct($pageName, $context = array())
+    {
+
+        $this->targetPage = __DIR__ . "/../templates/pages/" . $pageName . ".php";
+        $this->requestUri = $_SERVER["REQUEST_URI"];
+
+        $this->user = User::loadFromContext();
+    }
+
+    public function render()
+    {
+        $context = $this;
+
+        // Start output buffering - everything that is "printed" while we are buffering
+        // is captured for later use which allows us to insert the content into a larger
+        // construct like the overall page template.
+        ob_start();
+
+        if (is_file($this->targetPage))
+        {
+            include $this->targetPage;
+        }
+        else
+        {
+            print("Target page does not exist: " . $this->targetPage);
+        }
+
+        // End buffering and return its contents
+        $this->content = ob_get_clean();
+
+        // Now we take that "content" and wrap it inside of a page template so that the result
+        // will be a fully functional HTML page.
+        $this->template("html");
+    }
+
+    public function template($path, $variables = array())
+    {
+        $context = $this;
+        extract($variables);
+        include __DIR__ . "/../templates/" . $path . ".php";
+    }
+}

+ 0 - 0
src/lib/RedirectAction.php


+ 5 - 4
src/lib/Router.php

@@ -6,18 +6,19 @@ class Router
 
     public static function register(Action $route): void
     {
-        self::$routes[$route->path] = $route;
+        array_push(self::$routes, $route);
     }
 
     public static function findRoute(string $path, string $method): Action
     {
-        if (self::$routes[$path])
+        $matches = array_filter(self::$routes, function ($item) use ($path, $method) { return $item->path === $path && $item->method === $method; });
+        if (count($matches) === 1)
         {
-            return self::$routes[$path];
+            return current($matches);
         }
         else
         {
-            return self::$routes["/404"]; // default route 404 handling goes here
+            return self::findRoute("/404", "GET"); // default route 404 handling goes here
         }
     }
 

+ 4 - 3
src/lib/TemplateAction.php

@@ -2,7 +2,7 @@
 
 class TemplateAction extends Action
 {
-    private $templatePath;
+    protected $templatePath;
 
     public function __construct($path, $method, $templatePath)
     {
@@ -10,10 +10,11 @@ class TemplateAction extends Action
         $this->templatePath = $templatePath;
     }
 
-    
     public function execute(): void
     {
         http_response_code($this->statusCode);
-        echo $this->templatePath;
+
+        $page = new Page($this->templatePath);
+        $page->render();
     }
 }

+ 18 - 1
src/routes/404.php

@@ -1,3 +1,20 @@
 <?php
 
-Router::register((new TemplateAction("/404", "GET", "404"))->withStatus(404));
+class NotFoundAction extends TemplateAction
+{
+    public function __construct()
+    {
+        parent::__construct("/404", "GET", "404");
+    }
+
+    public function execute(): void
+    {
+        http_response_code(404);
+
+        $page = new Page($this->templatePath);
+        $page->title = "Not Found";
+        $page->render();
+    }
+}
+
+Router::register(new NotFoundAction());

+ 49 - 0
src/routes/api-get-data.php

@@ -0,0 +1,49 @@
+<?php
+
+Router::register(new LogicAction("/api/getdata", "GET", function () {
+    $names = array(
+        "Olivia",
+        "Liam",
+        "Emma",
+        "Noah",
+        "Amelia",
+        "Oliver",
+        "Ava",
+        "Elijah",
+        "Sophia",
+        "Lucas",
+        "Charlotte",
+        "Mason",
+        "Isabella",
+        "Levi",
+        "Mia",
+        "Asher",
+        "Luna",
+        "James",
+        "Harper",
+        "Mateo",
+        "Gianna",
+        "Leo",
+        "Evelyn",
+        "Benjamin",
+        "Aria",
+        "Aiden",
+        "Ellie",
+        "Logan",
+        "Ella",
+        "Jack",
+    );
+
+    $data = array();
+
+    for ($i = 0; $i < 10; $i++)
+    {
+        array_push($data, array(
+            "id" => $i,
+            "name" => $names[rand(0, count($names) - 1)],
+            "age" => rand(5, 75),
+        ));
+    }
+
+    print(json_encode($data, JSON_PRETTY_PRINT));
+}));

+ 0 - 2
src/routes/home.php

@@ -1,5 +1,3 @@
 <?php
 
-echo "Hello from HOME route";
-
 Router::register(new TemplateAction("/", "GET", "home"));

+ 3 - 0
src/routes/welcome-details.php

@@ -0,0 +1,3 @@
+<?php
+
+Router::register(new TemplateAction("/welcome/details", "GET", "welcome/details"));

+ 3 - 0
src/routes/welcome.php

@@ -0,0 +1,3 @@
+<?php
+
+Router::register(new TemplateAction("/welcome", "GET", "welcome"));