mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Merge pull request #507 from skipperbent/feature-default-namespace
[FEATURE] Default-namespace optimisations #446
This commit is contained in:
@@ -319,7 +319,7 @@ class InputHandler
|
|||||||
public function all(array $filter = []): array
|
public function all(array $filter = []): array
|
||||||
{
|
{
|
||||||
$output = $this->originalParams + $this->originalPost + $this->originalFile;
|
$output = $this->originalParams + $this->originalPost + $this->originalFile;
|
||||||
$output = (\count($filter) > 0) ? array_intersect_key($output, array_flip($filter)) : $output;
|
$output = (\count($filter) > 0) ? \array_intersect_key($output, \array_flip($filter)) : $output;
|
||||||
|
|
||||||
foreach ($filter as $filterKey) {
|
foreach ($filter as $filterKey) {
|
||||||
if (array_key_exists($filterKey, $output) === false) {
|
if (array_key_exists($filterKey, $output) === false) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Pecee\SimpleRouter\ClassLoader;
|
namespace Pecee\SimpleRouter\ClassLoader;
|
||||||
|
|
||||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
|
||||||
|
|
||||||
class ClassLoader implements IClassLoader
|
class ClassLoader implements IClassLoader
|
||||||
{
|
{
|
||||||
@@ -15,8 +15,8 @@ class ClassLoader implements IClassLoader
|
|||||||
*/
|
*/
|
||||||
public function loadClass(string $class)
|
public function loadClass(string $class)
|
||||||
{
|
{
|
||||||
if (\class_exists($class) === false) {
|
if (class_exists($class) === false) {
|
||||||
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404);
|
throw new ClassNotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404, null, $class);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new $class();
|
return new $class();
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pecee\SimpleRouter\Exceptions;
|
||||||
|
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class ClassNotFoundHttpException extends NotFoundHttpException
|
||||||
|
{
|
||||||
|
protected $class;
|
||||||
|
protected $method;
|
||||||
|
|
||||||
|
public function __construct($message = "", $code = 0, Throwable $previous = null, string $class, ?string $method = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
|
||||||
|
$this->class = $class;
|
||||||
|
$this->method = $method;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get class name
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getClass(): string
|
||||||
|
{
|
||||||
|
return $this->class;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get method
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getMethod(): ?string
|
||||||
|
{
|
||||||
|
return $this->method;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ namespace Pecee\SimpleRouter\Route;
|
|||||||
|
|
||||||
use Pecee\Http\Middleware\IMiddleware;
|
use Pecee\Http\Middleware\IMiddleware;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
|
||||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
use Pecee\SimpleRouter\Router;
|
use Pecee\SimpleRouter\Router;
|
||||||
|
|
||||||
@@ -94,7 +95,7 @@ abstract class Route implements IRoute
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (method_exists($class, $method) === false) {
|
if (method_exists($class, $method) === false) {
|
||||||
throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404);
|
throw new ClassNotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404, null, $className, $method);
|
||||||
}
|
}
|
||||||
|
|
||||||
$router->debug('Executing callback');
|
$router->debug('Executing callback');
|
||||||
|
|||||||
@@ -58,6 +58,11 @@ class SimpleRouter
|
|||||||
*/
|
*/
|
||||||
public static function start(): void
|
public static function start(): void
|
||||||
{
|
{
|
||||||
|
// Set default namespaces
|
||||||
|
foreach (static::router()->getRoutes() as $route) {
|
||||||
|
static::addDefaultNamespace($route);
|
||||||
|
}
|
||||||
|
|
||||||
echo static::router()->start();
|
echo static::router()->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,8 +311,8 @@ class SimpleRouter
|
|||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|array|\Closure $callback
|
* @param string|array|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @see SimpleRouter::form
|
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @see SimpleRouter::form
|
||||||
*/
|
*/
|
||||||
public static function basic(string $url, $callback, array $settings = null): IRoute
|
public static function basic(string $url, $callback, array $settings = null): IRoute
|
||||||
{
|
{
|
||||||
@@ -321,14 +326,14 @@ class SimpleRouter
|
|||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|array|\Closure $callback
|
* @param string|array|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @see SimpleRouter::form
|
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
|
* @see SimpleRouter::form
|
||||||
*/
|
*/
|
||||||
public static function form(string $url, $callback, array $settings = null): IRoute
|
public static function form(string $url, $callback, array $settings = null): IRoute
|
||||||
{
|
{
|
||||||
return static::match([
|
return static::match([
|
||||||
Request::REQUEST_TYPE_GET,
|
Request::REQUEST_TYPE_GET,
|
||||||
Request::REQUEST_TYPE_POST
|
Request::REQUEST_TYPE_POST,
|
||||||
], $url, $callback, $settings);
|
], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,7 +350,6 @@ class SimpleRouter
|
|||||||
{
|
{
|
||||||
$route = new RouteUrl($url, $callback);
|
$route = new RouteUrl($url, $callback);
|
||||||
$route->setRequestMethods($requestMethods);
|
$route->setRequestMethods($requestMethods);
|
||||||
$route = static::addDefaultNamespace($route);
|
|
||||||
|
|
||||||
if ($settings !== null) {
|
if ($settings !== null) {
|
||||||
$route->setSettings($settings);
|
$route->setSettings($settings);
|
||||||
@@ -365,7 +369,6 @@ class SimpleRouter
|
|||||||
public static function all(string $url, $callback, array $settings = null)
|
public static function all(string $url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
$route = new RouteUrl($url, $callback);
|
$route = new RouteUrl($url, $callback);
|
||||||
$route = static::addDefaultNamespace($route);
|
|
||||||
|
|
||||||
if ($settings !== null) {
|
if ($settings !== null) {
|
||||||
$route->setSettings($settings);
|
$route->setSettings($settings);
|
||||||
@@ -385,7 +388,6 @@ class SimpleRouter
|
|||||||
public static function controller(string $url, string $controller, array $settings = null)
|
public static function controller(string $url, string $controller, array $settings = null)
|
||||||
{
|
{
|
||||||
$route = new RouteController($url, $controller);
|
$route = new RouteController($url, $controller);
|
||||||
$route = static::addDefaultNamespace($route);
|
|
||||||
|
|
||||||
if ($settings !== null) {
|
if ($settings !== null) {
|
||||||
$route->setSettings($settings);
|
$route->setSettings($settings);
|
||||||
@@ -405,7 +407,6 @@ class SimpleRouter
|
|||||||
public static function resource(string $url, string $controller, array $settings = null)
|
public static function resource(string $url, string $controller, array $settings = null)
|
||||||
{
|
{
|
||||||
$route = new RouteResource($url, $controller);
|
$route = new RouteResource($url, $controller);
|
||||||
$route = static::addDefaultNamespace($route);
|
|
||||||
|
|
||||||
if ($settings !== null) {
|
if ($settings !== null) {
|
||||||
$route->setSettings($settings);
|
$route->setSettings($settings);
|
||||||
@@ -510,22 +511,19 @@ class SimpleRouter
|
|||||||
{
|
{
|
||||||
if (static::$defaultNamespace !== null) {
|
if (static::$defaultNamespace !== null) {
|
||||||
|
|
||||||
$callback = $route->getCallback();
|
$ns = static::$defaultNamespace;
|
||||||
|
$namespace = $route->getNamespace();
|
||||||
|
|
||||||
/* Only add default namespace on relative callbacks */
|
if ($namespace !== null) {
|
||||||
if ($callback === null || (\is_string($callback) === true && $callback[0] !== '\\')) {
|
// Don't overwrite namespaces that starts with \
|
||||||
|
if ($namespace[0] !== '\\') {
|
||||||
$namespace = static::$defaultNamespace;
|
$ns .= '\\' . $namespace;
|
||||||
|
} else {
|
||||||
$currentNamespace = $route->getNamespace();
|
$ns = $namespace;
|
||||||
|
|
||||||
if ($currentNamespace !== null) {
|
|
||||||
$namespace .= '\\' . $currentNamespace;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$route->setDefaultNamespace($namespace);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$route->setNamespace($ns);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
|
|||||||
{
|
{
|
||||||
TestRouter::get('/', 'DummyController@method1');
|
TestRouter::get('/', 'DummyController@method1');
|
||||||
TestRouter::get('/page/{id?}', 'DummyController@method1');
|
TestRouter::get('/page/{id?}', 'DummyController@method1');
|
||||||
TestRouter::get('/test-output', function() {
|
TestRouter::get('/test-output', function () {
|
||||||
return 'return value';
|
return 'return value';
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -175,7 +175,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
|
|||||||
{
|
{
|
||||||
TestRouter::request()->setHost('google.com');
|
TestRouter::request()->setHost('google.com');
|
||||||
|
|
||||||
TestRouter::get('/admin/', function() {
|
TestRouter::get('/admin/', function () {
|
||||||
return 'match';
|
return 'match';
|
||||||
})->setMatch('/^\/admin\/?(.*)/i');
|
})->setMatch('/^\/admin\/?(.*)/i');
|
||||||
|
|
||||||
@@ -183,4 +183,54 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertEquals('match', $output);
|
$this->assertEquals('match', $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDefaultNamespace()
|
||||||
|
{
|
||||||
|
TestRouter::setDefaultNamespace('\\Default\\Namespace');
|
||||||
|
|
||||||
|
TestRouter::get('/', 'DummyController@method1', ['as' => 'home']);
|
||||||
|
|
||||||
|
TestRouter::group([
|
||||||
|
'namespace' => 'Appended\Namespace',
|
||||||
|
'prefix' => '/horses',
|
||||||
|
], function () {
|
||||||
|
|
||||||
|
TestRouter::get('/', 'DummyController@method1');
|
||||||
|
|
||||||
|
TestRouter::group([
|
||||||
|
'namespace' => '\\New\\Namespace',
|
||||||
|
'prefix' => '/race',
|
||||||
|
], function () {
|
||||||
|
|
||||||
|
TestRouter::get('/', 'DummyController@method1');
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test appended namespace
|
||||||
|
|
||||||
|
$class = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
TestRouter::debugNoReset('/horses/');
|
||||||
|
} catch (\Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException $e) {
|
||||||
|
$class = $e->getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals('\\Default\\Namespace\\Appended\Namespace\\DummyController', $class);
|
||||||
|
|
||||||
|
// Test overwritten namespace
|
||||||
|
|
||||||
|
$class = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
TestRouter::debugNoReset('/horses/race');
|
||||||
|
} catch (\Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException $e) {
|
||||||
|
$class = $e->getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals('\\New\\Namespace\\DummyController', $class);
|
||||||
|
|
||||||
|
TestRouter::router()->reset();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user