Compare commits

...

24 Commits

Author SHA1 Message Date
Simon Sessingø 4b8dbdc9e5 Merge pull request #575 from skipperbent/v4-development
Updated documentation
2021-07-16 22:31:06 +02:00
Simon Sessingø 7fe66ac938 Updated documentation
- Changed Router references to SimpleRouter.
- Updated ErrorHandler example.
- Minor tweaks
2021-07-16 22:28:54 +02:00
Simon Sessingø e5552a88cf Merge pull request #568 from skipperbent/v4-development
Version 4.3.6.1
2021-06-15 10:16:25 +02:00
Simon Sessingø 7d80517c2f Merge pull request #567 from skipperbent/v4-setmatch-parameters
Fixed custom regex (setMatch) not setting parsed parameters (issue: #566)
2021-06-15 10:13:54 +02:00
Simon Sessingø b3c135c723 Development
- Fixed DebugHandler::fireEvent not providing correct arguments when calling fireEvents.
- Fixed custom regex setMatch not setting parsed parameters correctly (issue: #566).
- Added unit-tests for catching issue in the future.
- Added php-stan typehints.
2021-06-15 10:11:09 +02:00
Simon Sessingø e990b95c50 Merge pull request #563 from skipperbent/v4-development
Version 4.3.6.0
2021-06-09 09:19:14 +02:00
Simon Sessingø a35400b7a0 Merge pull request #562 from skipperbent/v4-cleanup-bugfixes
Development
2021-06-09 09:16:39 +02:00
Simon Sessingø 22606dfc12 Updates 2021-06-09 09:13:11 +02:00
Simon Sessingø 5cd6cab801 Development
- Fixed issue causing default-namespace to add duplicate namespace when using type-hints (issue: #561).
- Fixed phpstan issues.
- Tests: Fixed TestRouter not resetting namespace upon reset.
- Tests: Added NSController (namespace controller) class.
- Tests: added test for class hint + default namespace case.
- Composer: added phpstan support + configuration.
- Composer: updated phpunit version.
2021-06-09 08:54:24 +02:00
Simon Sessingø 319ce7a569 Merge pull request #559 from skipperbent/v4-development
Version 4.3.5.0
2021-05-20 15:19:00 +02:00
Simon Sessingø 4dff4006bf Merge pull request #558 from skipperbent/v4-exists-array
Added support for InputHandler::exists to check array of indexes.
2021-05-20 15:15:51 +02:00
Simon Sessingø eea30d0f59 Simplified RouteController and RouteResource by moving common group-match code to parent method. 2021-05-20 15:15:26 +02:00
Simon Sessingø ece9d30905 Added support for InputHandler::exists to check array of indexes.
- Updated documentation to reflect changes.
2021-05-20 15:03:56 +02:00
Simon Sessingø d4de7fc3df Merge pull request #556 from skipperbent/v4-development
Version 4.3.4.2
2021-05-19 22:05:44 +02:00
Simon Sessingø 03ef9dfb74 Merge pull request #555 from skipperbent/v4-error-group-fix
Fixed issue with SimpleRouter::error not firing within group (issue: #551)
2021-05-19 22:03:39 +02:00
Simon Sessingø 4c5f825c97 Fixed issue with SimpleRouter::error not firing within group (issue: #551).
- Fixed variable incorrect variable reference in `InputItem` class.
- Added new `Router::addExceptionHandler` method.
- Added parameter types in `Url` class.
- Fixed phpdoc parameter-type for `Request::getHeader`.
2021-05-19 22:00:42 +02:00
Simon Sessingø b7c31ae434 Merge pull request #553 from skipperbent/v4-development
Version 4.3.4.1
2021-05-19 04:43:29 +02:00
Simon Sessingø 0c329e4c5b Merge pull request #552 from skipperbent/v4-exception-callback-fix
SimpleRouter::error not working within group
2021-05-19 04:42:35 +02:00
Simon Sessingø e057a76153 Removed unused variable. 2021-05-19 04:41:36 +02:00
Simon Sessingø f7f1f1e3de Fixed issue causing SimpleRouter::exception helper not to work when used within a group (issue: #551) 2021-05-19 04:31:50 +02:00
Simon Sessingø f93621fa02 Merge pull request #549 from skipperbent/v4-development
Version 4.3.4.0
2021-05-19 00:45:32 +02:00
Simon Sessingø 5ab8826bfb Merge pull request #548 from skipperbent/v4-inputitem-array
InputHandler optimisations.
2021-05-19 00:42:48 +02:00
Simon Sessingø f863f931d8 Fixed php8 php-unit tests compatibility. 2021-05-18 18:08:41 +02:00
Simon Sessingø 0d6326dfbb InputHandler optimisations.
- InputItem can now be used like array (for example: input()->get('items')[0]) if value is array.
- Changed default-value parameter for get, post, file can now be mixed to allow object as return-type.
2021-05-18 18:00:18 +02:00
29 changed files with 377 additions and 106 deletions
+47 -11
View File
@@ -77,6 +77,7 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c
- [Get parameter object](#get-parameter-object)
- [Managing files](#managing-files)
- [Get all parameters](#get-all-parameters)
- [Check if parameters exists](#check-if-parameters-exists)
- [Events](#events)
- [Available events](#available-events)
- [Registering new event](#registering-new-event)
@@ -790,7 +791,7 @@ If you want to store the token elsewhere, please refer to the "Creating custom T
When you've created your CSRF-verifier you need to tell simple-php-router that it should use it. You can do this by adding the following line in your `routes.php` file:
```php
Router::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());
SimpleRouter::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());
```
## Getting CSRF-token
@@ -806,7 +807,7 @@ csrf_token();
You can also get the token directly:
```php
return Router::router()->getCsrfVerifier()->getTokenProvider()->getToken();
return SimpleRouter::router()->getCsrfVerifier()->getTokenProvider()->getToken();
```
The default name/key for the input-field is `csrf_token` and is defined in the `POST_KEY` constant in the `BaseCsrfVerifier` class.
@@ -892,10 +893,10 @@ class SessionTokenProvider implements ITokenProvider
Next you need to set your custom `ITokenProvider` implementation on your `BaseCsrfVerifier` class in your routes file:
```php
$verifier = new \dscuz\Middleware\CsrfVerifier();
$verifier = new \Demo\Middlewares\CsrfVerifier();
$verifier->setTokenProvider(new SessionTokenProvider());
Router::csrfVerifier($verifier);
SimpleRouter::csrfVerifier($verifier);
```
---
@@ -937,7 +938,7 @@ ExceptionHandler are classes that handles all exceptions. ExceptionsHandlers mus
## Handling 404, 403 and other errors
If you simply want to catch a 404 (page not found) etc. you can use the `Router::error($callback)` static helper method.
If you simply want to catch a 404 (page not found) etc. you can use the `SimpleRouter::error($callback)` static helper method.
This will add a callback method which is fired whenever an error occurs on all routes.
@@ -945,17 +946,31 @@ The basic example below simply redirect the page to `/not-found` if an `NotFound
The code should be placed in the file that contains your routes.
```php
Router::get('/not-found', 'PageController@notFound');
SimpleRouter::get('/not-found', 'PageController@notFound');
SimpleRouter::get('/forbidden', 'PageController@notFound');
Router::error(function(Request $request, \Exception $exception) {
SimpleRouter::error(function(Request $request, \Exception $exception) {
if($exception instanceof NotFoundHttpException && $exception->getCode() === 404) {
response()->redirect('/not-found');
switch($exception->getCode()) {
// Page not found
case 404:
response()->redirect('/not-found');
// Forbidden
case 403:
response()->redirect('/forbidden');
}
});
```
The example above will redirect all errors with http-code `404` (page not found) to `/not-found` and `403` (forbidden) to `/forbidden`.
If you do not want a redirect, but want the error-page rendered on the current-url, you can tell the router to execute a rewrite callback like so:
```php
$request->setRewriteCallback('ErrorController@notFound');
```
## Using custom exception handlers
This is a basic example of an ExceptionHandler implementation (please see "[Easily overwrite route about to be loaded](#easily-overwrite-route-about-to-be-loaded)" for examples on how to change callback).
@@ -1236,8 +1251,11 @@ $values = input()->all([
All object implements the `IInputItem` interface and will always contain these methods:
- `getIndex()` - returns the index/key of the input.
- `setIndex()` - set the index/key of the input.
- `getName()` - returns a human friendly name for the input (company_name will be Company Name etc).
- `setName()` - sets a human friendly name for the input (company_name will be Company Name etc).
- `getValue()` - returns the value of the input.
- `setValue()` - sets the value of the input.
`InputFile` has the same methods as above along with some other file-specific methods like:
@@ -1253,6 +1271,24 @@ All object implements the `IInputItem` interface and will always contain these m
---
### Check if parameters exists
You can easily if multiple items exists by using the `exists` method. It's simular to `value` as it can be used
to filter on request-methods and supports both `string` and `array` as parameter value.
**Example:**
```php
if(input()->exists(['name', 'lastname'])) {
// Do stuff
}
/* Similar to code above */
if(input()->exists('name') && input()->exists('lastname')) {
// Do stuff
}
```
# Events
This section will help you understand how to register your own callbacks to events in the router.
@@ -1275,7 +1311,7 @@ All event callbacks will retrieve a `EventArgument` object as parameter. This ob
| `EVENT_RENDER_BOOTMANAGER` | `bootmanagers`<br>`bootmanager` | Fires before a boot-manager is rendered. |
| `EVENT_LOAD_ROUTES` | `routes` | Fires when the router is about to load all routes. |
| `EVENT_FIND_ROUTE` | `name` | Fires whenever the `findRoute` method is called within the `Router`. This usually happens when the router tries to find routes that contains a certain url, usually after the `EventHandler::EVENT_GET_URL` event. |
| `EVENT_GET_URL` | `name`<br>`parameters`<br>`getParams` | Fires whenever the `Router::getUrl` method or `url`-helper function is called and the router tries to find the route. |
| `EVENT_GET_URL` | `name`<br>`parameters`<br>`getParams` | Fires whenever the `SimpleRouter::getUrl` method or `url`-helper function is called and the router tries to find the route. |
| `EVENT_MATCH_ROUTE` | `route` | Fires when a route is matched and valid (correct request-type etc). and before the route is rendered. |
| `EVENT_RENDER_ROUTE` | `route` | Fires before a route is rendered. |
| `EVENT_LOAD_EXCEPTIONS` | `exception`<br>`exceptionHandlers` | Fires when the router is loading exception-handlers. |
@@ -1458,7 +1494,7 @@ $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function(EventArgument $e
});
TestRouter::addEventHandler($eventHandler);
SimpleRouter::addEventHandler($eventHandler);
```
In the example shown above, we create a new `EVENT_ADD_ROUTE` event that triggers, when a new route is added.
+6 -2
View File
@@ -32,7 +32,11 @@
},
"require-dev": {
"phpunit/phpunit": "^7",
"mockery/mockery": "^1"
"mockery/mockery": "^1",
"phpstan/phpstan": "^0",
"phpstan/phpstan-phpunit": "^0",
"phpstan/phpstan-deprecation-rules": "^0",
"phpstan/phpstan-strict-rules": "^0"
},
"scripts": {
"test": [
@@ -44,4 +48,4 @@
"Pecee\\": "src/Pecee/"
}
}
}
}
+22
View File
@@ -0,0 +1,22 @@
parameters:
level: 6
paths:
- src
fileExtensions:
- php
bootstrapFiles:
- ./vendor/autoload.php
ignoreErrors:
reportUnmatchedIgnoredErrors: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
parallel:
processTimeout: 300.0
jobSize: 10
maximumNumberOfProcesses: 4
minimumNumberOfJobsPerProcess: 4
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
+6
View File
@@ -13,8 +13,14 @@ interface IInputItem
public function setName(string $name): self;
/**
* @return mixed
*/
public function getValue();
/**
* @param mixed $value
*/
public function setValue($value): self;
public function __toString(): string;
+27
View File
@@ -6,12 +6,39 @@ use Pecee\Exceptions\InvalidArgumentException;
class InputFile implements IInputItem
{
/**
* @var string
*/
public $index;
/**
* @var string
*/
public $name;
/**
* @var string|null
*/
public $filename;
/**
* @var int|null
*/
public $size;
/**
* @var int|null
*/
public $type;
/**
* @var int
*/
public $errors;
/**
* @var string|null
*/
public $tmpName;
public function __construct(string $index)
+23 -11
View File
@@ -115,7 +115,7 @@ class InputHandler
// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $parentKey ?? $key;
$values = ['index' => $parentKey ?? $key];
try {
$list[$key] = InputFile::createFromArray($values + $value);
@@ -161,7 +161,7 @@ class InputHandler
try {
$file = InputFile::createFromArray([
'index' => (empty($key) === true && empty($originalIndex) === false) ? $originalIndex : $key,
'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'tmp_name' => $original['tmp_name'][$key],
@@ -293,14 +293,26 @@ class InputHandler
}
/**
* Check if a input-item exist
* Check if a input-item exist.
* If an array is as $index parameter the method returns true if all elements exist.
*
* @param string $index
* @param string|array $index
* @param array ...$methods
* @return bool
*/
public function exists(string $index, ...$methods): bool
public function exists($index, ...$methods): bool
{
// Check array
if(is_array($index) === true) {
foreach($index as $key) {
if($this->value($key, null, ...$methods) === null) {
return false;
}
}
return true;
}
return $this->value($index, null, ...$methods) !== null;
}
@@ -308,10 +320,10 @@ class InputHandler
* Find post-value by index or return default value.
*
* @param string $index
* @param string|null $defaultValue
* @param mixed|null $defaultValue
* @return InputItem|array|string|null
*/
public function post(string $index, ?string $defaultValue = null)
public function post(string $index, $defaultValue = null)
{
return $this->post[$index] ?? $defaultValue;
}
@@ -320,10 +332,10 @@ class InputHandler
* Find file by index or return default value.
*
* @param string $index
* @param string|null $defaultValue
* @param mixed|null $defaultValue
* @return InputFile|array|string|null
*/
public function file(string $index, ?string $defaultValue = null)
public function file(string $index, $defaultValue = null)
{
return $this->file[$index] ?? $defaultValue;
}
@@ -332,10 +344,10 @@ class InputHandler
* Find parameter/query-string by index or return default value.
*
* @param string $index
* @param string|null $defaultValue
* @param mixed|null $defaultValue
* @return InputItem|array|string|null
*/
public function get(string $index, ?string $defaultValue = null)
public function get(string $index, $defaultValue = null)
{
return $this->get[$index] ?? $defaultValue;
}
+27 -1
View File
@@ -2,10 +2,11 @@
namespace Pecee\Http\Input;
use ArrayAccess;
use ArrayIterator;
use IteratorAggregate;
class InputItem implements IInputItem, IteratorAggregate
class InputItem implements ArrayAccess, IInputItem, IteratorAggregate
{
public $index;
public $name;
@@ -75,9 +76,34 @@ class InputItem implements IInputItem, IteratorAggregate
return $this;
}
public function offsetExists($offset): bool
{
return isset($this->value[$offset]);
}
public function offsetGet($offset)
{
if ($this->offsetExists($offset) === true) {
return $this->value[$offset];
}
return null;
}
public function offsetSet($offset, $value): void
{
$this->value[$offset] = $value;
}
public function offsetUnset($offset): void
{
unset($this->value[$offset]);
}
public function __toString(): string
{
$value = $this->getValue();
return (is_array($value) === true) ? json_encode($value) : $value;
}
@@ -18,11 +18,16 @@ class BaseCsrfVerifier implements IMiddleware
* @var array|null
*/
protected $except;
/**
* Urls to include. Can be used to include urls from a certain path.
* @var array|null
*/
protected $include;
/**
* @var ITokenProvider
*/
protected $tokenProvider;
/**
+3 -3
View File
@@ -137,7 +137,7 @@ class Request
public function isSecure(): bool
{
return $this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || $this->getHeader('server-port') === 443;
return $this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || (int)$this->getHeader('server-port') === 443;
}
/**
@@ -264,12 +264,12 @@ class Request
* Get header value by name
*
* @param string $name Name of the header.
* @param string|null $defaultValue Value to be returned if header is not found.
* @param string|mixed|null $defaultValue Value to be returned if header is not found.
* @param bool $tryParse When enabled the method will try to find the header from both from client (http) and server-side variants, if the header is not found.
*
* @return string|null
*/
public function getHeader(string $name, $defaultValue = null, $tryParse = true): ?string
public function getHeader(string $name, $defaultValue = null, bool $tryParse = true): ?string
{
$name = strtolower($name);
$header = $this->headers[$name] ?? null;
@@ -9,7 +9,14 @@ class CookieTokenProvider implements ITokenProvider
{
public const CSRF_KEY = 'CSRF-TOKEN';
/**
* @var string
*/
protected $token;
/**
* @var int
*/
protected $cookieTimeoutMinutes = 120;
/**
+40 -6
View File
@@ -7,15 +7,49 @@ use Pecee\Http\Exceptions\MalformedUrlException;
class Url implements JsonSerializable
{
/**
* @var string|null
*/
private $originalUrl;
/**
* @var string|null
*/
private $scheme;
/**
* @var string|null
*/
private $host;
/**
* @var int|null
*/
private $port;
/**
* @var string|null
*/
private $username;
/**
* @var string|null
*/
private $password;
/**
* @var string|null
*/
private $path;
/**
* @var array
*/
private $params = [];
/**
* @var string|null
*/
private $fragment;
/**
@@ -387,7 +421,7 @@ class Url implements JsonSerializable
{
$encodedUrl = preg_replace_callback(
'/[^:\/@?&=#]+/u',
static function ($matches) {
static function ($matches): string {
return urlencode($matches[0]);
},
$url
@@ -414,7 +448,7 @@ class Url implements JsonSerializable
if (count($getParams) !== 0) {
if ($includeEmpty === false) {
$getParams = array_filter($getParams, static function ($item) {
$getParams = array_filter($getParams, static function ($item): bool {
return (trim($item) !== '');
});
}
@@ -431,7 +465,7 @@ class Url implements JsonSerializable
* @param bool $includeParams
* @return string
*/
public function getRelativeUrl($includeParams = true): string
public function getRelativeUrl(bool $includeParams = true): string
{
$path = $this->path ?? '/';
@@ -451,14 +485,14 @@ class Url implements JsonSerializable
* @param bool $includeParams
* @return string
*/
public function getAbsoluteUrl($includeParams = true): string
public function getAbsoluteUrl(bool $includeParams = true): string
{
$scheme = $this->scheme !== null ? $this->scheme . '://' : '';
$host = $this->host ?? '';
$port = $this->port !== null ? ':' . $this->port : '';
$user = $this->username ?? '';
$pass = $this->password !== null ? ':' . $this->password : '';
$pass = ($user || $pass) ? $pass . '@' : '';
$pass = ($user !== '' || $pass !== '') ? $pass . '@' : '';
return $scheme . $user . $pass . $host . $port . $this->getRelativeUrl($includeParams);
}
@@ -466,7 +500,7 @@ class Url implements JsonSerializable
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* @return string data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
@@ -24,7 +24,7 @@ class EventArgument implements IEventArgument
*/
protected $arguments = [];
public function __construct($eventName, $router, array $arguments = [])
public function __construct(string $eventName, Router $router, array $arguments = [])
{
$this->eventName = $eventName;
$this->router = $router;
@@ -94,7 +94,7 @@ class EventArgument implements IEventArgument
* @param mixed $value
* @throws InvalidArgumentException
*/
public function __set(string $name, $value)
public function __set(string $name, $value): void
{
throw new InvalidArgumentException('Not supported');
}
@@ -6,10 +6,17 @@ use Throwable;
class ClassNotFoundHttpException extends NotFoundHttpException
{
/**
* @var string
*/
protected $class;
/**
* @var string|null
*/
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);
@@ -17,6 +17,9 @@ use Pecee\Http\Request;
class CallbackExceptionHandler implements IExceptionHandler
{
/**
* @var Closure
*/
protected $callback;
public function __construct(Closure $callback)
@@ -17,7 +17,7 @@ class DebugEventHandler implements IEventHandler
public function __construct()
{
$this->callback = static function (EventArgument $argument) {
$this->callback = static function (EventArgument $argument): void {
// todo: log in database
};
}
@@ -47,7 +47,7 @@ class DebugEventHandler implements IEventHandler
public function fireEvents(Router $router, string $name, array $eventArgs = []): void
{
$callback = $this->callback;
$callback(new EventArgument($router, $eventArgs));
$callback(new EventArgument($name, $router, $eventArgs));
}
/**
@@ -2,7 +2,7 @@
namespace Pecee\SimpleRouter\Route;
interface IControllerRoute extends IRoute
interface IControllerRoute extends ILoadableRoute
{
/**
* Get controller class-name
+24 -2
View File
@@ -19,6 +19,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
*/
protected $name;
/**
* @var string|null
*/
protected $regex;
/**
@@ -59,7 +62,14 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
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;
}
/**
@@ -100,6 +110,18 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
return $this->url;
}
/**
* Returns true if group is defined and matches the given url.
*
* @param string $url
* @param Request $request
* @return bool
*/
protected function matchGroup(string $url, Request $request): bool
{
return ($this->getGroup() === null || $this->getGroup()->matchRoute($url, $request) === true);
}
/**
* Find url that matches method, parameters or name.
* Used when calling the url() helper.
@@ -203,9 +225,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* Sets the router name, which makes it easier to obtain the url or router at a later point.
* Alias for LoadableRoute::setName().
*
* @see LoadableRoute::setName()
* @param string|array $name
* @return static
* @see LoadableRoute::setName()
*/
public function name($name): ILoadableRoute
{
+11 -2
View File
@@ -30,6 +30,9 @@ abstract class Route implements IRoute
protected $urlRegex = '/^%s\/?$/u';
protected $group;
protected $parent;
/**
* @var string|callable|null
*/
protected $callback;
protected $defaultNamespace;
@@ -67,7 +70,7 @@ abstract class Route implements IRoute
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, static function ($var) {
$parameters = array_filter($parameters, static function ($var): bool {
return ($var !== null);
});
}
@@ -82,6 +85,7 @@ abstract class Route implements IRoute
}
/* When the callback is a function */
return $router->getClassLoader()->loadClosure($callback, $parameters);
}
@@ -280,7 +284,7 @@ abstract class Route implements IRoute
}
/**
* @return string|callable
* @return string|callable|null
*/
public function getCallback()
{
@@ -337,6 +341,11 @@ abstract class Route implements IRoute
*/
public function setNamespace(string $namespace): IRoute
{
// Do not set namespace when class-hinting is used
if (is_array($this->callback) === true) {
return $this;
}
$ns = $this->getNamespace();
if ($ns !== null) {
@@ -52,7 +52,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
{
if (strpos($name, '.') !== false) {
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names, false);
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names, true);
if ($found !== false) {
$method = (string)$found;
}
@@ -67,7 +67,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
foreach (Request::$requestTypes as $requestType) {
if (stripos($method, $requestType) === 0) {
$method = (string)substr($method, strlen($requestType));
$method = substr($method, strlen($requestType));
break;
}
}
@@ -88,7 +88,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
public function matchRoute(string $url, Request $request): bool
{
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
if ($this->matchGroup($url, $request) === false) {
return false;
}
@@ -54,7 +54,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
/* Remove method/type */
if (strpos($name, '.') !== false) {
$name = (string)substr($name, 0, strrpos($name, '.'));
$name = substr($name, 0, strrpos($name, '.'));
}
return (strtolower($this->name) === strtolower($name));
@@ -68,7 +68,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
*/
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
{
$url = array_search($name, $this->names, false);
$url = array_search($name, $this->names, true);
if ($url !== false) {
return rtrim($this->url . $this->urls[$url], '/') . '/';
}
@@ -85,7 +85,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
public function matchRoute(string $url, Request $request): bool
{
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
if ($this->matchGroup($url, $request) === false) {
return false;
}
+6 -1
View File
@@ -6,7 +6,12 @@ use Pecee\Http\Request;
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->setCallback($callback);
+18 -17
View File
@@ -44,7 +44,7 @@ class Router
/**
* List of processed routes
* @var array
* @var array|ILoadableRoute[]
*/
protected $processedRoutes = [];
@@ -63,7 +63,7 @@ class Router
/**
* Csrf verifier class
* @var BaseCsrfVerifier
* @var BaseCsrfVerifier|null
*/
protected $csrfVerifier;
@@ -107,7 +107,7 @@ class Router
/**
* Class loader instance
* @var ClassLoader
* @var IClassLoader
*/
protected $classLoader;
@@ -215,7 +215,7 @@ class Router
$exceptionHandlers = [];
// Stop processing routes if no valid route is found.
if ($this->request->getRewriteRoute() === null && $this->request->getUrl() === null) {
if ($this->request->getRewriteRoute() === null && $this->request->getUrl()->getOriginalUrl() === '') {
$this->debug('Halted route-processing as no valid route was found');
return;
@@ -575,7 +575,6 @@ class Router
'name' => $name,
]);
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) {
/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */
@@ -593,7 +592,7 @@ class Router
}
/* Using @ is most definitely a controller@method or alias@method */
if (is_string($name) === true && strpos($name, '@') !== false) {
if (strpos($name, '@') !== false) {
[$controller, $method] = array_map('strtolower', explode('@', $name));
if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) {
@@ -605,7 +604,7 @@ class Router
/* Check if callback matches (if it's not a function) */
$callback = $route->getCallback();
if (is_string($name) === true && is_string($callback) === true && is_callable($callback) === false && strpos($name, '@') !== false && strpos($callback, '@') !== false) {
if (is_string($callback) === true && is_callable($callback) === false && strpos($name, '@') !== false && strpos($callback, '@') !== false) {
/* Check if the entire callback is matching */
if (strpos($callback, $name) === 0 || strtolower($callback) === strtolower($name)) {
@@ -656,10 +655,6 @@ class Router
'getParams' => $getParams,
]);
if ($getParams !== null && is_array($getParams) === false) {
throw new InvalidArgumentException('Invalid type for getParams. Must be array or null');
}
if ($name === '' && $parameters === '') {
return new Url('/');
}
@@ -703,21 +698,21 @@ class Router
/* Loop through all the routes to see if we can find a match */
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) {
foreach ($this->processedRoutes as $processedRoute) {
/* Check if the route contains the name/alias */
if ($route->hasName($controller) === true) {
if ($processedRoute->hasName($controller) === true) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($method, $parameters, $name))
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
}
/* Check if the route controller is equal to the name */
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($controller)) {
if ($processedRoute instanceof IControllerRoute && strtolower($processedRoute->getController()) === strtolower($controller)) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($method, $parameters, $name))
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
}
@@ -842,7 +837,7 @@ class Router
/**
* Get class loader
*
* @return ClassLoader
* @return IClassLoader
*/
public function getClassLoader(): IClassLoader
{
@@ -944,4 +939,10 @@ class Router
return $this;
}
public function addExceptionHandler(IExceptionHandler $handler): self
{
$this->exceptionHandlers[] = $handler;
return $this;
}
}
+14 -28
View File
@@ -22,6 +22,7 @@ use Pecee\SimpleRouter\Exceptions\HttpException;
use Pecee\SimpleRouter\Handlers\CallbackExceptionHandler;
use Pecee\SimpleRouter\Handlers\IEventHandler;
use Pecee\SimpleRouter\Route\IGroupRoute;
use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IPartialGroupRoute;
use Pecee\SimpleRouter\Route\IRoute;
use Pecee\SimpleRouter\Route\RouteController;
@@ -173,7 +174,7 @@ class SimpleRouter
*/
public static function redirect(string $where, string $to, int $httpCode = 301): IRoute
{
return static::get($where, function () use ($to, $httpCode) {
return static::get($where, static function () use ($to, $httpCode): void {
static::response()->redirect($to, $httpCode);
});
}
@@ -185,7 +186,7 @@ class SimpleRouter
* @param string|array|Closure $callback
* @param array|null $settings
*
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function get(string $url, $callback, array $settings = null): IRoute
{
@@ -198,7 +199,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function post(string $url, $callback, array $settings = null): IRoute
{
@@ -211,7 +212,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function put(string $url, $callback, array $settings = null): IRoute
{
@@ -224,7 +225,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function patch(string $url, $callback, array $settings = null): IRoute
{
@@ -237,7 +238,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function options(string $url, $callback, array $settings = null): IRoute
{
@@ -250,7 +251,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
*/
public static function delete(string $url, $callback, array $settings = null): IRoute
{
@@ -262,15 +263,11 @@ class SimpleRouter
*
* @param array $settings
* @param Closure $callback
* @return RouteGroup
* @return RouteGroup|IGroupRoute
* @throws InvalidArgumentException
*/
public static function group(array $settings, Closure $callback): IGroupRoute
{
if (is_callable($callback) === false) {
throw new InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$group = new RouteGroup();
$group->setCallback($callback);
$group->setSettings($settings);
@@ -287,15 +284,11 @@ class SimpleRouter
* @param string $url
* @param Closure $callback
* @param array $settings
* @return RoutePartialGroup
* @return RoutePartialGroup|IPartialGroupRoute
* @throws InvalidArgumentException
*/
public static function partialGroup(string $url, Closure $callback, array $settings = []): IPartialGroupRoute
{
if (is_callable($callback) === false) {
throw new InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$settings['prefix'] = $url;
$group = new RoutePartialGroup();
@@ -313,7 +306,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
* @see SimpleRouter::form
*/
public static function basic(string $url, $callback, array $settings = null): IRoute
@@ -328,7 +321,7 @@ class SimpleRouter
* @param string $url
* @param string|array|Closure $callback
* @param array|null $settings
* @return RouteUrl
* @return RouteUrl|IRoute
* @see SimpleRouter::form
*/
public static function form(string $url, $callback, array $settings = null): IRoute
@@ -425,16 +418,9 @@ class SimpleRouter
*/
public static function error(Closure $callback): CallbackExceptionHandler
{
$routes = static::router()->getRoutes();
$callbackHandler = new CallbackExceptionHandler($callback);
$group = new RouteGroup();
$group->addExceptionHandler($callbackHandler);
array_unshift($routes, $group);
static::router()->setRoutes($routes);
static::router()->addExceptionHandler($callbackHandler);
return $callbackHandler;
}
@@ -506,7 +492,7 @@ class SimpleRouter
/**
* Prepends the default namespace to all new routes added.
*
* @param IRoute $route
* @param ILoadableRoute|IRoute $route
* @return IRoute
*/
public static function addDefaultNamespace(IRoute $route): IRoute
@@ -16,11 +16,11 @@ class CustomClassLoader implements \Pecee\SimpleRouter\ClassLoader\IClassLoader
*/
public function loadClassMethod($class, string $method, array $parameters)
{
return call_user_func_array([$class, $method], ['result' => true]);
return call_user_func_array([$class, $method], [true]);
}
public function loadClosure(callable $closure, array $parameters)
{
return call_user_func_array($closure, ['result' => true]);
return call_user_func_array($closure, [true]);
}
}
@@ -0,0 +1,11 @@
<?php
namespace MyNamespace;
class NSController {
public function method()
{
return true;
}
}
@@ -19,10 +19,29 @@ class RouterCallbackExceptionHandlerTest extends \PHPUnit\Framework\TestCase
throw new ExceptionHandlerException();
});
TestRouter::debugNoReset('/404-url', 'get');
TestRouter::router()->reset();
TestRouter::debug('/404-url');
}
$this->assertTrue(true);
public function testExceptionHandlerCallback() {
TestRouter::group(['prefix' => null], function() {
TestRouter::get('/', function() {
return 'Hello world';
});
TestRouter::get('/not-found', 'DummyController@method1');
TestRouter::error(function(\Pecee\Http\Request $request, \Exception $exception) {
if($exception instanceof \Pecee\SimpleRouter\Exceptions\NotFoundHttpException && $exception->getCode() === 404) {
return $request->setRewriteCallback(static function() {
return 'success';
});
}
});
});
$result = TestRouter::debugOutput('/thisdoes-not/existssss', 'get');
$this->assertEquals('success', $result);
}
}
@@ -2,6 +2,7 @@
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/NSController.php';
require_once 'Dummy/Exception/ExceptionHandlerException.php';
class RouterRouteTest extends \PHPUnit\Framework\TestCase
@@ -246,6 +247,16 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
$this->assertTrue(true);
}
public function testDefaultNameSpaceOverload()
{
TestRouter::setDefaultNamespace('DefaultNamespace\\Controllers');
TestRouter::get('/test', [\MyNamespace\NSController::class, 'method']);
$result = TestRouter::debugOutput('/test');
$this->assertTrue( (bool)$result);
}
public function testSameRoutes()
{
TestRouter::get('/recipe', 'DummyController@method1')->name('add');
@@ -181,6 +181,22 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
$output = TestRouter::debugOutput('/admin/asd/bec/123', 'get');
$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()
+7 -5
View File
@@ -8,7 +8,7 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
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();
@@ -18,22 +18,24 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
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 {
static::debugNoReset($testUrl, $testMethod);
} catch(\Exception $e) {
} catch (\Exception $e) {
static::$defaultNamespace = null;
static::router()->reset();
throw $e;
}
if($reset === true) {
if ($reset === true) {
static::$defaultNamespace = null;
static::router()->reset();
}
}
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;