Merge branch 'v4-development' into fix-partial-group

This commit is contained in:
Simon Sessingø
2021-03-21 07:57:01 +01:00
committed by GitHub
9 changed files with 68 additions and 34 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ interface IRoute
/**
* Set callback
*
* @param string $callback
* @param string|array|\Closure $callback
* @return static
*/
public function setCallback($callback): self;
+15 -14
View File
@@ -95,26 +95,22 @@ abstract class Route implements IRoute
$router->debug('Executing callback');
/* When the callback is a function */
return $router->getClassLoader()->loadClosure($callback, $parameters);
}
/* When the callback is a class + method */
$controller = explode('@', $callback);
$controller = $this->getClass();
$method = $this->getMethod();
$namespace = $this->getNamespace();
$className = ($namespace !== null && $controller[0][0] !== '\\') ? $namespace . '\\' . $controller[0] : $controller[0];
$className = ($namespace !== null && $controller[0] !== '\\') ? $namespace . '\\' . $controller : $controller;
$router->debug('Loading class %s', $className);
$class = $router->getClassLoader()->loadClass($className);
if (\count($controller) === 1) {
if ($method === null) {
$controller[1] = '__invoke';
}
$method = $controller[1];
if (method_exists($class, $method) === false) {
throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404);
}
@@ -271,7 +267,7 @@ abstract class Route implements IRoute
/**
* Set callback
*
* @param string $callback
* @param string|array\Closure $callback
* @return static
*/
public function setCallback($callback): IRoute
@@ -291,6 +287,10 @@ abstract class Route implements IRoute
public function getMethod(): ?string
{
if(\is_array($this->callback) === true && \count($this->callback) > 1) {
return $this->callback[1];
}
if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback);
@@ -302,9 +302,12 @@ abstract class Route implements IRoute
public function getClass(): ?string
{
if(\is_array($this->callback) === true && \count($this->callback) > 0) {
return $this->callback[0];
}
if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback);
return $tmp[0];
}
@@ -313,15 +316,13 @@ abstract class Route implements IRoute
public function setMethod(string $method): IRoute
{
$this->callback = sprintf('%s@%s', $this->getClass(), $method);
$this->callback = [$this->getClass(), $method];
return $this;
}
public function setClass(string $class): IRoute
{
$this->callback = sprintf('%s@%s', $class, $this->getMethod());
$this->callback = [$class, $this->getMethod()];
return $this;
}
@@ -110,7 +110,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
$this->parameters = \array_slice($path, 1);
// Set callback
$this->setCallback($this->controller . '@' . $this->method);
$this->setCallback([$this->controller, $this->method]);
return true;
}
@@ -78,7 +78,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
protected function call($method): bool
{
$this->setCallback($this->controller . '@' . $method);
$this->setCallback([$this->controller, $method]);
return true;
}
+12 -13
View File
@@ -12,7 +12,6 @@ namespace Pecee\SimpleRouter;
use DI\Container;
use Pecee\Exceptions\InvalidArgumentException;
use Pecee\Http\Exceptions\MalformedUrlException;
use Pecee\Http\Middleware\BaseCsrfVerifier;
use Pecee\Http\Request;
use Pecee\Http\Response;
@@ -178,7 +177,7 @@ class SimpleRouter
* Route the given url to your callback on GET request method.
*
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
*
* @return RouteUrl
@@ -192,7 +191,7 @@ class SimpleRouter
* Route the given url to your callback on POST request method.
*
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @return RouteUrl
*/
@@ -205,7 +204,7 @@ class SimpleRouter
* Route the given url to your callback on PUT request method.
*
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @return RouteUrl
*/
@@ -218,7 +217,7 @@ class SimpleRouter
* Route the given url to your callback on PATCH request method.
*
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @return RouteUrl
*/
@@ -231,7 +230,7 @@ class SimpleRouter
* Route the given url to your callback on OPTIONS request method.
*
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @return RouteUrl
*/
@@ -244,7 +243,7 @@ class SimpleRouter
* Route the given url to your callback on DELETE request method.
*
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @return RouteUrl
*/
@@ -307,7 +306,7 @@ class SimpleRouter
* Alias for the form method
*
* @param string $url
* @param callable $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @see SimpleRouter::form
* @return RouteUrl
@@ -322,7 +321,7 @@ class SimpleRouter
* Route the given url to your callback on POST and GET request method.
*
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @see SimpleRouter::form
* @return RouteUrl
@@ -337,7 +336,7 @@ class SimpleRouter
*
* @param array $requestMethods
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @return RouteUrl|IRoute
*/
@@ -358,7 +357,7 @@ class SimpleRouter
* This type will route the given url to your callback and allow any type of request method
*
* @param string $url
* @param string|\Closure $callback
* @param string|array|\Closure $callback
* @param array|null $settings
* @return RouteUrl|IRoute
*/
@@ -382,7 +381,7 @@ class SimpleRouter
* @param array|null $settings
* @return RouteController|IRoute
*/
public static function controller(string $url, $controller, array $settings = null)
public static function controller(string $url, string $controller, array $settings = null)
{
$route = new RouteController($url, $controller);
$route = static::addDefaultNamespace($route);
@@ -402,7 +401,7 @@ class SimpleRouter
* @param array|null $settings
* @return RouteResource|IRoute
*/
public static function resource(string $url, $controller, array $settings = null)
public static function resource(string $url, string $controller, array $settings = null)
{
$route = new RouteResource($url, $controller);
$route = static::addDefaultNamespace($route);