This commit is contained in:
Halbe Bruno
2025-12-05 19:40:39 -03:00
commit f37bc712e6
4312 changed files with 359196 additions and 0 deletions

51
app/Core/Model.php Normal file
View File

@@ -0,0 +1,51 @@
<?php
namespace App\Core;
use App\Config\Database;
use PDO;
abstract class Model
{
protected $conn;
protected $table;
public function __construct()
{
$this->conn = Database::getInstance()->getConnection();
}
public function findAll()
{
$stmt = $this->conn->prepare("SELECT * FROM {$this->table}");
$stmt->execute();
return $stmt->fetchAll();
}
public function find($id)
{
$stmt = $this->conn->prepare("SELECT * FROM {$this->table} WHERE id = :id");
$stmt->execute(['id' => $id]);
return $stmt->fetch();
}
public function where($column, $value)
{
$stmt = $this->conn->prepare("SELECT * FROM {$this->table} WHERE {$column} = :value");
$stmt->execute(['value' => $value]);
return $stmt->fetchAll();
}
public function first($column, $value)
{
$stmt = $this->conn->prepare("SELECT * FROM {$this->table} WHERE {$column} = :value LIMIT 1");
$stmt->execute(['value' => $value]);
return $stmt->fetch();
}
public function delete($id)
{
$stmt = $this->conn->prepare("DELETE FROM {$this->table} WHERE id = :id");
return $stmt->execute(['id' => $id]);
}
}

84
app/Core/Router.php Normal file
View File

@@ -0,0 +1,84 @@
<?php
namespace App\Core;
class Router
{
private $routes = [];
public function get($path, $handler)
{
$this->add('GET', $path, $handler);
}
public function post($path, $handler)
{
$this->add('POST', $path, $handler);
}
public function put($path, $handler)
{
$this->add('PUT', $path, $handler);
}
public function delete($path, $handler)
{
$this->add('DELETE', $path, $handler);
}
private function add($method, $path, $handler)
{
$this->routes[] = [
'method' => $method,
'path' => $path,
'handler' => $handler,
'middleware' => []
];
}
public function addMiddleware($middleware)
{
// Add middleware to the last added route
if (!empty($this->routes)) {
$this->routes[count($this->routes) - 1]['middleware'][] = $middleware;
}
}
public function dispatch($method, $uri)
{
$uri = parse_url($uri, PHP_URL_PATH);
foreach ($this->routes as $route) {
if ($route['method'] !== $method)
continue;
$pattern = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '(?P<\1>[^/]+)', $route['path']);
$pattern = '#^' . $pattern . '$#';
if (preg_match($pattern, $uri, $matches)) {
// Filter named matches
$params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
// Run Middlewares
foreach ($route['middleware'] as $middleware) {
$middlewareInstance = new $middleware();
if (!$middlewareInstance->handle()) {
return; // Middleware blocked request
}
}
$handler = $route['handler'];
if (is_array($handler)) {
$controller = new $handler[0]();
$method = $handler[1];
return call_user_func_array([$controller, $method], $params);
}
return call_user_func_array($handler, $params);
}
}
http_response_code(404);
echo "404 Not Found";
}
}