mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Development
- Fixed: only set default namespace on relative callbacks. - Fixed: default-namespace not being set when calling `SimpleRouter::resource`. - Minor optimisations.
This commit is contained in:
@@ -237,16 +237,23 @@ class Request
|
|||||||
{
|
{
|
||||||
$this->rewriteRoute = $route;
|
$this->rewriteRoute = $route;
|
||||||
|
|
||||||
$namespace = SimpleRouter::getDefaultNamespace();
|
$callback = $route->getCallback();
|
||||||
|
|
||||||
if ($namespace !== null) {
|
/* Only add default namespace on relative callbacks */
|
||||||
|
if($callback === null || $callback[0] !== '\\') {
|
||||||
|
|
||||||
|
$namespace = SimpleRouter::getDefaultNamespace();
|
||||||
|
|
||||||
|
if ($namespace !== null) {
|
||||||
|
|
||||||
|
if ($this->rewriteRoute->getNamespace() !== null) {
|
||||||
|
$namespace .= '\\' . $this->rewriteRoute->getNamespace();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->rewriteRoute->setDefaultNamespace($namespace);
|
||||||
|
|
||||||
if ($this->rewriteRoute->getNamespace() !== null) {
|
|
||||||
$namespace .= '\\' . $this->rewriteRoute->getNamespace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->rewriteRoute->setDefaultNamespace($namespace);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
|||||||
*/
|
*/
|
||||||
public function loadMiddleware(Request $request)
|
public function loadMiddleware(Request $request)
|
||||||
{
|
{
|
||||||
if (count($this->getMiddlewares()) > 0) {
|
$max = count($this->getMiddlewares());
|
||||||
|
|
||||||
$max = count($this->getMiddlewares());
|
if ($max > 0) {
|
||||||
|
|
||||||
for ($i = 0; $i < $max; $i++) {
|
for ($i = 0; $i < $max; $i++) {
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
|||||||
|
|
||||||
$middleware = $this->loadClass($middleware);
|
$middleware = $this->loadClass($middleware);
|
||||||
|
|
||||||
if (!($middleware instanceof IMiddleware)) {
|
if (($middleware instanceof IMiddleware) === false) {
|
||||||
throw new HttpException($middleware . ' must be instance of Middleware');
|
throw new HttpException($middleware . ' must be instance of Middleware');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +48,6 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
|||||||
|
|
||||||
public function matchRegex(Request $request, $url)
|
public function matchRegex(Request $request, $url)
|
||||||
{
|
{
|
||||||
|
|
||||||
/* Match on custom defined regular expression */
|
/* Match on custom defined regular expression */
|
||||||
|
|
||||||
if ($this->regex === null) {
|
if ($this->regex === null) {
|
||||||
|
|||||||
@@ -57,16 +57,21 @@ abstract class Route implements IRoute
|
|||||||
|
|
||||||
public function renderRoute(Request $request)
|
public function renderRoute(Request $request)
|
||||||
{
|
{
|
||||||
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
|
$callback = $this->getCallback();
|
||||||
|
|
||||||
|
if ($callback !== null && is_callable($callback)) {
|
||||||
|
|
||||||
/* When the callback is a function */
|
/* When the callback is a function */
|
||||||
call_user_func_array($this->getCallback(), $this->getParameters());
|
call_user_func_array($callback, $this->getParameters());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
/* When the callback is a method */
|
/* When the callback is a method */
|
||||||
$controller = explode('@', $this->getCallback());
|
$controller = explode('@', $callback);
|
||||||
$className = $this->getNamespace() . '\\' . $controller[0];
|
|
||||||
|
$namespace = $this->getNamespace();
|
||||||
|
|
||||||
|
$className = ($namespace !== null) ? $namespace . '\\' . $controller[0] : $controller[0];
|
||||||
|
|
||||||
$class = $this->loadClass($className);
|
$class = $this->loadClass($className);
|
||||||
$method = $controller[1];
|
$method = $controller[1];
|
||||||
|
|||||||
@@ -340,7 +340,7 @@ class Router
|
|||||||
|
|
||||||
if ($includeEmpty === false) {
|
if ($includeEmpty === false) {
|
||||||
$getParams = array_filter($getParams, function ($item) {
|
$getParams = array_filter($getParams, function ($item) {
|
||||||
return (trim($item) !== false);
|
return (trim($item) !== '');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -284,6 +284,7 @@ class SimpleRouter
|
|||||||
public static function resource($url, $controller, array $settings = null)
|
public static function resource($url, $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);
|
||||||
@@ -360,13 +361,23 @@ class SimpleRouter
|
|||||||
protected static function addDefaultNamespace(IRoute $route)
|
protected static function addDefaultNamespace(IRoute $route)
|
||||||
{
|
{
|
||||||
if (static::$defaultNamespace !== null) {
|
if (static::$defaultNamespace !== null) {
|
||||||
$namespace = static::$defaultNamespace;
|
|
||||||
|
|
||||||
if ($route->getNamespace() !== null) {
|
$callback = $route->getCallback();
|
||||||
$namespace .= '\\' . $route->getNamespace();
|
|
||||||
|
/* Only add default namespace on relative callbacks */
|
||||||
|
if($callback === null || $callback[0] !== '\\') {
|
||||||
|
|
||||||
|
$namespace = static::$defaultNamespace;
|
||||||
|
|
||||||
|
$currentNamespace = $route->getNamespace();
|
||||||
|
|
||||||
|
if ($currentNamespace !== null) {
|
||||||
|
$namespace .= '\\' . $currentNamespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
$route->setDefaultNamespace($namespace);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$route->setDefaultNamespace($namespace);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
|
|||||||
Reference in New Issue
Block a user