Add request handler

This commit is contained in:
Augusto Gunsch 2022-07-27 20:26:50 +02:00
parent 6d40f9c2fc
commit fffd25c454
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
6 changed files with 113 additions and 7 deletions

View File

@ -4,8 +4,6 @@ const loadItems = () => {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
console.log(this.responseText);
const products = JSON.parse(this.responseText);
const boxes = products.map(product =>
@ -23,7 +21,7 @@ const loadItems = () => {
productsList.innerHTML = boxes.join('\n');
}
xhttp.open('GET', 'src/View/Product', true);
xhttp.open('GET', 'products', true);
xhttp.send();
}
loadItems();
@ -39,7 +37,7 @@ const deleteSelected = () => {
loadItems();
}
xhttp.open('DELETE', 'src/View/Product', true);
xhttp.open('DELETE', 'products', true);
xhttp.send();
}

View File

@ -1,4 +1,17 @@
<?php
require 'autoload.php';
echo json_encode(ProductList\Model\Product::selectAll());
use ProductList\Http\Request;
use ProductList\Http\RequestHandler;
use ProductList\Http\Route;
$request = new Request($_SERVER);
$handler = new RequestHandler($request);
$handler->registerRoutes([
new Route('GET', 'products', ['ProductList\View\Product', 'listAll'])
]);
$handler->setIndex('index.html');
$handler->handle();

24
src/Http/Request.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace ProductList\Http;
class Request
{
private $method;
private $uri;
public function getMethod()
{
return $this->method;
}
public function getUri()
{
return $this->uri;
}
public function __construct(array $params)
{
$this->uri = basename($params['REQUEST_URI']);
$this->method = $params['REQUEST_METHOD'];
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace ProductList\Http;
class RequestHandler
{
private $index;
private $request;
private $routes;
public function __construct(Request $request)
{
$this->request = $request;
}
public function setIndex($index)
{
$this->index = $index;
}
public function registerRoutes(array $routes)
{
$this->routes = $routes;
}
public function handle()
{
foreach ($this->routes as $route) {
if ($route->matches($this->request)) {
$route->execute($this->request);
return;
}
}
readfile($this->index);
}
}

26
src/Http/Route.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace ProductList\Http;
class Route
{
private $method;
private $uri;
private $view;
public function __construct(string $method, string $uri, array $view)
{
$this->method = $method;
$this->uri = $uri;
$this->view = $view;
}
public function matches(Request $request)
{
return $this->method === $request->getMethod() && $this->uri === $request->getUri();
}
public function execute(Request $request)
{
call_user_func($this->view, $request);
}
}

View File

@ -1,4 +1,13 @@
<?php
require '../../Autoload.php';
namespace ProductList\View;
echo json_encode(ProductList\Model\Product::selectAll());
use ProductList\Http\Request;
use ProductList\Model\Product as ProductModel;
class Product
{
public static function listAll(Request $request)
{
echo json_encode(ProductModel::selectAll());
}
}