Merge pull request #645 from skipperbent/v5-development

Version 5.2.0.0
This commit is contained in:
Simon Sessingø
2023-04-02 03:20:01 +02:00
committed by GitHub
25 changed files with 135 additions and 129 deletions
+11 -11
View File
@@ -9,37 +9,37 @@ class InputFile implements IInputItem
/** /**
* @var string * @var string
*/ */
public $index; public string $index;
/** /**
* @var string * @var string
*/ */
public $name; public string $name;
/** /**
* @var string|null * @var string|null
*/ */
public $filename; public ?string $filename = null;
/** /**
* @var int|null * @var int|null
*/ */
public $size; public ?int $size = null;
/** /**
* @var int|null * @var string|null
*/ */
public $type; public ?string $type = null;
/** /**
* @var int * @var int
*/ */
public $errors; public int $errors = 0;
/** /**
* @var string|null * @var string|null
*/ */
public $tmpName; public ?string $tmpName = null;
public function __construct(string $index) public function __construct(string $index)
{ {
@@ -74,7 +74,7 @@ class InputFile implements IInputItem
'error' => null, 'error' => null,
]; ];
return (new static($values['index'])) return (new self($values['index']))
->setSize((int)$values['size']) ->setSize((int)$values['size'])
->setError((int)$values['error']) ->setError((int)$values['error'])
->setType($values['type']) ->setType($values['type'])
@@ -104,9 +104,9 @@ class InputFile implements IInputItem
} }
/** /**
* @return string * @return int
*/ */
public function getSize(): string public function getSize(): ?int
{ {
return $this->size; return $this->size;
} }
+7 -7
View File
@@ -10,40 +10,40 @@ class InputHandler
/** /**
* @var array * @var array
*/ */
protected $get = []; protected array $get = [];
/** /**
* @var array * @var array
*/ */
protected $post = []; protected array $post = [];
/** /**
* @var array * @var array
*/ */
protected $file = []; protected array $file = [];
/** /**
* @var Request * @var Request
*/ */
protected $request; protected Request $request;
/** /**
* Original post variables * Original post variables
* @var array * @var array
*/ */
protected $originalPost = []; protected array $originalPost = [];
/** /**
* Original get/params variables * Original get/params variables
* @var array * @var array
*/ */
protected $originalParams = []; protected array $originalParams = [];
/** /**
* Get original file variables * Get original file variables
* @var array * @var array
*/ */
protected $originalFile = []; protected array $originalFile = [];
/** /**
* Input constructor. * Input constructor.
+10 -2
View File
@@ -8,10 +8,18 @@ use IteratorAggregate;
class InputItem implements ArrayAccess, IInputItem, IteratorAggregate class InputItem implements ArrayAccess, IInputItem, IteratorAggregate
{ {
public $index; public string $index;
public $name; public string $name;
/**
* @var mixed|null
*/
public $value; public $value;
/**
* @param string $index
* @param mixed $value
*/
public function __construct(string $index, $value = null) public function __construct(string $index, $value = null)
{ {
$this->index = $index; $this->index = $index;
@@ -17,18 +17,18 @@ class BaseCsrfVerifier implements IMiddleware
* For example: /admin/* * For example: /admin/*
* @var array|null * @var array|null
*/ */
protected $except; protected ?array $except = null;
/** /**
* Urls to include. Can be used to include urls from a certain path. * Urls to include. Can be used to include urls from a certain path.
* @var array|null * @var array|null
*/ */
protected $include; protected ?array $include = null;
/** /**
* @var ITokenProvider * @var ITokenProvider
*/ */
protected $tokenProvider; protected ITokenProvider $tokenProvider;
/** /**
* BaseCsrfVerifier constructor. * BaseCsrfVerifier constructor.
@@ -7,8 +7,8 @@ use Pecee\SimpleRouter\Exceptions\HttpException;
abstract class IpRestrictAccess implements IMiddleware abstract class IpRestrictAccess implements IMiddleware
{ {
protected $ipBlacklist = []; protected array $ipBlacklist = [];
protected $ipWhitelist = []; protected array $ipWhitelist = [];
protected function validate(string $ip): bool protected function validate(string $ip): bool
{ {
+19 -17
View File
@@ -29,7 +29,7 @@ class Request
* All request-types * All request-types
* @var string[] * @var string[]
*/ */
public static $requestTypes = [ public static array $requestTypes = [
self::REQUEST_TYPE_GET, self::REQUEST_TYPE_GET,
self::REQUEST_TYPE_POST, self::REQUEST_TYPE_POST,
self::REQUEST_TYPE_PUT, self::REQUEST_TYPE_PUT,
@@ -43,7 +43,7 @@ class Request
* Post request-types. * Post request-types.
* @var string[] * @var string[]
*/ */
public static $requestTypesPost = [ public static array $requestTypesPost = [
self::REQUEST_TYPE_POST, self::REQUEST_TYPE_POST,
self::REQUEST_TYPE_PUT, self::REQUEST_TYPE_PUT,
self::REQUEST_TYPE_PATCH, self::REQUEST_TYPE_PATCH,
@@ -55,65 +55,65 @@ class Request
* *
* @var array * @var array
*/ */
private $data = []; private array $data = [];
/** /**
* Server headers * Server headers
* @var array * @var array
*/ */
protected $headers = []; protected array $headers = [];
/** /**
* Request ContentType * Request ContentType
* @var string * @var string
*/ */
protected $contentType; protected string $contentType;
/** /**
* Request host * Request host
* @var string * @var string|null
*/ */
protected $host; protected ?string $host;
/** /**
* Current request url * Current request url
* @var Url * @var Url
*/ */
protected $url; protected Url $url;
/** /**
* Request method * Request method
* @var string * @var string
*/ */
protected $method; protected string $method;
/** /**
* Input handler * Input handler
* @var InputHandler * @var InputHandler
*/ */
protected $inputHandler; protected InputHandler $inputHandler;
/** /**
* Defines if request has pending rewrite * Defines if request has pending rewrite
* @var bool * @var bool
*/ */
protected $hasPendingRewrite = false; protected bool $hasPendingRewrite = false;
/** /**
* @var ILoadableRoute|null * @var ILoadableRoute|null
*/ */
protected $rewriteRoute; protected ?ILoadableRoute $rewriteRoute = null;
/** /**
* Rewrite url * Rewrite url
* @var string|null * @var string|null
*/ */
protected $rewriteUrl; protected ?string $rewriteUrl = null;
/** /**
* @var array * @var array
*/ */
protected $loadedRoutes = []; protected array $loadedRoutes = [];
/** /**
* Request constructor. * Request constructor.
@@ -224,15 +224,17 @@ class Request
*/ */
public function getIp(bool $safeMode = false): ?string public function getIp(bool $safeMode = false): ?string
{ {
$headers = ['remote-addr']; $headers = [];
if($safeMode === false) { if($safeMode === false) {
$headers = array_merge($headers, [ $headers = [
'http-cf-connecting-ip', 'http-cf-connecting-ip',
'http-client-ip', 'http-client-ip',
'http-x-forwarded-for', 'http-x-forwarded-for',
]); ];
} }
$headers[] = 'remote-addr';
return $this->getFirstHeader($headers); return $this->getFirstHeader($headers);
} }
+1 -5
View File
@@ -7,7 +7,7 @@ use Pecee\Exceptions\InvalidArgumentException;
class Response class Response
{ {
protected $request; protected Request $request;
public function __construct(Request $request) public function __construct(Request $request)
{ {
@@ -39,9 +39,6 @@ class Response
$this->httpCode($httpCode); $this->httpCode($httpCode);
} }
// Gracefully end session (avoid any changes being lost)
session_write_close();
$this->header('location: ' . $url); $this->header('location: ' . $url);
exit(0); exit(0);
} }
@@ -68,7 +65,6 @@ class Response
public function cache(string $eTag, int $lastModifiedTime = 2592000): self public function cache(string $eTag, int $lastModifiedTime = 2592000): self
{ {
$this->headers([ $this->headers([
'Cache-Control: public', 'Cache-Control: public',
sprintf('Last-Modified: %s GMT', gmdate('D, d M Y H:i:s', $lastModifiedTime)), sprintf('Last-Modified: %s GMT', gmdate('D, d M Y H:i:s', $lastModifiedTime)),
@@ -12,12 +12,12 @@ class CookieTokenProvider implements ITokenProvider
/** /**
* @var string * @var string
*/ */
protected $token; protected ?string $token = null;
/** /**
* @var int * @var int
*/ */
protected $cookieTimeoutMinutes = 120; protected int $cookieTimeoutMinutes = 120;
/** /**
* CookieTokenProvider constructor. * CookieTokenProvider constructor.
+10 -10
View File
@@ -10,53 +10,53 @@ class Url implements JsonSerializable
/** /**
* @var string|null * @var string|null
*/ */
private $originalUrl; private ?string $originalUrl = null;
/** /**
* @var string|null * @var string|null
*/ */
private $scheme; private ?string $scheme = null;
/** /**
* @var string|null * @var string|null
*/ */
private $host; private ?string $host = null;
/** /**
* @var int|null * @var int|null
*/ */
private $port; private ?int $port = null;
/** /**
* @var string|null * @var string|null
*/ */
private $username; private ?string $username = null;
/** /**
* @var string|null * @var string|null
*/ */
private $password; private ?string $password = null;
/** /**
* @var string|null * @var string|null
*/ */
private $path; private ?string $path = null;
/** /**
* Original path with no sanitization to ending slash * Original path with no sanitization to ending slash
* @var string|null * @var string|null
*/ */
private $originalPath; private ?string $originalPath = null;
/** /**
* @var array * @var array
*/ */
private $params = []; private array $params = [];
/** /**
* @var string|null * @var string|null
*/ */
private $fragment; private ?string $fragment = null;
/** /**
* Url constructor. * Url constructor.
@@ -27,7 +27,7 @@ class ClassLoader implements IClassLoader
* @param object $class * @param object $class
* @param string $method * @param string $method
* @param array $parameters * @param array $parameters
* @return object * @return mixed
*/ */
public function loadClassMethod($class, string $method, array $parameters) public function loadClassMethod($class, string $method, array $parameters)
{ {
@@ -17,7 +17,7 @@ interface IClassLoader
* @param object $class * @param object $class
* @param string $method * @param string $method
* @param array $parameters * @param array $parameters
* @return object * @return mixed
*/ */
public function loadClassMethod($class, string $method, array $parameters); public function loadClassMethod($class, string $method, array $parameters);
@@ -12,17 +12,17 @@ class EventArgument implements IEventArgument
* Event name * Event name
* @var string * @var string
*/ */
protected $eventName; protected string $eventName;
/** /**
* @var Router * @var Router
*/ */
protected $router; protected Router $router;
/** /**
* @var array * @var array
*/ */
protected $arguments = []; protected array $arguments = [];
public function __construct(string $eventName, Router $router, array $arguments = []) public function __construct(string $eventName, Router $router, array $arguments = [])
{ {
@@ -9,12 +9,12 @@ class ClassNotFoundHttpException extends NotFoundHttpException
/** /**
* @var string * @var string
*/ */
protected $class; protected string $class;
/** /**
* @var string|null * @var string|null
*/ */
protected $method; protected ?string $method = null;
public function __construct(string $class, ?string $method = null, string $message = "", int $code = 0, Throwable $previous = null) public function __construct(string $class, ?string $method = null, string $message = "", int $code = 0, Throwable $previous = null)
{ {
@@ -20,7 +20,7 @@ class CallbackExceptionHandler implements IExceptionHandler
/** /**
* @var Closure * @var Closure
*/ */
protected $callback; protected Closure $callback;
public function __construct(Closure $callback) public function __construct(Closure $callback)
{ {
@@ -13,7 +13,7 @@ class DebugEventHandler implements IEventHandler
* Debug callback * Debug callback
* @var Closure * @var Closure
*/ */
protected $callback; protected Closure $callback;
public function __construct() public function __construct()
{ {
@@ -97,7 +97,7 @@ class EventHandler implements IEventHandler
* All available events * All available events
* @var array * @var array
*/ */
public static $events = [ public static array $events = [
self::EVENT_ALL, self::EVENT_ALL,
self::EVENT_INIT, self::EVENT_INIT,
self::EVENT_LOAD, self::EVENT_LOAD,
@@ -120,7 +120,7 @@ class EventHandler implements IEventHandler
* List of all registered events * List of all registered events
* @var array * @var array
*/ */
private $registeredEvents = []; private array $registeredEvents = [];
/** /**
* Register new event * Register new event
@@ -12,17 +12,17 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
/** /**
* @var string * @var string
*/ */
protected $url; protected string $url;
/** /**
* @var string * @var string
*/ */
protected $name; protected ?string $name = null;
/** /**
* @var string|null * @var string|null
*/ */
protected $regex; protected ?string $regex = null;
/** /**
* Loads and renders middlewares-classes * Loads and renders middlewares-classes
@@ -195,7 +195,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
*/ */
public function hasName(string $name): bool public function hasName(string $name): bool
{ {
return strtolower((string)$this->name) === strtolower((string)$name); return strtolower((string)$this->name) === strtolower($name);
} }
/** /**
+15 -15
View File
@@ -18,37 +18,37 @@ abstract class Route implements IRoute
* *
* @var bool * @var bool
*/ */
protected $filterEmptyParams = true; protected bool $filterEmptyParams = true;
/** /**
* If true the last parameter of the route will include ending trail/slash. * If true the last parameter of the route will include ending trail/slash.
* @var bool * @var bool
*/ */
protected $slashParameterEnabled = false; protected bool $slashParameterEnabled = false;
/** /**
* Default regular expression used for parsing parameters. * Default regular expression used for parsing parameters.
* @var string|null * @var string|null
*/ */
protected $defaultParameterRegex; protected ?string $defaultParameterRegex = null;
protected $paramModifiers = '{}'; protected string $paramModifiers = '{}';
protected $paramOptionalSymbol = '?'; protected string $paramOptionalSymbol = '?';
protected $urlRegex = '/^%s\/?$/u'; protected string $urlRegex = '/^%s\/?$/u';
protected $group; protected ?IGroupRoute $group = null;
protected $parent; protected ?IRoute $parent = null;
/** /**
* @var string|callable|null * @var string|callable|null
*/ */
protected $callback; protected $callback;
protected $defaultNamespace; protected ?string $defaultNamespace = null;
/* Default options */ /* Default options */
protected $namespace; protected ?string $namespace = null;
protected $requestMethods = []; protected array $requestMethods = [];
protected $where = []; protected array $where = [];
protected $parameters = []; protected array $parameters = [];
protected $originalParameters = []; protected array $originalParameters = [];
protected $middlewares = []; protected array $middlewares = [];
/** /**
* Render route * Render route
@@ -6,10 +6,10 @@ use Pecee\Http\Request;
class RouteController extends LoadableRoute implements IControllerRoute class RouteController extends LoadableRoute implements IControllerRoute
{ {
protected $defaultMethod = 'index'; protected string $defaultMethod = 'index';
protected $controller; protected string $controller;
protected $method; protected ?string $method = null;
protected $names = []; protected array $names = [];
public function __construct($url, $controller) public function __construct($url, $controller)
{ {
+7 -7
View File
@@ -7,12 +7,12 @@ use Pecee\SimpleRouter\Handlers\IExceptionHandler;
class RouteGroup extends Route implements IGroupRoute class RouteGroup extends Route implements IGroupRoute
{ {
protected $urlRegex = '/^%s\/?/u'; protected string $urlRegex = '/^%s\/?/u';
protected $prefix; protected ?string $prefix = null;
protected $name; protected ?string $name = null;
protected $domains = []; protected array $domains = [];
protected $exceptionHandlers = []; protected array $exceptionHandlers = [];
protected $mergeExceptionHandlers = true; protected bool $mergeExceptionHandlers = true;
/** /**
* Method called to check if a domain matches * Method called to check if a domain matches
@@ -22,7 +22,7 @@ class RouteGroup extends Route implements IGroupRoute
*/ */
public function matchDomain(Request $request): bool public function matchDomain(Request $request): bool
{ {
if ($this->domains === null || count($this->domains) === 0) { if (count($this->domains) === 0) {
return true; return true;
} }
@@ -6,7 +6,7 @@ use Pecee\Http\Request;
class RouteResource extends LoadableRoute implements IControllerRoute class RouteResource extends LoadableRoute implements IControllerRoute
{ {
protected $urls = [ protected array $urls = [
'index' => '', 'index' => '',
'create' => 'create', 'create' => 'create',
'store' => '', 'store' => '',
@@ -16,7 +16,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
'destroy' => '', 'destroy' => '',
]; ];
protected $methodNames = [ protected array $methodNames = [
'index' => 'index', 'index' => 'index',
'create' => 'create', 'create' => 'create',
'store' => 'store', 'store' => 'store',
@@ -26,8 +26,8 @@ class RouteResource extends LoadableRoute implements IControllerRoute
'destroy' => 'destroy', 'destroy' => 'destroy',
]; ];
protected $names = []; protected array $names = [];
protected $controller; protected string $controller;
public function __construct($url, $controller) public function __construct($url, $controller)
{ {
+17 -17
View File
@@ -28,56 +28,56 @@ class Router
* Current request * Current request
* @var Request * @var Request
*/ */
protected $request; protected Request $request;
/** /**
* Defines if a route is currently being processed. * Defines if a route is currently being processed.
* @var bool * @var bool
*/ */
protected $isProcessingRoute; protected bool $isProcessingRoute;
/** /**
* Defines all data from current processing route. * Defines all data from current processing route.
* @var ILoadableRoute * @var ILoadableRoute
*/ */
protected $currentProcessingRoute; protected ILoadableRoute $currentProcessingRoute;
/** /**
* All added routes * All added routes
* @var array * @var array
*/ */
protected $routes = []; protected array $routes = [];
/** /**
* List of processed routes * List of processed routes
* @var array|ILoadableRoute[] * @var array|ILoadableRoute[]
*/ */
protected $processedRoutes = []; protected array $processedRoutes = [];
/** /**
* Stack of routes used to keep track of sub-routes added * Stack of routes used to keep track of sub-routes added
* when a route is being processed. * when a route is being processed.
* @var array * @var array
*/ */
protected $routeStack = []; protected array$routeStack = [];
/** /**
* List of added bootmanagers * List of added bootmanagers
* @var array * @var array
*/ */
protected $bootManagers = []; protected array $bootManagers = [];
/** /**
* Csrf verifier class * Csrf verifier class
* @var BaseCsrfVerifier|null * @var BaseCsrfVerifier|null
*/ */
protected $csrfVerifier; protected ?BaseCsrfVerifier $csrfVerifier;
/** /**
* Get exception handlers * Get exception handlers
* @var array * @var array
*/ */
protected $exceptionHandlers = []; protected array $exceptionHandlers = [];
/** /**
* List of loaded exception that has been loaded. * List of loaded exception that has been loaded.
@@ -85,44 +85,44 @@ class Router
* *
* @var array * @var array
*/ */
protected $loadedExceptionHandlers = []; protected array $loadedExceptionHandlers = [];
/** /**
* Enable or disabled debugging * Enable or disabled debugging
* @var bool * @var bool
*/ */
protected $debugEnabled = false; protected bool $debugEnabled = false;
/** /**
* The start time used when debugging is enabled * The start time used when debugging is enabled
* @var float * @var float
*/ */
protected $debugStartTime; protected float $debugStartTime;
/** /**
* List containing all debug messages * List containing all debug messages
* @var array * @var array
*/ */
protected $debugList = []; protected array $debugList = [];
/** /**
* Contains any registered event-handler. * Contains any registered event-handler.
* @var array * @var array
*/ */
protected $eventHandlers = []; protected array $eventHandlers = [];
/** /**
* Class loader instance * Class loader instance
* @var IClassLoader * @var IClassLoader
*/ */
protected $classLoader; protected IClassLoader $classLoader;
/** /**
* When enabled the router will render all routes that matches. * When enabled the router will render all routes that matches.
* When disabled the router will stop execution when first route is found. * When disabled the router will stop execution when first route is found.
* @var bool * @var bool
*/ */
protected $renderMultipleRoutes = false; protected bool $renderMultipleRoutes = false;
/** /**
* Router constructor. * Router constructor.
@@ -345,7 +345,7 @@ class Router
try { try {
/* Verify csrf token for request */ /* Verify csrf token for request */
$this->csrfVerifier->handle($this->request); $this->csrfVerifier->handle($this->request);
} catch(\Exception $e) { } catch(Exception $e) {
$this->handleException($e); $this->handleException($e);
} }
} }
+4 -4
View File
@@ -37,19 +37,19 @@ class SimpleRouter
* Default namespace added to all routes * Default namespace added to all routes
* @var string|null * @var string|null
*/ */
protected static $defaultNamespace; protected static ?string $defaultNamespace = null;
/** /**
* The response object * The response object
* @var Response * @var Response
*/ */
protected static $response; protected static Response $response;
/** /**
* Router instance * Router instance
* @var Router * @var Router
*/ */
protected static $router; protected static ?Router $router = null;
/** /**
* Start routing * Start routing
@@ -493,7 +493,7 @@ class SimpleRouter
* Prepends the default namespace to all new routes added. * Prepends the default namespace to all new routes added.
* *
* @param ILoadableRoute|IRoute $route * @param ILoadableRoute|IRoute $route
* @return IRoute * @return IRoute|ILoadableRoute
*/ */
public static function addDefaultNamespace(IRoute $route): IRoute public static function addDefaultNamespace(IRoute $route): IRoute
{ {
@@ -2,12 +2,12 @@
class DummyCsrfVerifier extends \Pecee\Http\Middleware\BaseCsrfVerifier { class DummyCsrfVerifier extends \Pecee\Http\Middleware\BaseCsrfVerifier {
protected $except = [ protected ?array $except = [
'/exclude-page', '/exclude-page',
'/exclude-all/*', '/exclude-all/*',
]; ];
protected $include = [ protected ?array $include = [
'/exclude-all/include-page', '/exclude-all/include-page',
]; ];
@@ -2,12 +2,12 @@
class IpRestrictMiddleware extends \Pecee\Http\Middleware\IpRestrictAccess { class IpRestrictMiddleware extends \Pecee\Http\Middleware\IpRestrictAccess {
protected $ipBlacklist = [ protected array $ipBlacklist = [
'5.5.5.5', '5.5.5.5',
'8.8.*', '8.8.*',
]; ];
protected $ipWhitelist = [ protected array $ipWhitelist = [
'8.8.2.2', '8.8.2.2',
]; ];