mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
Compare commits
67 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e34fe47a04 | |||
| f0a4b6e46f | |||
| e38a406957 | |||
| b82e29c864 | |||
| 9c79901316 | |||
| fbc87cc9bd | |||
| 301c2cfe4a | |||
| 01bad94af0 | |||
| a1d5f38af7 | |||
| b5e42dbdfb | |||
| 06a63eb0e7 | |||
| 5268a998ff | |||
| 9fa7ad3e91 | |||
| 0097725ef2 | |||
| 749f252ffb | |||
| 032a2ae7e0 | |||
| c2e2d3bb5d | |||
| 5dd0690009 | |||
| dbcf8f19a3 | |||
| ee61eda1e8 | |||
| 471bbe137f | |||
| b08dea9da5 | |||
| b17ba06a8c | |||
| 69494265a5 | |||
| 0d8915b206 | |||
| b54a25804a | |||
| e3145cc1ec | |||
| 4b8dbdc9e5 | |||
| 7fe66ac938 | |||
| 75ea58dd9c | |||
| e5552a88cf | |||
| 7d80517c2f | |||
| b3c135c723 | |||
| 470000ad05 | |||
| e990b95c50 | |||
| a35400b7a0 | |||
| 22606dfc12 | |||
| 5cd6cab801 | |||
| 3ffe9c8c07 | |||
| 319ce7a569 | |||
| 4dff4006bf | |||
| eea30d0f59 | |||
| ece9d30905 | |||
| 44c2b99513 | |||
| d4de7fc3df | |||
| 03ef9dfb74 | |||
| 4c5f825c97 | |||
| 8b9e43c99e | |||
| b7c31ae434 | |||
| 0c329e4c5b | |||
| e057a76153 | |||
| f7f1f1e3de | |||
| c4a9918048 | |||
| f93621fa02 | |||
| 5ab8826bfb | |||
| f863f931d8 | |||
| 0d6326dfbb | |||
| 63a9ec65cf | |||
| 448a423d5d | |||
| df9a855579 | |||
| d6bd9bbd72 | |||
| 14d3577a6a | |||
| 869c65f347 | |||
| 4fc48b4420 | |||
| bef3207fcd | |||
| d7bdee1092 | |||
| 8c5ed8410a |
@@ -62,6 +62,7 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c
|
||||
- [ExceptionHandlers](#exceptionhandlers)
|
||||
- [Handling 404, 403 and other errors](#handling-404-403-and-other-errors)
|
||||
- [Using custom exception handlers](#using-custom-exception-handlers)
|
||||
- [Prevent merge of parent exception-handlers](#prevent-merge-of-parent-exception-handlers)
|
||||
- [Urls](#urls)
|
||||
- [Get the current url](#get-the-current-url)
|
||||
- [Get by name (single route)](#get-by-name-single-route)
|
||||
@@ -77,6 +78,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 +792,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 +808,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 +894,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 +939,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 +947,37 @@ 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');
|
||||
```
|
||||
|
||||
If you will set the correct status for the browser error use:
|
||||
|
||||
```php
|
||||
SimpleRouter::response()->httpCode(404);
|
||||
```
|
||||
|
||||
## 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).
|
||||
@@ -999,6 +1021,41 @@ class CustomExceptionHandler implements IExceptionHandler
|
||||
}
|
||||
```
|
||||
|
||||
You can add your custom exception-handler class to your group by using the `exceptionHandler` settings-attribute.
|
||||
`exceptionHandler` can be either class-name or array of class-names.
|
||||
|
||||
```php
|
||||
SimpleRouter::group(['exceptionHandler' => \Demo\Handlers\CustomExceptionHandler::class], function() {
|
||||
|
||||
// Your routes here
|
||||
|
||||
});
|
||||
```
|
||||
|
||||
### Prevent merge of parent exception-handlers
|
||||
|
||||
By default the router will merge exception-handlers to any handlers provided by parent groups, and will be executed in the order of newest to oldest.
|
||||
|
||||
If you want your groups exception handler to be executed independently, you can add the `mergeExceptionHandlers` attribute and set it to `false`.
|
||||
|
||||
```php
|
||||
SimpleRouter::group(['prefix' => '/', 'exceptionHandler' => \Demo\Handlers\FirstExceptionHandler::class, 'mergeExceptionHandlers' => false], function() {
|
||||
|
||||
SimpleRouter::group(['prefix' => '/admin', 'exceptionHandler' => \Demo\Handlers\SecondExceptionHandler::class], function() {
|
||||
|
||||
// Both SecondExceptionHandler and FirstExceptionHandler will trigger (in that order).
|
||||
|
||||
});
|
||||
|
||||
SimpleRouter::group(['prefix' => '/user', 'exceptionHandler' => \Demo\Handlers\SecondExceptionHandler::class, 'mergeExceptionHandlers' => false], function() {
|
||||
|
||||
// Only SecondExceptionHandler will trigger.
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Urls
|
||||
@@ -1236,8 +1293,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 +1313,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 +1353,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 +1536,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.
|
||||
@@ -2001,4 +2079,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
SOFTWARE.
|
||||
|
||||
+7
-3
@@ -27,12 +27,16 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.1",
|
||||
"php": ">=7.4",
|
||||
"ext-json": "*"
|
||||
},
|
||||
"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/"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
@@ -216,11 +243,11 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* Get upload-error code.
|
||||
*
|
||||
* @return int
|
||||
* @return int|null
|
||||
*/
|
||||
public function getError(): int
|
||||
public function getError(): ?int
|
||||
{
|
||||
return (int)$this->errors;
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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): ?self
|
||||
{
|
||||
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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -129,7 +129,12 @@ class Request
|
||||
$this->setHost($this->getHeader('http-host'));
|
||||
|
||||
// Check if special IIS header exist, otherwise use default.
|
||||
$this->setUrl(new Url($this->getFirstHeader(['unencoded-url', 'request-uri'])));
|
||||
$url = $this->getHeader('unencoded-url');
|
||||
if($url !== null){
|
||||
$this->setUrl(new Url($url));
|
||||
}else{
|
||||
$this->setUrl(new Url(urldecode($this->getHeader('request-uri'))));
|
||||
}
|
||||
$this->setContentType((string)$this->getHeader('content-type'));
|
||||
$this->setMethod((string)($_POST[static::FORCE_METHOD_KEY] ?? $this->getHeader('request-method')));
|
||||
$this->inputHandler = new InputHandler($this);
|
||||
@@ -137,7 +142,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 +269,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;
|
||||
|
||||
/**
|
||||
|
||||
+43
-8
@@ -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;
|
||||
|
||||
/**
|
||||
@@ -248,8 +282,9 @@ class Url implements JsonSerializable
|
||||
public function setQueryString(string $queryString): self
|
||||
{
|
||||
$params = [];
|
||||
parse_str($queryString, $params);
|
||||
|
||||
if(parse_str($queryString, $params) !== false) {
|
||||
if(count($params) > 0) {
|
||||
return $this->setParams($params);
|
||||
}
|
||||
|
||||
@@ -341,7 +376,7 @@ class Url implements JsonSerializable
|
||||
*/
|
||||
public function removeParams(...$names): self
|
||||
{
|
||||
$params = array_diff_key($this->getParams(), array_flip($names));
|
||||
$params = array_diff_key($this->getParams(), array_flip(...$names));
|
||||
$this->setParams($params);
|
||||
|
||||
return $this;
|
||||
@@ -386,7 +421,7 @@ class Url implements JsonSerializable
|
||||
{
|
||||
$encodedUrl = preg_replace_callback(
|
||||
'/[^:\/@?&=#]+/u',
|
||||
static function ($matches) {
|
||||
static function ($matches): string {
|
||||
return urlencode($matches[0]);
|
||||
},
|
||||
$url
|
||||
@@ -413,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) !== '');
|
||||
});
|
||||
}
|
||||
@@ -430,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 ?? '/';
|
||||
|
||||
@@ -450,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);
|
||||
}
|
||||
@@ -465,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
|
||||
|
||||
@@ -31,6 +31,21 @@ interface IGroupRoute extends IRoute
|
||||
*/
|
||||
public function setExceptionHandlers(array $handlers): self;
|
||||
|
||||
/**
|
||||
* Returns true if group should overwrite existing exception-handlers.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getMergeExceptionHandlers(): bool;
|
||||
|
||||
/**
|
||||
* When enabled group will overwrite any existing exception-handlers.
|
||||
*
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setMergeExceptionHandlers(bool $merge): self;
|
||||
|
||||
/**
|
||||
* Get exception-handlers for group
|
||||
*
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -119,7 +123,8 @@ abstract class Route implements IRoute
|
||||
);
|
||||
|
||||
// Ensures that host names/domains will work with parameters
|
||||
$url = '/' . ltrim($url, '/');
|
||||
|
||||
if($route[0] == '{') $url = '/' . ltrim($url, '/');
|
||||
$urlRegex = '';
|
||||
$parameters = [];
|
||||
|
||||
@@ -127,7 +132,7 @@ abstract class Route implements IRoute
|
||||
$urlRegex = preg_quote($route, '/');
|
||||
} else {
|
||||
|
||||
foreach (preg_split('/((-?\/?){[^}]+})/', $route) as $key => $t) {
|
||||
foreach (preg_split('/((\.?-?\/?){[^}]+})/', $route) as $key => $t) {
|
||||
|
||||
$regex = '';
|
||||
|
||||
@@ -142,7 +147,7 @@ abstract class Route implements IRoute
|
||||
$regex = $parameterRegex ?? $this->defaultParameterRegex ?? static::PARAMETERS_DEFAULT_REGEX;
|
||||
}
|
||||
|
||||
$regex = sprintf('((\/|-)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex);
|
||||
$regex = sprintf('((\/|-|\.)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex);
|
||||
}
|
||||
|
||||
$urlRegex .= preg_quote($t, '/') . $regex;
|
||||
@@ -280,7 +285,7 @@ abstract class Route implements IRoute
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|callable
|
||||
* @return string|callable|null
|
||||
*/
|
||||
public function getCallback()
|
||||
{
|
||||
@@ -337,6 +342,22 @@ 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) {
|
||||
// Don't overwrite namespaces that starts with \
|
||||
if ($ns[0] !== '\\') {
|
||||
$namespace .= '\\' . $ns;
|
||||
} else {
|
||||
$namespace = $ns;
|
||||
}
|
||||
}
|
||||
|
||||
$this->namespace = $namespace;
|
||||
|
||||
return $this;
|
||||
@@ -407,7 +428,7 @@ abstract class Route implements IRoute
|
||||
*/
|
||||
public function setSettings(array $settings, bool $merge = false): IRoute
|
||||
{
|
||||
if ($this->namespace === null && isset($settings['namespace']) === true) {
|
||||
if (isset($settings['namespace']) === true) {
|
||||
$this->setNamespace($settings['namespace']);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
protected $name;
|
||||
protected $domains = [];
|
||||
protected $exceptionHandlers = [];
|
||||
protected $mergeExceptionHandlers = true;
|
||||
|
||||
/**
|
||||
* Method called to check if a domain matches
|
||||
@@ -36,6 +37,7 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
|
||||
if ($parameters !== null && count($parameters) !== 0) {
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -76,7 +78,7 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
}
|
||||
|
||||
/* Skip if prefix doesn't match */
|
||||
if ($this->prefix !== null && stripos($url, $parsedPrefix) === false) {
|
||||
if ($this->prefix !== null && stripos($url, rtrim($parsedPrefix, '/') . '/') === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -174,6 +176,29 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
return $this->prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* When enabled group will overwrite any existing exception-handlers.
|
||||
*
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setMergeExceptionHandlers(bool $merge): IGroupRoute
|
||||
{
|
||||
$this->mergeExceptionHandlers = $merge;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if group should overwrite existing exception-handlers.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getMergeExceptionHandlers(): bool
|
||||
{
|
||||
return $this->mergeExceptionHandlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
@@ -187,6 +212,10 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
$this->setPrefix($settings['prefix'] . $this->prefix);
|
||||
}
|
||||
|
||||
if (isset($settings['mergeExceptionHandlers']) === true) {
|
||||
$this->setMergeExceptionHandlers($settings['mergeExceptionHandlers']);
|
||||
}
|
||||
|
||||
if ($merge === false && isset($settings['exceptionHandler']) === true) {
|
||||
$this->setExceptionHandlers((array)$settings['exceptionHandler']);
|
||||
}
|
||||
|
||||
@@ -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,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);
|
||||
|
||||
@@ -35,6 +35,12 @@ class Router
|
||||
* @var bool
|
||||
*/
|
||||
protected $isProcessingRoute;
|
||||
|
||||
/**
|
||||
* Defines all data from current processing route.
|
||||
* @var ILoadableRoute
|
||||
*/
|
||||
protected $currentProcessingRoute;
|
||||
|
||||
/**
|
||||
* All added routes
|
||||
@@ -44,7 +50,7 @@ class Router
|
||||
|
||||
/**
|
||||
* List of processed routes
|
||||
* @var array
|
||||
* @var array|ILoadableRoute[]
|
||||
*/
|
||||
protected $processedRoutes = [];
|
||||
|
||||
@@ -63,7 +69,7 @@ class Router
|
||||
|
||||
/**
|
||||
* Csrf verifier class
|
||||
* @var BaseCsrfVerifier
|
||||
* @var BaseCsrfVerifier|null
|
||||
*/
|
||||
protected $csrfVerifier;
|
||||
|
||||
@@ -107,7 +113,7 @@ class Router
|
||||
|
||||
/**
|
||||
* Class loader instance
|
||||
* @var ClassLoader
|
||||
* @var IClassLoader
|
||||
*/
|
||||
protected $classLoader;
|
||||
|
||||
@@ -160,7 +166,7 @@ class Router
|
||||
public function addRoute(IRoute $route): IRoute
|
||||
{
|
||||
$this->fireEvents(EventHandler::EVENT_ADD_ROUTE, [
|
||||
'route' => $route,
|
||||
'route' => $route,
|
||||
'isSubRoute' => $this->isProcessingRoute,
|
||||
]);
|
||||
|
||||
@@ -203,7 +209,7 @@ class Router
|
||||
/**
|
||||
* Process added routes.
|
||||
*
|
||||
* @param array $routes
|
||||
* @param array|IRoute[] $routes
|
||||
* @param IGroupRoute|null $group
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
@@ -211,11 +217,8 @@ class Router
|
||||
{
|
||||
$this->debug('Processing routes');
|
||||
|
||||
// Loop through each route-request
|
||||
$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;
|
||||
@@ -223,7 +226,7 @@ class Router
|
||||
|
||||
$url = $this->request->getRewriteUrl() ?? $this->request->getUrl()->getPath();
|
||||
|
||||
/* @var $route IRoute */
|
||||
// Loop through each route-request
|
||||
foreach ($routes as $route) {
|
||||
|
||||
$this->debug('Processing route "%s"', get_class($route));
|
||||
@@ -240,13 +243,22 @@ class Router
|
||||
|
||||
/* Add exception handlers */
|
||||
if (count($route->getExceptionHandlers()) !== 0) {
|
||||
/** @noinspection AdditionOperationOnArraysInspection */
|
||||
$exceptionHandlers += $route->getExceptionHandlers();
|
||||
|
||||
if ($route->getMergeExceptionHandlers() === true) {
|
||||
|
||||
foreach ($route->getExceptionHandlers() as $handler) {
|
||||
$this->exceptionHandlers[] = $handler;
|
||||
}
|
||||
|
||||
} else {
|
||||
$this->exceptionHandlers = $route->getExceptionHandlers();
|
||||
}
|
||||
}
|
||||
|
||||
/* Only render partial group if it matches */
|
||||
if ($route instanceof IPartialGroupRoute === true) {
|
||||
$this->renderAndProcess($route);
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -264,8 +276,6 @@ class Router
|
||||
$this->processedRoutes[] = $route;
|
||||
}
|
||||
}
|
||||
|
||||
$this->exceptionHandlers = array_merge($exceptionHandlers, $this->exceptionHandlers);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -367,6 +377,9 @@ class Router
|
||||
foreach ($this->processedRoutes as $key => $route) {
|
||||
|
||||
$this->debug('Matching route "%s"', get_class($route));
|
||||
|
||||
/* Add current processing route to constants */
|
||||
$this->currentProcessingRoute = $route;
|
||||
|
||||
/* If the route matches */
|
||||
if ($route->matchRoute($url, $this->request) === true) {
|
||||
@@ -509,7 +522,7 @@ class Router
|
||||
]);
|
||||
|
||||
/* @var $handler IExceptionHandler */
|
||||
foreach ($this->exceptionHandlers as $key => $handler) {
|
||||
foreach (array_reverse($this->exceptionHandlers) as $key => $handler) {
|
||||
|
||||
if (is_object($handler) === false) {
|
||||
$handler = new $handler();
|
||||
@@ -575,7 +588,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 +605,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 +617,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 +668,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('/');
|
||||
}
|
||||
@@ -684,7 +692,7 @@ class Router
|
||||
->setParams($getParams);
|
||||
}
|
||||
|
||||
if($name !== null) {
|
||||
if ($name !== null) {
|
||||
/* We try to find a match on the given name */
|
||||
$route = $this->findRoute($name);
|
||||
|
||||
@@ -703,21 +711,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 +850,7 @@ class Router
|
||||
/**
|
||||
* Get class loader
|
||||
*
|
||||
* @return ClassLoader
|
||||
* @return IClassLoader
|
||||
*/
|
||||
public function getClassLoader(): IClassLoader
|
||||
{
|
||||
@@ -928,6 +936,16 @@ class Router
|
||||
{
|
||||
return $this->debugList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current processing route details.
|
||||
*
|
||||
* @return ILoadableRoute
|
||||
*/
|
||||
public function getCurrentProcessingRoute(): ILoadableRoute
|
||||
{
|
||||
return $this->currentProcessingRoute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the rendering behavior of the router.
|
||||
@@ -944,4 +962,11 @@ class Router
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
public function addExceptionHandler(IExceptionHandler $handler): self
|
||||
{
|
||||
$this->exceptionHandlers[] = $handler;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -348,7 +341,7 @@ class SimpleRouter
|
||||
* @param array|null $settings
|
||||
* @return RouteUrl|IRoute
|
||||
*/
|
||||
public static function match(array $requestMethods, string $url, $callback, array $settings = null)
|
||||
public static function match(array $requestMethods, string $url, $callback, array $settings = null): IRoute
|
||||
{
|
||||
$route = new RouteUrl($url, $callback);
|
||||
$route->setRequestMethods($requestMethods);
|
||||
@@ -368,7 +361,7 @@ class SimpleRouter
|
||||
* @param array|null $settings
|
||||
* @return RouteUrl|IRoute
|
||||
*/
|
||||
public static function all(string $url, $callback, array $settings = null)
|
||||
public static function all(string $url, $callback, array $settings = null): IRoute
|
||||
{
|
||||
$route = new RouteUrl($url, $callback);
|
||||
|
||||
@@ -387,7 +380,7 @@ class SimpleRouter
|
||||
* @param array|null $settings
|
||||
* @return RouteController|IRoute
|
||||
*/
|
||||
public static function controller(string $url, string $controller, array $settings = null)
|
||||
public static function controller(string $url, string $controller, array $settings = null): IRoute
|
||||
{
|
||||
$route = new RouteController($url, $controller);
|
||||
|
||||
@@ -406,7 +399,7 @@ class SimpleRouter
|
||||
* @param array|null $settings
|
||||
* @return RouteResource|IRoute
|
||||
*/
|
||||
public static function resource(string $url, string $controller, array $settings = null)
|
||||
public static function resource(string $url, string $controller, array $settings = null): IRoute
|
||||
{
|
||||
$route = new RouteResource($url, $controller);
|
||||
|
||||
@@ -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,26 +492,13 @@ 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
|
||||
{
|
||||
if (static::$defaultNamespace !== null) {
|
||||
|
||||
$ns = static::$defaultNamespace;
|
||||
$namespace = $route->getNamespace();
|
||||
|
||||
if ($namespace !== null) {
|
||||
// Don't overwrite namespaces that starts with \
|
||||
if ($namespace[0] !== '\\') {
|
||||
$ns .= '\\' . $namespace;
|
||||
} else {
|
||||
$ns = $namespace;
|
||||
}
|
||||
}
|
||||
|
||||
$route->setNamespace($ns);
|
||||
$route->setNamespace(static::$defaultNamespace);
|
||||
}
|
||||
|
||||
return $route;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+39
-3
@@ -3,20 +3,20 @@
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
|
||||
class GroupTest extends \PHPUnit\Framework\TestCase
|
||||
class RouterGroupTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testGroupLoad()
|
||||
{
|
||||
$result = false;
|
||||
|
||||
TestRouter::group(['prefix' => '/group'], function () use(&$result) {
|
||||
TestRouter::group(['prefix' => '/group'], function () use (&$result) {
|
||||
$result = true;
|
||||
});
|
||||
|
||||
try {
|
||||
TestRouter::debug('/', 'get');
|
||||
} catch(\Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
$this->assertTrue($result);
|
||||
@@ -81,4 +81,40 @@ class GroupTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testNamespaceExtend()
|
||||
{
|
||||
TestRouter::group(['namespace' => '\My\Namespace'], function () use (&$result) {
|
||||
|
||||
TestRouter::group(['namespace' => 'Service'], function () use (&$result) {
|
||||
|
||||
TestRouter::get('/test', function () use (&$result) {
|
||||
return \Pecee\SimpleRouter\SimpleRouter::router()->getRequest()->getLoadedRoute()->getNamespace();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
$namespace = TestRouter::debugOutput('/test');
|
||||
$this->assertEquals('\My\Namespace\Service', $namespace);
|
||||
}
|
||||
|
||||
public function testNamespaceOverwrite()
|
||||
{
|
||||
TestRouter::group(['namespace' => '\My\Namespace'], function () use (&$result) {
|
||||
|
||||
TestRouter::group(['namespace' => '\Service'], function () use (&$result) {
|
||||
|
||||
TestRouter::get('/test', function () use (&$result) {
|
||||
return \Pecee\SimpleRouter\SimpleRouter::router()->getRequest()->getLoadedRoute()->getNamespace();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
$namespace = TestRouter::debugOutput('/test');
|
||||
$this->assertEquals('\Service', $namespace);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,9 +33,9 @@ class RouterRewriteTest extends \PHPUnit\Framework\TestCase
|
||||
global $stack;
|
||||
$stack = [];
|
||||
|
||||
TestRouter::group(['exceptionHandler' => [ExceptionHandlerFirst::class, ExceptionHandlerSecond::class]], function () use ($stack) {
|
||||
TestRouter::group(['exceptionHandler' => [ExceptionHandlerFirst::class, ExceptionHandlerSecond::class]], function () {
|
||||
|
||||
TestRouter::group(['exceptionHandler' => ExceptionHandlerThird::class], function () use ($stack) {
|
||||
TestRouter::group(['prefix' => '/test', 'exceptionHandler' => ExceptionHandlerThird::class], function () {
|
||||
|
||||
TestRouter::get('/my-path', 'DummyController@method1');
|
||||
|
||||
@@ -43,21 +43,48 @@ class RouterRewriteTest extends \PHPUnit\Framework\TestCase
|
||||
});
|
||||
|
||||
try {
|
||||
TestRouter::debug('/my-non-existing-path', 'get');
|
||||
TestRouter::debug('/test/non-existing', 'get');
|
||||
} catch (\ResponseException $e) {
|
||||
|
||||
}
|
||||
|
||||
$expectedStack = [
|
||||
ExceptionHandlerFirst::class,
|
||||
ExceptionHandlerSecond::class,
|
||||
ExceptionHandlerThird::class,
|
||||
ExceptionHandlerSecond::class,
|
||||
ExceptionHandlerFirst::class,
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedStack, $stack);
|
||||
|
||||
}
|
||||
|
||||
public function testStopMergeExceptionHandlers()
|
||||
{
|
||||
global $stack;
|
||||
$stack = [];
|
||||
|
||||
TestRouter::group(['prefix' => '/', 'exceptionHandler' => ExceptionHandlerFirst::class], function () {
|
||||
|
||||
TestRouter::group(['prefix' => '/admin', 'exceptionHandler' => ExceptionHandlerSecond::class, 'mergeExceptionHandlers' => false], function () {
|
||||
|
||||
TestRouter::get('/my-path', 'DummyController@method1');
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
TestRouter::debug('/admin/my-path-test', 'get');
|
||||
} catch (\Pecee\SimpleRouter\Exceptions\NotFoundHttpException $e) {
|
||||
|
||||
}
|
||||
|
||||
$expectedStack = [
|
||||
ExceptionHandlerSecond::class,
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedStack, $stack);
|
||||
}
|
||||
|
||||
public function testRewriteExceptionMessage()
|
||||
{
|
||||
$this->expectException(\Pecee\SimpleRouter\Exceptions\NotFoundHttpException::class);
|
||||
|
||||
@@ -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
|
||||
@@ -174,6 +175,70 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
TestRouter::debug('/test', 'get');
|
||||
|
||||
$this->assertFalse($result);
|
||||
|
||||
}
|
||||
|
||||
public function testFixedSubdomainDynamicDomain()
|
||||
{
|
||||
TestRouter::request()->setHost('other.world.com');
|
||||
|
||||
$result = false;
|
||||
|
||||
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
|
||||
TestRouter::get('/test', function ($domain = null) use (&$result) {
|
||||
|
||||
$result = true;
|
||||
});
|
||||
});
|
||||
|
||||
TestRouter::debug('/test', 'get');
|
||||
|
||||
$this->assertTrue($result);
|
||||
|
||||
}
|
||||
|
||||
public function testFixedSubdomainDynamicDomainParameter()
|
||||
{
|
||||
TestRouter::request()->setHost('other.world.com');
|
||||
|
||||
$result = false;
|
||||
|
||||
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
|
||||
TestRouter::get('/test', 'DummyController@param');
|
||||
TestRouter::get('/test/{key}', 'DummyController@param');
|
||||
});
|
||||
|
||||
$response = TestRouter::debugOutputNoReset('/test', 'get');
|
||||
|
||||
$this->assertEquals('world.com', $response);
|
||||
|
||||
$response = TestRouter::debugOutput('/test/unittest', 'get');
|
||||
|
||||
$this->assertEquals('unittest, world.com', $response);
|
||||
|
||||
}
|
||||
|
||||
public function testWrongFixedSubdomainDynamicDomain()
|
||||
{
|
||||
TestRouter::request()->setHost('wrong.world.com');
|
||||
|
||||
$result = false;
|
||||
|
||||
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
|
||||
TestRouter::get('/test', function ($domain = null) use (&$result) {
|
||||
|
||||
$result = true;
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
TestRouter::debug('/test', 'get');
|
||||
} catch(\Exception $e) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
$this->assertFalse($result);
|
||||
|
||||
}
|
||||
@@ -246,6 +311,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()
|
||||
|
||||
+20
-5
@@ -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;
|
||||
|
||||
@@ -46,4 +48,17 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function debugOutputNoReset(string $testUrl, string $testMethod = 'get', bool $reset = true): string
|
||||
{
|
||||
$response = null;
|
||||
|
||||
// Route request
|
||||
ob_start();
|
||||
static::debugNoReset($testUrl, $testMethod, $reset);
|
||||
$response = ob_get_clean();
|
||||
|
||||
// Return response
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user