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

Commit

Permalink
Redesigned the Router component
Browse files Browse the repository at this point in the history
  • Loading branch information
o-alquimista committed Mar 21, 2020
1 parent db84d0a commit b98bd80
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 373 deletions.
18 changes: 16 additions & 2 deletions src/Fragments/Component/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace Fragments\Component;

use Fragments\Component\Routing\Router;

/**
* Server Request Utility
*
Expand Down Expand Up @@ -53,11 +55,23 @@ public function get(string $value)
}

/**
* Redirects the web browser to the specified URI.
* Redirects the client to the specified path.
*/
public function redirect(string $path)
public function redirect(string $routeId)
{
header('Location: ' . $path, true, 301);
exit;
}

/**
* Generates a path from a route ID and redirects to it.
*/
public function redirectToRoute(string $routeId)
{
$router = new Router;
$path = $router->generateUrl($routeId);

header('Location: ' . $path, true, 301);
exit;
}
}
112 changes: 112 additions & 0 deletions src/Fragments/Component/Routing/Model/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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\Routing\Model;

class Route
{
/**
* The route identifier.
*/
private $id;

/**
* The associated URL.
*/
private $path;

/**
* The fully qualified class name of the controller.
*/
private $controller;

/**
* The class method to be executed.
*/
private $action;

/**
* The request methods supported.
*/
private $methods;

public function getId(): string
{
return $this->id;
}

public function setId(string $id)
{
$this->id = $id;

return $this;
}

public function getPath(): string
{
return $this->path;
}

public function setPath(string $path)
{
$this->path = $path;

return $this;
}

public function getController(): string
{
return $this->controller;
}

public function setController(string $controller)
{
$this->controller = $controller;

return $this;
}

public function getAction(): string
{
return $this->action;
}

public function setAction(string $action)
{
$this->action = $action;

return $this;
}

public function getMethods(): array
{
return $this->methods;
}

public function setMethods(string $methods)
{
// Store them in an array
$methods = explode('|', $methods);
$this->methods = $methods;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,27 @@
<?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\Routing;

use Fragments\Component\Request;

/**
* Request context.
*
* Stores information about the HTTP request.
*/
class RequestContext
{
public $uri;

public $requestMethod;

public function __construct()
{
$request = new Request;

$uri = $request->getURI();

// Remove trailing slash from path, except for the root
if ($uri !== '/') {
$uri = rtrim($uri, '/');
}

$this->uri = $uri;
$this->requestMethod = $request->requestMethod();
}
}
<?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\Routing\Parser;

interface ParserInterface
{
public function getRoutes(): array;
}
51 changes: 51 additions & 0 deletions src/Fragments/Component/Routing/Parser/XMLParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Routing\Parser;

use Fragments\Bundle\Exception\ServerErrorHttpException;
use Fragments\Component\Routing\Model\Route;

class XMLParser implements ParserInterface
{
public function getRoutes(): array
{
if (!file_exists('../config/routes.xml')) {
throw new ServerErrorHttpException('The route definition file is missing.');
}

$file = simplexml_load_file('../config/routes.xml');
$routes = [];

foreach ($file as $entry) {
$route = new Route;
$route->setId($entry->id);
$route->setPath($entry->path);
$route->setMethods($entry->methods);
$route->setController($entry->controller);
$route->setAction($entry->action);

$routes[] = $route;
}

return $routes;
}
}
Loading

0 comments on commit b98bd80

Please sign in to comment.