lollipop

A PHP-framework
Log | Files | Refs

commit 3d900209ec716b414cf8ab672870c039f4f060cd
parent 532bf650d12d8705200e83d6a64ea066f1fa955b
Author: Friedel Schon <[email protected]>
Date:   Tue, 11 Apr 2023 12:51:04 +0200

rearranging this and fixing route

Diffstat:
ALollipop/DatabaseObject.php | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ALollipop/Router.php | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MLollipop/SQLDatabase.php | 115+++++++++----------------------------------------------------------------------
DRouter.php | 57---------------------------------------------------------
Dautoloader.php | 18------------------
Mindex.php | 18++++++++----------
Drouting.php | 6------
Autils/autoloader.php | 20++++++++++++++++++++
8 files changed, 198 insertions(+), 194 deletions(-)

diff --git a/Lollipop/DatabaseObject.php b/Lollipop/DatabaseObject.php @@ -0,0 +1,96 @@ +<?php + +namespace Lollipop { + require_once "SQLDatabase.php"; + + abstract class DatabaseObject + { + protected string $table; + protected string $primary; + + protected SQLDatabase $db; + protected array $data; + protected array $changed_keys; + + function __construct(SQLDatabase $db) + { + $this->db = $db; + $this->primary = $this->get_primary(); + $this->table = $this->get_table(); + } + + abstract static function get_primary(): string; + abstract static function get_table(): string; + + public function load(string $id): bool + { + $sql = "SELECT * FROM {$this->table} WHERE {$this->primary} = ?"; + + $stmt = $this->db->conn->prepare($sql); + $stmt->bind_param("s", $id); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows == 0) { + return false; + } + + $this->data = $result->fetch_assoc(); + return true; + } + + public function save() + { + if (!$this->changed_keys) + return; + + $sql = "UPDATE {$this->table} SET "; + + $values = []; + $types = ""; + foreach ($this->changed_keys as $index => $key) { + if ($index > 0) + $sql .= ', '; + $sql .= "$key = ?"; + $values[] = $this->data[$key]; + $types .= 's'; + } + + $sql .= " WHERE $this->primary = ?"; + $values[] = $this->data[$this->primary]; + $types .= 's'; + + $stmt = $this->db->conn->prepare($sql); + $stmt->bind_param($types, ...$values); + $stmt->execute(); + + $this->changed_keys = []; + } + + public function delete() + { + $sql = "DELETE FROM {$this->table} WHERE {$this->primary} = ?"; + $stmt = $this->db->conn->prepare($sql); + $stmt->bind_param("s", $this->data[$this->primary]); + $stmt->execute(); + $this->data = []; + $this->changed_keys = []; + } + + public function __get(string $name) + { + return $this->data[$name]; + } + + public function __set(string $name, $value) + { + $this->data[$name] = $value; + $this->changed_keys[] = $name; + } + + public function getData() + { + return $this->data; + } + } +} +\ No newline at end of file diff --git a/Lollipop/Router.php b/Lollipop/Router.php @@ -0,0 +1,60 @@ +<?php + +namespace Lollipop { + class Router + { + protected array $routes = []; + protected string $path; + + protected function match(string $match, array &$route_vars): bool + { + $route_split = explode('/', $this->path); + $match_split = explode('/', $match); + + if (sizeof($route_split) != sizeof($match_split)) { + return false; + } + + foreach ($match_split as $index => $m) { + if (str_starts_with($m, ':')) { + $route_vars[substr($m, 1)] = $route_split[$index]; + } else if ($m != $route_split[$index]) { + return false; + } + } + return true; + } + + + function addRoute(string $method, string $match, callable $func) + { + $this->routes[] = array( + "method" => $method, + "match" => $match, + "func" => $func, + ); + } + + function route(string $base = null) + { + $this->path = $_SERVER["REQUEST_URI"]; + + if ($base && strpos($this->path, $base)) + $this->path = explode($base, $this->path)[1]; + + $method = $_SERVER["REQUEST_METHOD"]; + + foreach ($this->routes as $route) { + if ($route["method"] != null && $route["method"] != $method) + continue; + + $vars = []; + if ($this->match($route["match"], $vars)) + return $route["func"]($vars); + } + + echo "404 '$this->path' not found!"; + return null; + } + } +} +\ No newline at end of file diff --git a/Lollipop/SQLDatabase.php b/Lollipop/SQLDatabase.php @@ -1,114 +1,23 @@ <?php -namespace Lollipop; +namespace Lollipop { + use mysqli; -use mysqli; - -abstract class DatabaseObject -{ - protected string $table; - protected string $primary; - - protected SQLDatabase $db; - protected array $data; - protected array $changed_keys; - - function __construct(SQLDatabase $db) + class SQLDatabase { - $this->db = $db; - $this->primary = $this->get_primary(); - $this->table = $this->get_table(); - } - - abstract static function get_primary(): string; - abstract static function get_table(): string; + public mysqli $conn; - public function load(string $id): bool - { - $sql = "SELECT * FROM {$this->table} WHERE {$this->primary} = ?"; - - $stmt = $this->db->conn->prepare($sql); - $stmt->bind_param("s", $id); - $stmt->execute(); - $result = $stmt->get_result(); - - if ($result->num_rows == 0) { - return false; + function __construct(string $host, string $username, string $password, string $database = null, int $port = null) + { + $this->conn = new mysqli($host, $username, $password, $database, $port); } - $this->data = $result->fetch_assoc(); - return true; - } - - public function save() - { - if (!$this->changed_keys) - return; - - $sql = "UPDATE {$this->table} SET "; - - $values = []; - $types = ""; - foreach ($this->changed_keys as $index => $key) { - if ($index > 0) - $sql .= ', '; - $sql .= "$key = ?"; - $values[] = $this->data[$key]; - $types .= 's'; + function get(string $table_class, $name) + { + $cls = new $table_class($this); + $cls->load($name); + return $cls; } - - $sql .= " WHERE $this->primary = ?"; - $values[] = $this->data[$this->primary]; - $types .= 's'; - - $stmt = $this->db->conn->prepare($sql); - $stmt->bind_param($types, ...$values); - $stmt->execute(); - - $this->changed_keys = []; - } - - public function delete() - { - $sql = "DELETE FROM {$this->table} WHERE {$this->primary} = ?"; - $stmt = $this->db->conn->prepare($sql); - $stmt->bind_param("s", $this->data[$this->primary]); - $stmt->execute(); - $this->data = []; - $this->changed_keys = []; - } - - public function __get(string $name) - { - return $this->data[$name]; - } - - public function __set(string $name, $value) - { - $this->data[$name] = $value; - $this->changed_keys[] = $name; - } - - public function getData() - { - return $this->data; - } -} - -class SQLDatabase -{ - public mysqli $conn; - - function __construct(string $host, string $username, string $password, string $database = null, int $port = null) - { - $this->conn = new mysqli($host, $username, $password, $database, $port); - } - - function get(string $table_class, $name) - { - $cls = new $table_class($this); - $cls->load($name); - return $cls; } } ?> \ No newline at end of file diff --git a/Router.php b/Router.php @@ -1,56 +0,0 @@ -<?php - -class Router -{ - protected array $routes = []; - protected string $route; - - protected function match(string $match, array &$route_vars): bool - { - $route_split = explode('/', $this->route); - $match_split = explode('/', $match); - - if (sizeof($route_split) != sizeof($match_split)) { - return false; - } - - foreach ($match_split as $index => $m) { - if (str_starts_with($m, ':')) { - $route_vars[substr($m, 1)] = $route_split[$index]; - } else if ($m != $route_split[$index]) { - return false; - } - } - return true; - } - - - function addRoute(string $method, string $match, callable $func) - { - $this->routes[] = array( - "method" => $method, - "match" => $method, - "func" => $func, - ); - } - - function route(string $base = null) - { - $this->route = $_SERVER["REQUEST_URI"]; - - if ($base && strpos($this->route, $base)) - $this->route = explode($base, $this->route)[1]; - - $method = $_SERVER["REQUEST_METHOD"]; - - foreach ($this->routes as $route) { - if ($route["method"] != null && $route["method"] != $method) - continue; - - $vars = []; - if ($this->match($route["match"], $vars)) - return $route["func"]($vars); - } - return null; - } -} -\ No newline at end of file diff --git a/autoloader.php b/autoloader.php @@ -1,17 +0,0 @@ -<?php - spl_autoload_register(function ($class_name) { - $DIR = dirname(__FILE__); - $sr = '\\'; - $filename = $DIR . $sr .$class_name . '.php'; - if(! file_exists($filename)){ - $filename = $DIR . $sr .'classes' . $sr . $class_name . '.php'; - if(! file_exists($filename)){ - return false; - }else{ - include 'classes' . $sr . $class_name . '.php'; - } - }else{ - include $class_name . '.php'; - } - }); -?> -\ No newline at end of file diff --git a/index.php b/index.php @@ -1,13 +1,11 @@ <?php -require_once "orm.php"; +require_once "utils/autoloader.php"; -class User -{ +$router = new Lollipop\Router(); +$router->addRoute("GET", "/hello/:world", function ($vars) { + echo "hello"; + var_dump($vars); +}); -} - -$db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "lollipop"); -$db->register(User::class); - -$db->get("user"); -\ No newline at end of file +$router->route(); +\ No newline at end of file diff --git a/routing.php b/routing.php @@ -1,5 +0,0 @@ -<?php -if($_SERVER['GET']) -{ - rout -}; -\ No newline at end of file diff --git a/utils/autoloader.php b/utils/autoloader.php @@ -0,0 +1,19 @@ +<?php + +spl_autoload_register(function ($class_name) { + if (DIRECTORY_SEPARATOR != "\\") + $class_name = str_replace("\\", DIRECTORY_SEPARATOR, $class_name); + + $sr = DIRECTORY_SEPARATOR; + $filename = $class_name . '.php'; + if (!file_exists($filename)) { + $filename = 'classes' . $sr . $class_name . '.php'; + if (!file_exists($filename)) { + return false; + } else { + include 'classes' . $sr . $class_name . '.php'; + } + } else { + include $class_name . '.php'; + } +}); +\ No newline at end of file