Development

- Better php7 support.
- Added easier way to debug router.
- Improvements and bugfixes.
- Updated documentation.
This commit is contained in:
Simon Sessingø
2018-03-26 23:43:27 +02:00
parent f23d569757
commit 085f98cf08
28 changed files with 847 additions and 233 deletions
+3 -3
View File
@@ -7,15 +7,15 @@ interface IInputItem
public function getIndex(): string;
public function setIndex($index): self;
public function setIndex(string $index): self;
public function getName(): string;
public function setName($name): self;
public function setName(string $name): self;
public function getValue(): string;
public function setValue($value): self;
public function setValue(string $value): self;
public function __toString();
+7 -7
View File
@@ -14,7 +14,7 @@ class InputFile implements IInputItem
public $errors;
public $tmpName;
public function __construct($index)
public function __construct(string $index)
{
$this->index = $index;
@@ -48,7 +48,7 @@ class InputFile implements IInputItem
];
return (new static($values['index']))
->setSize($values['size'])
->setSize((int)$values['size'])
->setError($values['error'])
->setType($values['type'])
->setTmpName($values['tmp_name'])
@@ -69,7 +69,7 @@ class InputFile implements IInputItem
* @param string $index
* @return static
*/
public function setIndex($index): IInputItem
public function setIndex(string $index): IInputItem
{
$this->index = $index;
@@ -89,7 +89,7 @@ class InputFile implements IInputItem
* @param int $size
* @return static
*/
public function setSize($size): IInputItem
public function setSize(int $size): IInputItem
{
$this->size = $size;
@@ -118,7 +118,7 @@ class InputFile implements IInputItem
* @param string $type
* @return static
*/
public function setType($type): IInputItem
public function setType(string $type): IInputItem
{
$this->type = $type;
@@ -152,7 +152,7 @@ class InputFile implements IInputItem
* @param string $name
* @return static
*/
public function setName($name): IInputItem
public function setName(string $name): IInputItem
{
$this->name = $name;
@@ -270,7 +270,7 @@ class InputFile implements IInputItem
* @param string $value
* @return static
*/
public function setValue($value): IInputItem
public function setValue(string $value): IInputItem
{
$this->filename = $value;
+13 -13
View File
@@ -42,7 +42,7 @@ class InputHandler
* Parse input values
*
*/
public function parseInputs() : void
public function parseInputs(): void
{
/* Parse get requests */
if (\count($_GET) !== 0) {
@@ -69,7 +69,7 @@ class InputHandler
/**
* @return array
*/
public function parseFiles() : array
public function parseFiles(): array
{
$list = [];
@@ -80,7 +80,7 @@ class InputHandler
$values['index'] = $key;
try {
$list[$key] = InputFile::createFromArray($values + $value);
} catch(InvalidArgumentException $e ){
} catch (InvalidArgumentException $e) {
}
continue;
@@ -101,7 +101,7 @@ class InputHandler
return $list;
}
protected function rearrangeFiles(array $values, &$index, $original) : array
protected function rearrangeFiles(array $values, &$index, $original): array
{
$originalIndex = $index[0];
@@ -132,7 +132,7 @@ class InputHandler
$output[$key] = $file;
continue;
} catch(InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
}
}
@@ -152,7 +152,7 @@ class InputHandler
return $output;
}
protected function handleGetPost(array $array) : array
protected function handleGetPost(array $array): array
{
$list = [];
@@ -179,7 +179,7 @@ class InputHandler
* @param string|null $defaultValue
* @return InputItem|string
*/
public function findPost($index, $defaultValue = null)
public function findPost(string $index, ?string $defaultValue = null)
{
return $this->post[$index] ?? $defaultValue;
}
@@ -191,7 +191,7 @@ class InputHandler
* @param string|null $defaultValue
* @return InputFile|string
*/
public function findFile($index, $defaultValue = null)
public function findFile(string $index, ?string $defaultValue = null)
{
return $this->file[$index] ?? $defaultValue;
}
@@ -203,7 +203,7 @@ class InputHandler
* @param string|null $defaultValue
* @return InputItem|string
*/
public function findGet($index, $defaultValue = null)
public function findGet(string $index, ?string $defaultValue = null)
{
return $this->get[$index] ?? $defaultValue;
}
@@ -216,7 +216,7 @@ class InputHandler
* @param array|string|null $methods
* @return IInputItem|string
*/
public function getObject($index, $defaultValue = null, $methods = null)
public function getObject(string $index, ?string $defaultValue = null, $methods = null)
{
if ($methods !== null && \is_string($methods) === true) {
$methods = [$methods];
@@ -247,7 +247,7 @@ class InputHandler
* @param array|string|null $methods
* @return InputItem|string
*/
public function get($index, $defaultValue = null, $methods = null)
public function get(string $index, ?string $defaultValue = null, $methods = null)
{
$input = $this->getObject($index, $defaultValue, $methods);
@@ -264,7 +264,7 @@ class InputHandler
* @param string $index
* @return bool
*/
public function exists($index) : bool
public function exists(string $index): bool
{
return ($this->getObject($index) !== null);
}
@@ -274,7 +274,7 @@ class InputHandler
* @param array|null $filter Only take items in filter
* @return array
*/
public function all(array $filter = null) : array
public function all(array $filter = null): array
{
$output = $_GET + $_POST;
+6 -6
View File
@@ -8,7 +8,7 @@ class InputItem implements IInputItem
public $name;
public $value;
public function __construct($index, $value = null)
public function __construct(string $index, ?string $value = null)
{
$this->index = $index;
$this->value = $value;
@@ -25,7 +25,7 @@ class InputItem implements IInputItem
return $this->index;
}
public function setIndex($index): IInputItem
public function setIndex(string $index): IInputItem
{
$this->index = $index;
@@ -43,9 +43,9 @@ class InputItem implements IInputItem
/**
* Set input name
* @param string $name
* @return static $this
* @return static
*/
public function setName($name): IInputItem
public function setName(string $name): IInputItem
{
$this->name = $name;
@@ -63,9 +63,9 @@ class InputItem implements IInputItem
/**
* Set input value
* @param string $value
* @return static $this
* @return static
*/
public function setValue($value): IInputItem
public function setValue(string $value): IInputItem
{
$this->value = $value;
@@ -1,4 +1,5 @@
<?php
namespace Pecee\Http\Middleware\Exceptions;
class TokenMismatchException extends \Exception
+23 -24
View File
@@ -10,7 +10,7 @@ use Pecee\SimpleRouter\SimpleRouter;
class Request
{
private $data = [];
protected $headers;
protected $headers = [];
protected $host;
protected $url;
protected $method;
@@ -35,7 +35,11 @@ class Request
*/
public function __construct()
{
$this->parseHeaders();
foreach ($_SERVER as $key => $value) {
$this->headers[strtolower($key)] = $value;
$this->headers[strtolower(str_replace('_', '-', $key))] = $value;
}
$this->setHost($this->getHeader('http-host'));
// Check if special IIS header exist, otherwise use default.
@@ -45,17 +49,6 @@ class Request
$this->method = strtolower($this->inputHandler->get('_method', $this->getHeader('request-method')));
}
protected function parseHeaders(): void
{
$this->headers = [];
foreach ($_SERVER as $key => $value) {
$this->headers[strtolower($key)] = $value;
$this->headers[strtolower(str_replace('_', '-', $key))] = $value;
}
}
public function isSecure(): bool
{
return $this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || $this->getHeader('server-port') === 443;
@@ -221,9 +214,9 @@ class Request
}
/**
* @param string $host
* @param string|null $host
*/
public function setHost($host): void
public function setHost(?string $host): void
{
$this->host = $host;
}
@@ -231,7 +224,7 @@ class Request
/**
* @param string $method
*/
public function setMethod($method): void
public function setMethod(string $method): void
{
$this->method = $method;
}
@@ -341,26 +334,32 @@ class Request
return $this;
}
/**
* Returns true if the request contains a rewrite
*
* @return bool
*/
public function hasRewrite(): bool
{
return $this->hasRewrite;
}
public function setHasRewrite($value): self
/**
* Defines if the current request contains a rewrite.
*
* @param bool $boolean
* @return Request
*/
public function setHasRewrite(bool $boolean): self
{
$this->hasRewrite = $value;
$this->hasRewrite = $boolean;
return $this;
}
public function isRewrite($url): bool
{
return ($this->rewriteUrl === $url);
}
public function __isset($name)
{
return array_key_exists($name, $this->data);
return array_key_exists($name, $this->data) === true;
}
public function __set($name, $value = null)
+9 -9
View File
@@ -19,7 +19,7 @@ class Response
* @param int $code
* @return static
*/
public function httpCode($code): self
public function httpCode(int $code): self
{
http_response_code($code);
@@ -32,7 +32,7 @@ class Response
* @param string $url
* @param int $httpCode
*/
public function redirect($url, $httpCode = null): void
public function redirect(string $url, ?int $httpCode = null): void
{
if ($httpCode !== null) {
$this->httpCode($httpCode);
@@ -52,7 +52,7 @@ class Response
* @param string $name
* @return static
*/
public function auth($name = ''): self
public function auth(string $name = ''): self
{
$this->headers([
'WWW-Authenticate: Basic realm="' . $name . '"',
@@ -62,19 +62,19 @@ class Response
return $this;
}
public function cache($eTag, $lastModified = 2592000): self
public function cache(string $eTag, int $lastModifiedTime = 2592000): self
{
$this->headers([
'Cache-Control: public',
'Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT',
'Etag: ' . $eTag,
sprintf('Last-Modified: %s GMT', gmdate('D, d M Y H:i:s', $lastModifiedTime)),
sprintf('Etag: %s', $eTag),
]);
$httpModified = $this->request->getHeader('http-if-modified-since');
$httpIfNoneMatch = $this->request->getHeader('http-if-none-match');
if (($httpIfNoneMatch !== null && $httpIfNoneMatch === $eTag) || ($httpModified !== null && strtotime($httpModified) === $lastModified)) {
if (($httpIfNoneMatch !== null && $httpIfNoneMatch === $eTag) || ($httpModified !== null && strtotime($httpModified) === $lastModifiedTime)) {
$this->header('HTTP/1.1 304 Not Modified');
exit(0);
@@ -90,7 +90,7 @@ class Response
* @param int $dept JSON debt.
* @throws InvalidArgumentException
*/
public function json($value, $options = null, $dept = 512): void
public function json($value, ?int $options = null, int $dept = 512): void
{
if (($value instanceof \JsonSerializable) === false && \is_array($value) === false) {
throw new InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
@@ -106,7 +106,7 @@ class Response
* @param string $value
* @return static
*/
public function header($value): self
public function header(string $value): self
{
header($value);
@@ -45,9 +45,9 @@ class CookieTokenProvider implements ITokenProvider
* @param string $token
* @return bool
*/
public function validate($token): bool
public function validate(string $token): bool
{
if ($token !== null && $this->getToken() !== null) {
if ($this->getToken() !== null) {
return hash_equals($token, $this->getToken());
}
@@ -60,7 +60,7 @@ class CookieTokenProvider implements ITokenProvider
*
* @param string $token
*/
public function setToken($token): void
public function setToken(string $token): void
{
$this->token = $token;
setcookie(static::CSRF_KEY, $token, time() + 60 * $this->cookieTimeoutMinutes, '/');
@@ -71,7 +71,7 @@ class CookieTokenProvider implements ITokenProvider
* @param string|null $defaultValue
* @return string|null
*/
public function getToken($defaultValue = null): ?string
public function getToken(?string $defaultValue = null): ?string
{
$this->token = ($this->hasToken() === true) ? $_COOKIE[static::CSRF_KEY] : null;
@@ -108,9 +108,9 @@ class CookieTokenProvider implements ITokenProvider
/**
* Set cookie timeout in minutes
* @param $minutes
* @param int $minutes
*/
public function setCookieTimeoutMinutes($minutes): void
public function setCookieTimeoutMinutes(int $minutes): void
{
$this->cookieTimeoutMinutes = $minutes;
}
@@ -1,6 +1,8 @@
<?php
namespace Pecee\Http\Security\Exceptions;
class SecurityException extends \Exception {
class SecurityException extends \Exception
{
}
+2 -2
View File
@@ -16,7 +16,7 @@ interface ITokenProvider
* @param string $token
* @return bool
*/
public function validate($token): bool;
public function validate(string $token): bool;
/**
* Get token token
@@ -24,6 +24,6 @@ interface ITokenProvider
* @param string|null $defaultValue
* @return string|null
*/
public function getToken($defaultValue = null);
public function getToken(?string $defaultValue = null): ?string;
}
+30 -9
View File
@@ -23,15 +23,16 @@ class Url
* @param string $url
* @throws MalformedUrlException
*/
public function __construct($url)
public function __construct(?string $url)
{
$this->originalUrl = $url;
$this->data = $this->parseUrl($url) + $this->data;
if ($url !== null) {
$this->data = $this->parseUrl($url) + $this->data;
if (isset($this->data['path']) === true && $this->data['path'] !== '/') {
$this->data['path'] = rtrim($this->data['path'], '/') . '/';
if (isset($this->data['path']) === true && $this->data['path'] !== '/') {
$this->data['path'] = rtrim($this->data['path'], '/') . '/';
}
}
}
/**
@@ -103,7 +104,7 @@ class Url
*/
public function getPath(): ?string
{
return $this->data['path'];
return $this->data['path'] ?? '/';
}
/**
@@ -139,7 +140,7 @@ class Url
* @throws MalformedUrlException
* @return array
*/
public function parseUrl($url, $component = -1): array
public function parseUrl(string $url, int $component = -1): array
{
$encodedUrl = preg_replace_callback(
'/[^:\/@?&=#]+/u',
@@ -158,9 +159,29 @@ class Url
return array_map('urldecode', $parts);
}
public function contains($value): bool
/**
* Get position of value.
* Returns -1 on failure.
*
* @param string $value
* @return int
*/
public function indexOf(string $value): int
{
return (stripos($this->getOriginalUrl(), $value) === false);
$index = stripos($this->getOriginalUrl(), $value);
return ($index === false) ? -1 : $index;
}
/**
* Check if url contains value.
*
* @param string $value
* @return bool
*/
public function contains(string $value): bool
{
return (stripos($this->getOriginalUrl(), $value) !== false);
}
/**