This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db84d0a
commit b98bd80
Showing
8 changed files
with
301 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
78 changes: 27 additions & 51 deletions
78
...ents/Component/Routing/RequestContext.php → ...ponent/Routing/Parser/ParserInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.