Merge pull request #567 from skipperbent/v4-setmatch-parameters

Fixed custom regex (setMatch) not setting parsed parameters (issue: #566)
This commit is contained in:
Simon Sessingø
2021-06-15 10:13:54 +02:00
committed by GitHub
12 changed files with 123 additions and 9 deletions
+27
View File
@@ -6,12 +6,39 @@ use Pecee\Exceptions\InvalidArgumentException;
class InputFile implements IInputItem class InputFile implements IInputItem
{ {
/**
* @var string
*/
public $index; public $index;
/**
* @var string
*/
public $name; public $name;
/**
* @var string|null
*/
public $filename; public $filename;
/**
* @var int|null
*/
public $size; public $size;
/**
* @var int|null
*/
public $type; public $type;
/**
* @var int
*/
public $errors; public $errors;
/**
* @var string|null
*/
public $tmpName; public $tmpName;
public function __construct(string $index) public function __construct(string $index)
@@ -18,11 +18,16 @@ class BaseCsrfVerifier implements IMiddleware
* @var array|null * @var array|null
*/ */
protected $except; protected $except;
/** /**
* 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 $include;
/**
* @var ITokenProvider
*/
protected $tokenProvider; protected $tokenProvider;
/** /**
@@ -9,7 +9,14 @@ class CookieTokenProvider implements ITokenProvider
{ {
public const CSRF_KEY = 'CSRF-TOKEN'; public const CSRF_KEY = 'CSRF-TOKEN';
/**
* @var string
*/
protected $token; protected $token;
/**
* @var int
*/
protected $cookieTimeoutMinutes = 120; protected $cookieTimeoutMinutes = 120;
/** /**
+34
View File
@@ -7,15 +7,49 @@ use Pecee\Http\Exceptions\MalformedUrlException;
class Url implements JsonSerializable class Url implements JsonSerializable
{ {
/**
* @var string|null
*/
private $originalUrl; private $originalUrl;
/**
* @var string|null
*/
private $scheme; private $scheme;
/**
* @var string|null
*/
private $host; private $host;
/**
* @var int|null
*/
private $port; private $port;
/**
* @var string|null
*/
private $username; private $username;
/**
* @var string|null
*/
private $password; private $password;
/**
* @var string|null
*/
private $path; private $path;
/**
* @var array
*/
private $params = []; private $params = [];
/**
* @var string|null
*/
private $fragment; private $fragment;
/** /**
@@ -24,7 +24,7 @@ class EventArgument implements IEventArgument
*/ */
protected $arguments = []; protected $arguments = [];
public function __construct($eventName, $router, array $arguments = []) public function __construct(string $eventName, Router $router, array $arguments = [])
{ {
$this->eventName = $eventName; $this->eventName = $eventName;
$this->router = $router; $this->router = $router;
@@ -94,7 +94,7 @@ class EventArgument implements IEventArgument
* @param mixed $value * @param mixed $value
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function __set(string $name, $value) public function __set(string $name, $value): void
{ {
throw new InvalidArgumentException('Not supported'); throw new InvalidArgumentException('Not supported');
} }
@@ -6,10 +6,17 @@ use Throwable;
class ClassNotFoundHttpException extends NotFoundHttpException class ClassNotFoundHttpException extends NotFoundHttpException
{ {
/**
* @var string
*/
protected $class; protected $class;
/**
* @var string|null
*/
protected $method; protected $method;
public function __construct(string $class, ?string $method = null, $message = "", $code = 0, Throwable $previous = null) public function __construct(string $class, ?string $method = null, string $message = "", int $code = 0, Throwable $previous = null)
{ {
parent::__construct($message, $code, $previous); parent::__construct($message, $code, $previous);
@@ -17,6 +17,9 @@ use Pecee\Http\Request;
class CallbackExceptionHandler implements IExceptionHandler class CallbackExceptionHandler implements IExceptionHandler
{ {
/**
* @var Closure
*/
protected $callback; protected $callback;
public function __construct(Closure $callback) public function __construct(Closure $callback)
@@ -47,7 +47,7 @@ class DebugEventHandler implements IEventHandler
public function fireEvents(Router $router, string $name, array $eventArgs = []): void public function fireEvents(Router $router, string $name, array $eventArgs = []): void
{ {
$callback = $this->callback; $callback = $this->callback;
$callback(new EventArgument($router, $eventArgs)); $callback(new EventArgument($name, $router, $eventArgs));
} }
/** /**
+11 -1
View File
@@ -19,6 +19,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
*/ */
protected $name; protected $name;
/**
* @var string|null
*/
protected $regex; protected $regex;
/** /**
@@ -59,7 +62,14 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
return null; return null;
} }
return ((bool)preg_match($this->regex, $url) !== false); $parameters = [];
if ((bool)preg_match($this->regex, $url, $parameters) !== false) {
$this->setParameters($parameters);
return true;
}
return false;
} }
/** /**
+6 -1
View File
@@ -6,7 +6,12 @@ use Pecee\Http\Request;
class RouteUrl extends LoadableRoute class RouteUrl extends LoadableRoute
{ {
public function __construct($url, $callback) /**
* RouteUrl constructor.
* @param string $url
* @param \Closure|string $callback
*/
public function __construct(string $url, $callback)
{ {
$this->setUrl($url); $this->setUrl($url);
$this->setCallback($callback); $this->setCallback($callback);
@@ -181,6 +181,22 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
$output = TestRouter::debugOutput('/admin/asd/bec/123', 'get'); $output = TestRouter::debugOutput('/admin/asd/bec/123', 'get');
$this->assertEquals('match', $output); $this->assertEquals('match', $output);
TestRouter::router()->reset();
}
public function testCustomRegexWithParameter()
{
TestRouter::request()->setHost('google.com');
$results = '';
TestRouter::get('/tester/{param}', function ($param = null) use($results) {
return $results = $param;
})->setMatch('/(.*)/i');
$output = TestRouter::debugOutput('/tester/abepik/ko');
$this->assertEquals('/tester/abepik/ko/', $output);
} }
public function testRenderMultipleRoutesDisabled() public function testRenderMultipleRoutesDisabled()
+3 -3
View File
@@ -8,7 +8,7 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
static::request()->setHost('testhost.com'); static::request()->setHost('testhost.com');
} }
public static function debugNoReset($testUrl, $testMethod = 'get') public static function debugNoReset(string $testUrl, string $testMethod = 'get'): void
{ {
$request = static::request(); $request = static::request();
@@ -18,7 +18,7 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
static::start(); static::start();
} }
public static function debug($testUrl, $testMethod = 'get', bool $reset = true) public static function debug(string $testUrl, string $testMethod = 'get', bool $reset = true): void
{ {
try { try {
static::debugNoReset($testUrl, $testMethod); static::debugNoReset($testUrl, $testMethod);
@@ -35,7 +35,7 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
} }
public static function debugOutput($testUrl, $testMethod = 'get', bool $reset = true) public static function debugOutput(string $testUrl, string $testMethod = 'get', bool $reset = true): string
{ {
$response = null; $response = null;