mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 12:42:16 +00:00
[FEATURE] Changed behavior for default-namespace after issue #446
- Router::setDefaultNamespace() no longer has to be set in the beginning of routes.php. - Default namespace are now set once the router is started (Router::start()). - [WIP] Added unit-tests for custom-namespaces.
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) {
|
||||||
|
|||||||
@@ -59,6 +59,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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,8 +312,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
|
||||||
{
|
{
|
||||||
@@ -322,14 +327,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,7 +351,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);
|
||||||
@@ -366,7 +370,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);
|
||||||
@@ -386,7 +389,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);
|
||||||
@@ -406,7 +408,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);
|
||||||
@@ -511,22 +512,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;
|
||||||
|
|||||||
@@ -183,4 +183,26 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertEquals('match', $output);
|
$this->assertEquals('match', $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDefaultNamespace()
|
||||||
|
{
|
||||||
|
TestRouter::setDefaultNamespace('\\TestRoutersss');
|
||||||
|
|
||||||
|
TestRouter::get('/', 'DummyController@method1', ['as' => 'home']);
|
||||||
|
TestRouter::get('/about', 'DummyController@about');
|
||||||
|
|
||||||
|
TestRouter::group([
|
||||||
|
'prefix' => '/horses',
|
||||||
|
], function() {
|
||||||
|
|
||||||
|
TestRouter::get('/about', 'DummyController@about');
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
TestRouter::debugNoReset('/horses/about');
|
||||||
|
|
||||||
|
|
||||||
|
$routes = TestRouter::router()->getRoutes();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user