Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
Using Request/Response objects; added a new Templating class; using a…
Browse files Browse the repository at this point in the history
… temporary exception handling in the skeleton project's index.php file
  • Loading branch information
o-alquimista committed Oct 11, 2020
1 parent 2687353 commit ef48052
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 262 deletions.
29 changes: 17 additions & 12 deletions src/Fragments/Bundle/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,32 @@

namespace Fragments\Bundle\Controller;

use Fragments\Component\Request;
use Fragments\Component\Http\Request;
use Fragments\Component\Http\Response;
use Fragments\Component\Http\RedirectResponse;
use Fragments\Component\CsrfTokenManager;
use Fragments\Component\Feedback;
use Fragments\Component\TemplateHelper;
use Fragments\Component\Templating;
use Fragments\Component\Routing\Router;

abstract class AbstractController
{
protected function renderTemplate(string $path, array $variables = []) {
$templateHelper = new TemplateHelper;
$templateHelper->render($path, $variables);
/**
* Conveniently render a template from the controllers.
*/
protected function render(string $template, array $variables = []): Response
{
$templating = new Templating();

return $templating->render($template, $variables);
}

protected function isFormSubmitted(): bool
protected function redirectToRoute(string $routeId, $parameters = []): RedirectResponse
{
$request = new Request;

if ($request->requestMethod() == "POST") {
return true;
}
$router = new Router();
$url = $router->generateUrl($routeId, $parameters);

return false;
return new RedirectResponse($url);
}

protected function isCsrfTokenValid(string $targetId, string $token): bool
Expand Down
67 changes: 0 additions & 67 deletions src/Fragments/Component/Bootstrap.php

This file was deleted.

39 changes: 39 additions & 0 deletions src/Fragments/Component/Http/RedirectResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* Copyright 2019-2020 Douglas Silva (0x9fd287d56ec107ac)
*
* This file is part of Fragments.
*
* Fragments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fragments. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Fragments\Component\Http;

/**
* An object-oriented representation of the HTTP response.
*/
class RedirectResponse extends Response
{
public function __construct(string $url)
{
$headers = [];
$content = "";

$content = "Redirecting to {$url}";
$headers['Location'] = $url;

parent::__construct($content, 302, $headers);
}
}
41 changes: 41 additions & 0 deletions src/Fragments/Component/Http/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Copyright 2019-2020 Douglas Silva (0x9fd287d56ec107ac)
*
* This file is part of Fragments.
*
* Fragments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fragments. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Fragments\Component\Http;

/**
* An object-oriented representation of the HTTP request.
*/
class Request
{
public $post;

public $get;

public $server;

public function __construct()
{
$this->post = $_POST;
$this->get = $_GET;
$this->server = $_SERVER;
}
}
61 changes: 61 additions & 0 deletions src/Fragments/Component/Http/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* Copyright 2019-2020 Douglas Silva (0x9fd287d56ec107ac)
*
* This file is part of Fragments.
*
* Fragments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fragments. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Fragments\Component\Http;

/**
* An object-oriented representation of the HTTP response.
*/
class Response
{
public $content;

public $statusCode;

public $headers;

public function __construct(string $content = '', int $statusCode = 200, array $headers = [])
{
$this->content = $content;
$this->statusCode = $statusCode;
$this->headers = $headers;
}

public function send()
{
$this->sendHeaders();
$this->sendContent();
}

private function sendHeaders()
{
foreach ($this->headers as $name => $value) {
header("{$name}: {$value}", true, $this->statusCode);
}

http_response_code($this->statusCode);
}

private function sendContent()
{
echo $this->content;
}
}
81 changes: 0 additions & 81 deletions src/Fragments/Component/Request.php

This file was deleted.

20 changes: 10 additions & 10 deletions src/Fragments/Component/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

use Fragments\Component\Routing\Model\Route;
use Fragments\Component\Routing\Parser\XMLParser;
use Fragments\Component\Request;
use Fragments\Component\Http\Request;
use Fragments\Component\Http\Response;
use Fragments\Bundle\Exception\NotFoundHttpException;
use Fragments\Bundle\Exception\MethodNotAllowedHttpException;
use Fragments\Bundle\Exception\ServerErrorHttpException;
Expand All @@ -32,30 +33,29 @@ class Router
{
private $parser;

private $request;

public function __construct()
{
$this->parser = new XMLParser;
$this->request = new Request;
}

public function start()
public function run(Request $request): Response
{
$route = $this->getMatchingRoute();
$route = $this->getMatchingRoute($request);

$controller = $route->getController();
$action = $route->getAction();
$parameters = $route->getParameters();

$controller = new $controller;
$controller->{$action}(...$parameters);
$response = $controller->{$action}(...$parameters);

return $response;
}

private function getMatchingRoute(): Route
private function getMatchingRoute(Request $request): Route
{
$routes = $this->parser->getRoutes();
$uri = $this->request->getURI();
$uri = $request->server['REQUEST_URI'];

// Ignore GET parameters in the URI, if present
if (strpos($uri, '?') !== false) {
Expand Down Expand Up @@ -101,7 +101,7 @@ private function getMatchingRoute(): Route
}
}

if (!in_array($this->request->requestMethod(), $route->getMethods())) {
if (!in_array($request->server['REQUEST_METHOD'], $route->getMethods())) {
throw new MethodNotAllowedHttpException;
}

Expand Down
Loading

0 comments on commit ef48052

Please sign in to comment.