mirror of
https://github.com/Aviortheking/DeltaCMS.git
synced 2025-04-22 19:02:12 +00:00
Added Routes Handlers
This commit is contained in:
parent
3fe397dacd
commit
12b6bf71cc
53
src/DeltaCMS/Route/AbstractRoute.php
Normal file
53
src/DeltaCMS/Route/AbstractRoute.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace DeltaCMS\Route;
|
||||
|
||||
abstract class AbstractRoute implements RouteInterface
|
||||
{
|
||||
protected $name;
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
protected $path;
|
||||
public function getPath(): string
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function setPath(string $path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
public function processPath(array $vars): string
|
||||
{
|
||||
return "WIP";
|
||||
}
|
||||
|
||||
public function listOptions(): array
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
protected $options = array();
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $option => $value) {
|
||||
if (in_array($option, $this->listOptions())) {
|
||||
$this->options[$option] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
24
src/DeltaCMS/Route/ControllerRoute.php
Normal file
24
src/DeltaCMS/Route/ControllerRoute.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace DeltaCMS\Route;
|
||||
|
||||
class ControllerRoute extends AbstractRoute
|
||||
{
|
||||
|
||||
public function listOptions(): array
|
||||
{
|
||||
return array(
|
||||
'controller',
|
||||
'function',
|
||||
'args'
|
||||
);
|
||||
}
|
||||
|
||||
public function render(array $args = array()): string
|
||||
{
|
||||
$controller = new $this->options["controller"]();
|
||||
$function = $this->options["function"];
|
||||
$controller->setUrlArguments($args);
|
||||
return $controller->$function();
|
||||
}
|
||||
}
|
28
src/DeltaCMS/Route/FileRoute.php
Normal file
28
src/DeltaCMS/Route/FileRoute.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace DeltaCMS\Route;
|
||||
|
||||
use DeltaCMS\DeltaCMS;
|
||||
|
||||
class FileRoute extends AbstractRoute
|
||||
{
|
||||
public function listOptions(): array
|
||||
{
|
||||
return array(
|
||||
'file',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param array $args
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render(array $args = array()): string
|
||||
{
|
||||
$string = file_get_contents($this->options["file"]);
|
||||
return $string !== false ? $string : "";
|
||||
}
|
||||
}
|
29
src/DeltaCMS/Route/RouteInterface.php
Normal file
29
src/DeltaCMS/Route/RouteInterface.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace DeltaCMS\Route;
|
||||
|
||||
interface RouteInterface
|
||||
{
|
||||
|
||||
public function getName(): string;
|
||||
public function setName(string $name);
|
||||
|
||||
public function getPath(): string;
|
||||
public function setPath(string $path);
|
||||
|
||||
/**
|
||||
* Process a path with variables
|
||||
*
|
||||
* @param array $vars path vars
|
||||
*
|
||||
* @return string the final path
|
||||
*/
|
||||
public function processPath(array $vars): string;
|
||||
|
||||
public function listOptions(): array;
|
||||
|
||||
public function getOptions(): array;
|
||||
public function setOptions(array $options);
|
||||
|
||||
public function render(array $args = array()): string;
|
||||
}
|
145
src/DeltaCMS/Route/Router.php
Normal file
145
src/DeltaCMS/Route/Router.php
Normal file
@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace DeltaCMS\Route;
|
||||
|
||||
use DeltaCMS\Route\RouteInterface;
|
||||
use DeltaCMS\DeltaCMS;
|
||||
|
||||
class Router
|
||||
{
|
||||
private $cache;
|
||||
private $cacheName = "routes";
|
||||
private $ttl = 9999999;
|
||||
|
||||
/*
|
||||
Elements:
|
||||
c = Controller
|
||||
fu = Function
|
||||
o = Options
|
||||
fi = file
|
||||
*/
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->cache = DeltaCMS::getInstance()->getCache();
|
||||
$this->cache->set($this->cacheName, array(), $this->ttl);
|
||||
}
|
||||
|
||||
public function getRoutes(): array
|
||||
{
|
||||
return $this->cache->get($this->cacheName, array());
|
||||
}
|
||||
|
||||
public function setRoute(RouteInterface $route)
|
||||
{
|
||||
$this->cache->set(
|
||||
$this->cacheName,
|
||||
array_merge(
|
||||
$this->cache->get($this->cacheName),
|
||||
array(
|
||||
$route->getName() => $route
|
||||
)
|
||||
),
|
||||
$this->ttl
|
||||
);
|
||||
}
|
||||
|
||||
public function renderRouteByUri(string $uri)
|
||||
{
|
||||
$contains = strstr($uri, "?", true);
|
||||
if ($contains !== false) {
|
||||
$uri = $contains;
|
||||
}
|
||||
foreach ($this->getRoutes() as /*$routeName =>*/ $route) {
|
||||
// dump($routeName);
|
||||
$slug = $route->getPath();
|
||||
$options = $route->getOptions();
|
||||
$res = $this->isSlugEqualToUri($slug, $uri, $options);
|
||||
if ($res !== false) {
|
||||
// dump($route);
|
||||
return $route->render($res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a Slug containing `{var}` is equal to an URI
|
||||
*
|
||||
* @param string $slug
|
||||
* @param string $uri
|
||||
* @param array $options
|
||||
*
|
||||
* @return boolean|array
|
||||
*/
|
||||
public function isSlugEqualToUri(string $slug, string $uri, array $options)
|
||||
{
|
||||
$uri = trim($uri, "\/");
|
||||
$slug = trim($slug, "\/");
|
||||
$splittedSlug = explode("{", $slug);
|
||||
|
||||
if (count($splittedSlug) === 1) {
|
||||
return $uri === $slug ? array() : false;
|
||||
}
|
||||
|
||||
return $this->processSlug($uri, $splittedSlug, $options);
|
||||
}
|
||||
|
||||
private function processSlug($uri, $splittedSlug, $options)
|
||||
{
|
||||
$precedentContent = "";
|
||||
$urlVars = array();
|
||||
foreach ($splittedSlug as $value) {
|
||||
// dump($value, $uri);
|
||||
if (startsWith($uri, $value)) {
|
||||
$precedentContent .= $value;
|
||||
continue;
|
||||
}
|
||||
$splittedValue = explode("}", $value);
|
||||
if (count($splittedValue) == 1) {
|
||||
return false;
|
||||
}
|
||||
$key = $splittedValue[0];
|
||||
$afterContent = $splittedValue[1];
|
||||
|
||||
$regex = isset($options["args"][$key]["regex"]) ? $options["args"][$key]["regex"] : null;
|
||||
|
||||
$val = $this->getVal($precedentContent, $uri, $afterContent);
|
||||
if ($val !== false && isset($regex) && preg_match("/" . $regex . "/", $val)) {
|
||||
$urlVars[$key] = $val;
|
||||
$precedentContent .= $val . $afterContent;
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return $urlVars;
|
||||
}
|
||||
|
||||
public function getVal(string $precedentContent, string $uri, string $afterContent)
|
||||
{
|
||||
if (strpos($uri, $precedentContent) !== false &&
|
||||
($afterContent === '' || strpos($uri, $afterContent) !== false)
|
||||
) {
|
||||
$precedentContent = "/" . str_replace("/", "\/", $precedentContent) . "/";
|
||||
// $afterContent = "/" . str_replace("/", "\/", $afterContent) . "/";
|
||||
$content = preg_filter($precedentContent, "", $uri, 1);
|
||||
if ($afterContent === '') {
|
||||
return $content;
|
||||
}
|
||||
$arr = explode($afterContent, $content);
|
||||
if ($arr !== false && isset($arr[0])) {
|
||||
return $arr[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
paths examples
|
||||
- /test
|
||||
- /test/{test}
|
||||
- /test/test-{test}
|
||||
- /test-{test1}-{test2}
|
||||
|
||||
not compatible right now
|
||||
- /test{test1}{test2}
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user