Reverted Router changes.
This commit is contained in:
parent
405724ec3f
commit
d9f7cd5037
8 changed files with 61 additions and 33 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.2309.112008
|
0.2309.112031
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// HttpFx.php
|
// HttpFx.php
|
||||||
// Created: 2022-02-15
|
// Created: 2022-02-15
|
||||||
// Updated: 2023-09-11
|
// Updated: 2023-09-07
|
||||||
|
|
||||||
namespace Index\Http;
|
namespace Index\Http;
|
||||||
|
|
||||||
|
@ -207,6 +207,16 @@ class HttpFx implements IRouter {
|
||||||
$this->router->use($path, $handler);
|
$this->router->use($path, $handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges another router with this one with possibility of changing its root.
|
||||||
|
*
|
||||||
|
* @param string $path Base path to use.
|
||||||
|
* @param Router $router Router object to inherit from.
|
||||||
|
*/
|
||||||
|
public function merge(string $path, Router $router): void {
|
||||||
|
$this->router->merge($path, $router);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new route.
|
* Adds a new route.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
<?php
|
<?php
|
||||||
// HttpRequest.php
|
// HttpRequest.php
|
||||||
// Created: 2022-02-08
|
// Created: 2022-02-08
|
||||||
// Updated: 2023-09-11
|
// Updated: 2023-08-16
|
||||||
|
|
||||||
namespace Index\Http;
|
namespace Index\Http;
|
||||||
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use RuntimeException;
|
|
||||||
use Index\Version;
|
use Index\Version;
|
||||||
use Index\MediaType;
|
use Index\MediaType;
|
||||||
use Index\Http\Content\IHttpContent;
|
use Index\Http\Content\IHttpContent;
|
||||||
|
@ -81,22 +80,7 @@ class HttpRequest extends HttpMessage {
|
||||||
$build = new HttpRequestBuilder;
|
$build = new HttpRequestBuilder;
|
||||||
$build->setHttpVersion(new Version(...array_map('intval', explode('.', substr($_SERVER['SERVER_PROTOCOL'], 5)))));
|
$build->setHttpVersion(new Version(...array_map('intval', explode('.', substr($_SERVER['SERVER_PROTOCOL'], 5)))));
|
||||||
$build->setMethod($_SERVER['REQUEST_METHOD']);
|
$build->setMethod($_SERVER['REQUEST_METHOD']);
|
||||||
|
$build->setPath('/' . trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
|
||||||
// this currently doesn't "properly" support the scenario where a full url is specified in the http request
|
|
||||||
$path = $_SERVER['REQUEST_URI'];
|
|
||||||
$pathQueryOffset = strpos($path, '?');
|
|
||||||
if($pathQueryOffset !== false)
|
|
||||||
$path = substr($path, 0, $pathQueryOffset);
|
|
||||||
else {
|
|
||||||
$pathHashOffset = strpos($path, '#');
|
|
||||||
if($pathHashOffset !== false)
|
|
||||||
$path = substr($path, 0, $pathHashOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!str_starts_with($path, '/'))
|
|
||||||
$path = '/' . $path;
|
|
||||||
|
|
||||||
$build->setPath($path);
|
|
||||||
$build->setParams($_GET);
|
$build->setParams($_GET);
|
||||||
$build->setCookies($_COOKIE);
|
$build->setCookies($_COOKIE);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// IRouter.php
|
// IRouter.php
|
||||||
// Created: 2023-01-06
|
// Created: 2023-01-06
|
||||||
// Updated: 2023-09-11
|
// Updated: 2023-09-06
|
||||||
|
|
||||||
namespace Index\Routing;
|
namespace Index\Routing;
|
||||||
|
|
||||||
|
@ -14,6 +14,14 @@ interface IRouter {
|
||||||
*/
|
*/
|
||||||
public function use(string $path, callable $handler): void;
|
public function use(string $path, callable $handler): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges another router with this one with possibility of changing its root.
|
||||||
|
*
|
||||||
|
* @param string $path Base path to use.
|
||||||
|
* @param Router $router Router object to inherit from.
|
||||||
|
*/
|
||||||
|
public function merge(string $path, Router $router): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new route.
|
* Adds a new route.
|
||||||
*
|
*
|
||||||
|
|
|
@ -16,12 +16,7 @@ class Route {
|
||||||
private ?string $method;
|
private ?string $method;
|
||||||
private string $path;
|
private string $path;
|
||||||
|
|
||||||
/**
|
/** @internal */
|
||||||
* Creates a Route attribute.
|
|
||||||
*
|
|
||||||
* @param string $pathOrMethod Method name if this is registering a route, path if this is registering middleware.
|
|
||||||
* @param ?string $path Path if this registering a route, null if this is registering middleware.
|
|
||||||
*/
|
|
||||||
public function __construct(string $pathOrMethod, ?string $path = null) {
|
public function __construct(string $pathOrMethod, ?string $path = null) {
|
||||||
if($path === null) {
|
if($path === null) {
|
||||||
$this->method = null;
|
$this->method = null;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// RouteInfo.php
|
// RouteInfo.php
|
||||||
// Created: 2022-01-20
|
// Created: 2022-01-20
|
||||||
// Updated: 2023-09-11
|
// Updated: 2022-02-27
|
||||||
|
|
||||||
namespace Index\Routing;
|
namespace Index\Routing;
|
||||||
|
|
||||||
|
@ -33,6 +33,22 @@ class RouteInfo {
|
||||||
return $child;
|
return $child;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public function mergeRoute(string $path, RouteInfo $route): void {
|
||||||
|
if($path === '') {
|
||||||
|
$this->children = array_merge($this->children, $route->children);
|
||||||
|
$this->methods = array_merge($this->methods, $route->methods);
|
||||||
|
$this->middlewares = array_merge($this->middlewares, $route->middlewares);
|
||||||
|
if($this->dynamicChild === null)
|
||||||
|
$this->dynamicChild = $route->dynamicChild;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getChild($path, $next)->mergeRoute($next, $route);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// Router.php
|
// Router.php
|
||||||
// Created: 2022-01-18
|
// Created: 2022-01-18
|
||||||
// Updated: 2023-09-11
|
// Updated: 2023-09-06
|
||||||
|
|
||||||
namespace Index\Routing;
|
namespace Index\Routing;
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ class Router implements IRouter {
|
||||||
if($method === 'head')
|
if($method === 'head')
|
||||||
$method = 'get';
|
$method = 'get';
|
||||||
|
|
||||||
return $this->route->resolve($method, $path, $args);
|
return $this->route->resolve($method, trim($path, '/'), $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,7 +43,17 @@ class Router implements IRouter {
|
||||||
* @param callable $handler Middleware function.
|
* @param callable $handler Middleware function.
|
||||||
*/
|
*/
|
||||||
public function use(string $path, callable $handler): void {
|
public function use(string $path, callable $handler): void {
|
||||||
$this->route->addMiddleware($path, $handler);
|
$this->route->addMiddleware(trim($path, '/'), $handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges another router with this one with possibility of changing its root.
|
||||||
|
*
|
||||||
|
* @param string $path Base path to use.
|
||||||
|
* @param Router $router Router object to inherit from.
|
||||||
|
*/
|
||||||
|
public function merge(string $path, Router $router): void {
|
||||||
|
$this->route->mergeRoute(trim($path, '/'), $router->route);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,7 +67,7 @@ class Router implements IRouter {
|
||||||
if(empty($method))
|
if(empty($method))
|
||||||
throw new InvalidArgumentException('$method may not be empty.');
|
throw new InvalidArgumentException('$method may not be empty.');
|
||||||
|
|
||||||
$this->route->addMethod($method, $path, $handler);
|
$this->route->addMethod($method, trim($path, '/'), $handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// RouterTest.php
|
// RouterTest.php
|
||||||
// Created: 2022-01-20
|
// Created: 2022-01-20
|
||||||
// Updated: 2023-09-11
|
// Updated: 2023-09-08
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
@ -89,6 +89,11 @@ final class RouterTest extends TestCase {
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertEquals(['meow', 'rules page'], $router2->resolve('GET', '/rules')->runAll());
|
$this->assertEquals(['meow', 'rules page'], $router2->resolve('GET', '/rules')->runAll());
|
||||||
|
|
||||||
|
$router1->merge('/info', $router2);
|
||||||
|
|
||||||
|
$this->assertEquals(['warioware', 'meow', 'rules page'], $router1->resolve('GET', '/info/rules')->runAll());
|
||||||
|
|
||||||
$this->assertEquals(['meow', 'numeric test'], $router2->resolve('GET', '/25252')->runAll());
|
$this->assertEquals(['meow', 'numeric test'], $router2->resolve('GET', '/25252')->runAll());
|
||||||
|
|
||||||
$router3 = new Router;
|
$router3 = new Router;
|
||||||
|
|
Loading…
Reference in a new issue