mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Default-namespace changes.
- Added new ClassNotFoundHttpException thrown when class is not found. - ClassNotFoundHttpException is now thrown when class/method is not found (backwards compatible). - Added unit-tests for default-namespace tests (rewrite + append cases).
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace Pecee\SimpleRouter\ClassLoader;
|
namespace Pecee\SimpleRouter\ClassLoader;
|
||||||
|
|
||||||
use DI\Container;
|
use DI\Container;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
|
||||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
|
|
||||||
class ClassLoader implements IClassLoader
|
class ClassLoader implements IClassLoader
|
||||||
@@ -28,7 +29,7 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->useDependencyInjection === true) {
|
if ($this->useDependencyInjection === true) {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
@@ -95,7 +96,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');
|
||||||
|
|||||||
@@ -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');
|
||||||
|
|
||||||
@@ -185,24 +185,52 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
public function testDefaultNamespace()
|
public function testDefaultNamespace()
|
||||||
{
|
{
|
||||||
TestRouter::setDefaultNamespace('\\TestRoutersss');
|
TestRouter::setDefaultNamespace('\\Default\\Namespace');
|
||||||
|
|
||||||
TestRouter::get('/', 'DummyController@method1', ['as' => 'home']);
|
TestRouter::get('/', 'DummyController@method1', ['as' => 'home']);
|
||||||
TestRouter::get('/about', 'DummyController@about');
|
|
||||||
|
|
||||||
TestRouter::group([
|
TestRouter::group([
|
||||||
'prefix' => '/horses',
|
'namespace' => 'Appended\Namespace',
|
||||||
], function() {
|
'prefix' => '/horses',
|
||||||
|
], function () {
|
||||||
|
|
||||||
TestRouter::get('/about', 'DummyController@about');
|
TestRouter::get('/', 'DummyController@method1');
|
||||||
|
|
||||||
|
TestRouter::group([
|
||||||
|
'namespace' => '\\New\\Namespace',
|
||||||
|
'prefix' => '/race',
|
||||||
|
], function () {
|
||||||
|
|
||||||
|
TestRouter::get('/', 'DummyController@method1');
|
||||||
|
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Test appended namespace
|
||||||
|
|
||||||
TestRouter::debugNoReset('/horses/about');
|
$class = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
TestRouter::debugNoReset('/horses/');
|
||||||
|
} catch (\Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException $e) {
|
||||||
|
$class = $e->getClass();
|
||||||
|
}
|
||||||
|
|
||||||
$routes = TestRouter::router()->getRoutes();
|
$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