mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
Merge pull request #524 from skipperbent/v4-cleanup
[CLEANUP] Cleaned up code
This commit is contained in:
@@ -291,7 +291,7 @@ class Request
|
||||
* Will try to find first header from list of headers.
|
||||
*
|
||||
* @param array $headers
|
||||
* @param null $defaultValue
|
||||
* @param mixed|null $defaultValue
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getFirstHeader(array $headers, $defaultValue = null)
|
||||
@@ -534,7 +534,7 @@ class Request
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __isset($name)
|
||||
public function __isset($name): bool
|
||||
{
|
||||
return array_key_exists($name, $this->data) === true;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class ClassLoader implements IClassLoader
|
||||
public function loadClass(string $class)
|
||||
{
|
||||
if (class_exists($class) === false) {
|
||||
throw new ClassNotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404, null, $class);
|
||||
throw new ClassNotFoundHttpException($class, null, sprintf('Class "%s" does not exist', $class), 404, null);
|
||||
}
|
||||
|
||||
return new $class();
|
||||
@@ -34,4 +34,4 @@ class ClassLoader implements IClassLoader
|
||||
return \call_user_func_array($closure, $parameters);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,7 @@ class EventArgument implements IEventArgument
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset(string $name)
|
||||
public function __isset(string $name): bool
|
||||
{
|
||||
return array_key_exists($name, $this->arguments);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ class ClassNotFoundHttpException extends NotFoundHttpException
|
||||
protected $class;
|
||||
protected $method;
|
||||
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null, string $class, ?string $method = null)
|
||||
public function __construct(string $class, ?string $method = null, $message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ interface IGroupRoute extends IRoute
|
||||
* @param array $handlers
|
||||
* @return static
|
||||
*/
|
||||
public function setExceptionHandlers(array $handlers);
|
||||
public function setExceptionHandlers(array $handlers): self;
|
||||
|
||||
/**
|
||||
* Get exception-handlers for group
|
||||
|
||||
@@ -82,6 +82,6 @@ interface ILoadableRoute extends IRoute
|
||||
* @param string $regex
|
||||
* @return static
|
||||
*/
|
||||
public function setMatch($regex): self;
|
||||
public function setMatch(string $regex): self;
|
||||
|
||||
}
|
||||
@@ -10,11 +10,11 @@ interface IRoute
|
||||
/**
|
||||
* Method called to check if a domain matches
|
||||
*
|
||||
* @param string $route
|
||||
* @param string $url
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function matchRoute($route, Request $request): bool;
|
||||
public function matchRoute(string $url, Request $request): bool;
|
||||
|
||||
/**
|
||||
* Called when route is matched.
|
||||
@@ -129,7 +129,7 @@ interface IRoute
|
||||
* @param string $namespace
|
||||
* @return static
|
||||
*/
|
||||
public function setDefaultNamespace($namespace): IRoute;
|
||||
public function setDefaultNamespace(string $namespace): IRoute;
|
||||
|
||||
/**
|
||||
* Get default namespace
|
||||
@@ -196,7 +196,7 @@ interface IRoute
|
||||
* @param string $middleware
|
||||
* @return static
|
||||
*/
|
||||
public function addMiddleware($middleware): self;
|
||||
public function addMiddleware(string $middleware): self;
|
||||
|
||||
/**
|
||||
* Set middlewares array
|
||||
|
||||
@@ -183,7 +183,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
* @param string $regex
|
||||
* @return static
|
||||
*/
|
||||
public function setMatch($regex): ILoadableRoute
|
||||
public function setMatch(string $regex): ILoadableRoute
|
||||
{
|
||||
$this->regex = $regex;
|
||||
|
||||
@@ -229,15 +229,15 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param array $settings
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setSettings(array $values, bool $merge = false): IRoute
|
||||
public function setSettings(array $settings, bool $merge = false): IRoute
|
||||
{
|
||||
if (isset($values['as']) === true) {
|
||||
if (isset($settings['as']) === true) {
|
||||
|
||||
$name = $values['as'];
|
||||
$name = $settings['as'];
|
||||
|
||||
if ($this->name !== null && $merge !== false) {
|
||||
$name .= '.' . $this->name;
|
||||
@@ -246,11 +246,11 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
$this->setName($name);
|
||||
}
|
||||
|
||||
if (isset($values['prefix']) === true) {
|
||||
$this->prependUrl($values['prefix']);
|
||||
if (isset($settings['prefix']) === true) {
|
||||
$this->prependUrl($settings['prefix']);
|
||||
}
|
||||
|
||||
return parent::setSettings($values, $merge);
|
||||
return parent::setSettings($settings, $merge);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -95,7 +95,7 @@ abstract class Route implements IRoute
|
||||
}
|
||||
|
||||
if (method_exists($class, $method) === false) {
|
||||
throw new ClassNotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404, null, $className, $method);
|
||||
throw new ClassNotFoundHttpException($className, $method, sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404, null);
|
||||
}
|
||||
|
||||
$router->debug('Executing callback');
|
||||
@@ -327,7 +327,7 @@ abstract class Route implements IRoute
|
||||
* @param string $namespace
|
||||
* @return static
|
||||
*/
|
||||
public function setDefaultNamespace($namespace): IRoute
|
||||
public function setDefaultNamespace(string $namespace): IRoute
|
||||
{
|
||||
$this->defaultNamespace = $namespace;
|
||||
|
||||
@@ -382,35 +382,35 @@ abstract class Route implements IRoute
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param array $settings
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setSettings(array $values, bool $merge = false): IRoute
|
||||
public function setSettings(array $settings, bool $merge = false): IRoute
|
||||
{
|
||||
if ($this->namespace === null && isset($values['namespace']) === true) {
|
||||
$this->setNamespace($values['namespace']);
|
||||
if ($this->namespace === null && isset($settings['namespace']) === true) {
|
||||
$this->setNamespace($settings['namespace']);
|
||||
}
|
||||
|
||||
if (isset($values['method']) === true) {
|
||||
$this->setRequestMethods(array_merge($this->requestMethods, (array)$values['method']));
|
||||
if (isset($settings['method']) === true) {
|
||||
$this->setRequestMethods(array_merge($this->requestMethods, (array)$settings['method']));
|
||||
}
|
||||
|
||||
if (isset($values['where']) === true) {
|
||||
$this->setWhere(array_merge($this->where, (array)$values['where']));
|
||||
if (isset($settings['where']) === true) {
|
||||
$this->setWhere(array_merge($this->where, (array)$settings['where']));
|
||||
}
|
||||
|
||||
if (isset($values['parameters']) === true) {
|
||||
$this->setParameters(array_merge($this->parameters, (array)$values['parameters']));
|
||||
if (isset($settings['parameters']) === true) {
|
||||
$this->setParameters(array_merge($this->parameters, (array)$settings['parameters']));
|
||||
}
|
||||
|
||||
// Push middleware if multiple
|
||||
if (isset($values['middleware']) === true) {
|
||||
$this->setMiddlewares(array_merge((array)$values['middleware'], $this->middlewares));
|
||||
if (isset($settings['middleware']) === true) {
|
||||
$this->setMiddlewares(array_merge((array)$settings['middleware'], $this->middlewares));
|
||||
}
|
||||
|
||||
if (isset($values['defaultParameterRegex']) === true) {
|
||||
$this->setDefaultParameterRegex($values['defaultParameterRegex']);
|
||||
if (isset($settings['defaultParameterRegex']) === true) {
|
||||
$this->setDefaultParameterRegex($settings['defaultParameterRegex']);
|
||||
}
|
||||
|
||||
return $this;
|
||||
@@ -493,11 +493,11 @@ abstract class Route implements IRoute
|
||||
/**
|
||||
* Add middleware class-name
|
||||
*
|
||||
* @param IMiddleware|string $middleware
|
||||
* @param string $middleware
|
||||
* @return static
|
||||
* @deprecated This method is deprecated and will be removed in the near future.
|
||||
*/
|
||||
public function setMiddleware($middleware)
|
||||
public function setMiddleware(string $middleware): self
|
||||
{
|
||||
$this->middlewares[] = $middleware;
|
||||
|
||||
@@ -507,10 +507,10 @@ abstract class Route implements IRoute
|
||||
/**
|
||||
* Add middleware class-name
|
||||
*
|
||||
* @param IMiddleware|string $middleware
|
||||
* @param string $middleware
|
||||
* @return static
|
||||
*/
|
||||
public function addMiddleware($middleware): IRoute
|
||||
public function addMiddleware(string $middleware): IRoute
|
||||
{
|
||||
$this->middlewares[] = $middleware;
|
||||
|
||||
@@ -545,7 +545,7 @@ abstract class Route implements IRoute
|
||||
* @param string $regex
|
||||
* @return static
|
||||
*/
|
||||
public function setDefaultParameterRegex($regex)
|
||||
public function setDefaultParameterRegex(string $regex): self
|
||||
{
|
||||
$this->defaultParameterRegex = $regex;
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
|
||||
return '/' . trim($url, '/') . '/';
|
||||
}
|
||||
|
||||
public function matchRoute($url, Request $request): bool
|
||||
public function matchRoute(string $url, Request $request): bool
|
||||
{
|
||||
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
|
||||
return false;
|
||||
@@ -167,17 +167,17 @@ class RouteController extends LoadableRoute implements IControllerRoute
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param array $settings
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setSettings(array $values, bool $merge = false): IRoute
|
||||
public function setSettings(array $settings, bool $merge = false): IRoute
|
||||
{
|
||||
if (isset($values['names']) === true) {
|
||||
$this->names = $values['names'];
|
||||
if (isset($settings['names']) === true) {
|
||||
$this->names = $settings['names'];
|
||||
}
|
||||
|
||||
return parent::setSettings($values, $merge);
|
||||
return parent::setSettings($settings, $merge);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function matchRoute($url, Request $request): bool
|
||||
public function matchRoute(string $url, Request $request): bool
|
||||
{
|
||||
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
|
||||
return false;
|
||||
@@ -154,28 +154,28 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param array $settings
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setSettings(array $values, bool $merge = false): IRoute
|
||||
public function setSettings(array $settings, bool $merge = false): IRoute
|
||||
{
|
||||
|
||||
if (isset($values['prefix']) === true) {
|
||||
$this->setPrefix($values['prefix'] . $this->prefix);
|
||||
if (isset($settings['prefix']) === true) {
|
||||
$this->setPrefix($settings['prefix'] . $this->prefix);
|
||||
}
|
||||
|
||||
if ($merge === false && isset($values['exceptionHandler']) === true) {
|
||||
$this->setExceptionHandlers((array)$values['exceptionHandler']);
|
||||
if ($merge === false && isset($settings['exceptionHandler']) === true) {
|
||||
$this->setExceptionHandlers((array)$settings['exceptionHandler']);
|
||||
}
|
||||
|
||||
if ($merge === false && isset($values['domain']) === true) {
|
||||
$this->setDomains((array)$values['domain']);
|
||||
if ($merge === false && isset($settings['domain']) === true) {
|
||||
$this->setDomains((array)$settings['domain']);
|
||||
}
|
||||
|
||||
if (isset($values['as']) === true) {
|
||||
if (isset($settings['as']) === true) {
|
||||
|
||||
$name = $values['as'];
|
||||
$name = $settings['as'];
|
||||
|
||||
if ($this->name !== null && $merge !== false) {
|
||||
$name .= '.' . $this->name;
|
||||
@@ -184,7 +184,7 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
return parent::setSettings($values, $merge);
|
||||
return parent::setSettings($settings, $merge);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@ class RoutePartialGroup extends RouteGroup implements IPartialGroupRoute
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function matchRoute($url, Request $request): bool
|
||||
public function matchRoute(string $url, Request $request): bool
|
||||
{
|
||||
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
|
||||
return false;
|
||||
|
||||
@@ -83,7 +83,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
return true;
|
||||
}
|
||||
|
||||
public function matchRoute($url, Request $request): bool
|
||||
public function matchRoute(string $url, Request $request): bool
|
||||
{
|
||||
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
|
||||
return false;
|
||||
@@ -210,21 +210,21 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param array $settings
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setSettings(array $values, bool $merge = false): IRoute
|
||||
public function setSettings(array $settings, bool $merge = false): IRoute
|
||||
{
|
||||
if (isset($values['names']) === true) {
|
||||
$this->names = $values['names'];
|
||||
if (isset($settings['names']) === true) {
|
||||
$this->names = $settings['names'];
|
||||
}
|
||||
|
||||
if (isset($values['methods']) === true) {
|
||||
$this->methodNames = $values['methods'];
|
||||
if (isset($settings['methods']) === true) {
|
||||
$this->methodNames = $settings['methods'];
|
||||
}
|
||||
|
||||
return parent::setSettings($values, $merge);
|
||||
return parent::setSettings($settings, $merge);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@ class RouteUrl extends LoadableRoute
|
||||
$this->setCallback($callback);
|
||||
}
|
||||
|
||||
public function matchRoute($url, Request $request): bool
|
||||
public function matchRoute(string $url, Request $request): bool
|
||||
{
|
||||
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
|
||||
return false;
|
||||
|
||||
@@ -461,7 +461,7 @@ class Router
|
||||
* @throws HttpException
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function handleRouteRewrite($key, string $url): ?string
|
||||
protected function handleRouteRewrite(string $key, string $url): ?string
|
||||
{
|
||||
/* If the request has changed */
|
||||
if ($this->request->hasPendingRewrite() === false) {
|
||||
@@ -874,7 +874,7 @@ class Router
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
*/
|
||||
protected function fireEvents($name, array $arguments = []): void
|
||||
protected function fireEvents(string $name, array $arguments = []): void
|
||||
{
|
||||
if (\count($this->eventHandlers) === 0) {
|
||||
return;
|
||||
|
||||
@@ -169,7 +169,7 @@ class SimpleRouter
|
||||
* @param int $httpCode
|
||||
* @return IRoute
|
||||
*/
|
||||
public static function redirect($where, $to, $httpCode = 301): IRoute
|
||||
public static function redirect(string $where, string $to, int $httpCode = 301): IRoute
|
||||
{
|
||||
return static::get($where, function () use ($to, $httpCode) {
|
||||
static::response()->redirect($to, $httpCode);
|
||||
|
||||
Reference in New Issue
Block a user