Compare commits

...

78 Commits

Author SHA1 Message Date
Simon Sessingø 032a2ae7e0 Merge pull request #583 from skipperbent/v4-development
Version 4.3.7.2
2021-07-18 01:45:36 +02:00
Simon Sessingø c2e2d3bb5d Merge pull request #582 from skipperbent/v4-group-prefix-bug
Fixed issue causing group prefix to trigger on paths without "/" (issue #573 - thanks @Venloress)
2021-07-18 01:44:57 +02:00
Simon Sessingø 5dd0690009 Fixed issue causing group prefix to trigger on paths without "/" (issue #573 - thanks @Venloress). 2021-07-18 01:41:26 +02:00
Simon Sessingø ee61eda1e8 Merge pull request #580 from skipperbent/v4-development
Updated documentation
2021-07-17 22:00:22 +02:00
Simon Sessingø 471bbe137f Updated documentation 2021-07-17 21:59:23 +02:00
Simon Sessingø b17ba06a8c Merge pull request #578 from skipperbent/v4-development
Version 4.3.7.0
2021-07-17 21:56:47 +02:00
Simon Sessingø 69494265a5 Merge pull request #577 from skipperbent/v4-prevent-merge-attribute
Added group attribute to stop router from merging exception-handlers (issue: #573)
2021-07-17 21:54:07 +02:00
Simon Sessingø 0d8915b206 Fixed return type. 2021-07-17 21:52:00 +02:00
Simon Sessingø b54a25804a Added group attribute to stop router from merging exception-handlers (issue: #573)
- Added new mergeExceptionHandlers attribute to stop router from merging exception-handlers.
- RouteGroup: Added setMergeExceptionHandlers and getMergeExceptionHandlers methods.
- IRouteGroup: Added setMergeExceptionHandlers and getMergeExceptionHandlers method.
- Updated documentation to reflect changes.
- Added unit-tests.
2021-07-17 21:46:05 +02:00
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
Simon Sessingø 448a423d5d Merge pull request #544 from skipperbent/v4-development
Version 4.3.3.0
2021-05-02 14:00:10 +02:00
Simon Sessingø df9a855579 Merge pull request #543 from skipperbent/v4-bugfixes
Bugfixes
2021-05-02 13:58:00 +02:00
Simon Sessingø d6bd9bbd72 Development
- [FEATURE] Namespace overwrite now works globally. 'Service¨' will append namespace whereas '\Service' will overwrite it.
- [FEATURE] Exceptionhandlers are now rendered in reverse order from newest to oldest. This allows for exceptions to be handled from parent exceptions which were otherwise ignored.
- Fixed incorrect return type for InputFile::getError to nullable int.
- Added return type to all, match, controller and resource in SimpleRouter class.
- Fixed incorrect usage of parse_str in Url::setQueryString method.
- Fixed incorrect expected value in array_flip in Url::removeParams method.
2021-05-02 13:48:13 +02:00
Simon Sessingø 569b3a8760 Merge pull request #541 from skipperbent/v4-development
Version 4.3.2.3
2021-04-28 10:05:34 +02:00
Simon Sessingø 79a075ef49 Merge pull request #540 from skipperbent/v4-type-hint-notice
Fixed deprecated notice when using class type hinting (issue: #538)
2021-04-28 10:03:55 +02:00
Simon Sessingø c66d7f7df7 Fixed deprecated notice when using class type hinting (issue: #538) 2021-04-28 09:57:47 +02:00
Simon Sessingø c6d0ff3c0e Merge pull request #536 from skipperbent/v4-development
Version 4.3.2.2
2021-04-28 03:41:47 +02:00
Simon Sessingø 718d60c53b Removed return type from IResourceController as the return type can be mixed. 2021-04-04 11:21:26 +02:00
Simon Sessingø 2a573f27fe Merge pull request #534 from skipperbent/v4-development
Version 4.3.2.1
2021-04-01 03:16:29 +02:00
Simon Sessingø ecbb0825e0 Added include param parameter to Url::getAbsoluteUrl method. 2021-04-01 03:14:22 +02:00
Simon Sessingø b94dc4355f Optimisations 2021-04-01 03:11:05 +02:00
Simon Sessingø 52c6c226c0 [BUGFIX] Fixed issue with BaseCsrfVerifier matching urls against urls with parameters.
- Added optional $includeParams parameter to Url::getRelativeUrl method.
2021-04-01 03:04:32 +02:00
Simon Sessingø 982fb9fab4 Merge pull request #532 from skipperbent/v4-development
Version 4.3.2.0
2021-04-01 02:37:52 +02:00
Simon Sessingø ca8fbf2b27 Merge pull request #531 from skipperbent/v4-feature-ip
[FEATURE] IP restrict access
2021-04-01 02:35:40 +02:00
Simon Sessingø e4584a451d Improved phpDoc for prepend methods 2021-03-31 13:31:20 +02:00
Simon Sessingø 8b11377fe8 Fixed typo 2021-03-31 03:25:35 +02:00
Simon Sessingø eccda10169 Added prependPrefix to Group class & updated documentation. 2021-03-31 03:23:04 +02:00
Simon Sessingø d92d50ecdc Updated features in documentation 2021-03-31 03:08:01 +02:00
Simon Sessingø dca0389115 Merge branch 'v4-development' into v4-feature-ip 2021-03-31 03:00:16 +02:00
Simon Sessingø 7adb4e8597 Fixed wrong link for partial-groups 2021-03-31 02:40:24 +02:00
Simon Sessingø b0e4becbba Merge branch 'v4-development' of github.com:skipperbent/simple-php-router into v4-development 2021-03-31 02:36:48 +02:00
Simon Sessingø 3b8e92b406 Updated documentation table of contents 2021-03-31 02:36:11 +02:00
Simon Sessingø 0e393fdc5f Minor changes
- Added better description of partialGroups in the documentation.
- Added custom base path example in documentation.
- Added isSubRoute event parameter for EVENT_ADD_ROUTE.
- Removed deprecation phpDoc from partialGroup.
- Added unit-test for adding custom base path.
2021-03-31 02:31:56 +02:00
Simon Sessingø 56c73640b7 Merge pull request #530 from skipperbent/v4-feature-verifier
[FEATURE] Added include property to BaseCsrfVerifier + unit tests.
2021-03-31 01:02:05 +02:00
Simon Sessingø f91f280975 Added https scheme to Request::setUri (used when calling getAbsoluteUrl). 2021-03-30 21:13:06 +02:00
Simon Sessingø 40f9b72963 Updated documentation 2021-03-30 20:52:39 +02:00
Simon Sessingø 245b909ab6 Updated readme 2021-03-30 20:48:37 +02:00
Simon Sessingø 50b7129cab Changed name of IpBlockAccess to IpRestrictAccess & updated documentation. 2021-03-30 20:44:40 +02:00
Simon Sessingø 57047d23ea [FEATURE] Ip access block 2021-03-30 20:38:18 +02:00
Simon Sessingø a98b5ba842 Fixed unit-tests for CsrfVerifierTest 2021-03-30 18:54:45 +02:00
Simon Sessingø b3d28e9432 [FEATURE] Added include åproperty to BaseCsrfVerifier + unit tests. 2021-03-30 18:49:37 +02:00
Simon Sessingø d0c34255b5 Merge pull request #528 from skipperbent/v4-development
Version 4.3.1.0
2021-03-29 22:31:01 +02:00
Simon Sessingø 5a917a6905 Merge pull request #526 from skipperbent/v4-php8-fix
Version 4.3.1.0-test
2021-03-29 22:28:44 +02:00
Simon Sessingø be2d45f0ad Updated documentation 2021-03-29 22:24:02 +02:00
Simon Sessingø dd9a6eab7d [FEATURE] Added class + method loading to IClassLoader. 2021-03-29 22:15:55 +02:00
Simon Sessingø 5621ffc724 Updated documentation 2021-03-29 22:04:46 +02:00
Simon Sessingø 438193ef59 Added deprecated warning for RoutePartialGroup. 2021-03-29 21:59:30 +02:00
Simon Sessingø adc879bb13 [BUGFIX] Fixed InputHandler::find and InputHandler::value failing when using array methods. 2021-03-29 18:45:49 +02:00
Simon Sessingø 06ee78a48f Updated example for group parameters 2021-03-29 17:41:18 +02:00
Simon Sessingø 4b992f0a2f Merge pull request #527 from skipperbent/v4-feature-group-parameters
[FEATURE] Added support for parameters in group prefix.
2021-03-29 17:14:51 +02:00
Simon Sessingø c423172c23 Added deep parameters pass unit-test 2021-03-29 17:10:12 +02:00
Simon Sessingø d6d83ac5bd Parameters are now correctly passed on to sub-routes 2021-03-29 17:05:45 +02:00
Simon Sessingø b05bbccc28 [FEATURE] Added support for parameters in group prefix. 2021-03-29 16:56:37 +02:00
Simon Sessingø d6bc713e5b [CLEANUP] Added qualifier import. 2021-03-29 15:40:50 +02:00
Simon Sessingø 8eba5ab3d5 [FEATURE] php8 compatibility.
- Fixed possible error causing parameters not to be set properly when using partialGroup.
- Removed unused import reference.
- Added unit-tests.
2021-03-29 15:11:58 +02:00
53 changed files with 1418 additions and 417 deletions
+240 -73
View File
@@ -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,12 +78,15 @@ 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)
- [Custom EventHandlers](#custom-eventhandlers)
- [Advanced](#advanced)
- [Disable multiple route rendering](#disable-multiple-route-rendering)
- [Restrict access to IP](#restrict-access-to-ip)
- [Setting custom base path](#setting-custom-base-path)
- [Url rewriting](#url-rewriting)
- [Changing current route](#changing-current-route)
- [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically)
@@ -161,6 +165,8 @@ You can find the demo-project here: [https://github.com/skipperbent/simple-route
- Sub-domain routing
- Custom boot managers to rewrite urls to "nicer" ones.
- Input manager; easily manage `GET`, `POST` and `FILE` values.
- IP based restrictions.
- Easily extendable.
## Installation
@@ -467,7 +473,8 @@ SimpleRouter::get('/posts/{post}/comments/{comment}', function ($postId, $commen
});
```
**Note:** Route parameters are always encased within {} braces and should consist of alphabetic characters. Route parameters may not contain a - character. Use an underscore (_) instead.
**Note:** Route parameters are always encased within `{` `}` braces and should consist of alphabetic characters. Route parameters can only contain certain characters like `A-Z`, `a-z`, `0-9`, `-` and `_`.
If your route contain other characters, please see [Custom regex for matching parameters](#custom-regex-for-matching-parameters).
### Optional parameters
@@ -631,6 +638,23 @@ SimpleRouter::group(['namespace' => 'Admin'], function () {
});
```
You can add parameters to the prefixes of your routes.
Parameters from your previous routes will be injected
into your routes after any route-required parameters, starting from oldest to newest.
```php
SimpleRouter::group(['prefix' => '/lang/{lang}'], function ($language) {
SimpleRouter::get('/about', function($language) {
// Will match /lang/da/about
});
});
```
### Subdomain-routing
Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route urls, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the `domain` key on the group attribute array:
@@ -655,30 +679,37 @@ SimpleRouter::group(['prefix' => '/admin'], function () {
});
```
You can also use parameters in your groups:
```php
SimpleRouter::group(['prefix' => '/lang/{language}'], function ($language) {
SimpleRouter::get('/users', function ($language) {
// Matches The "/lang/da/users" URL
});
});
```
## Partial groups
Partial router groups has the same benefits as a normal group, but supports parameters and are only rendered once the url has matched.
Partial groups will render once a part of the url has matched.
Partial router groups has the same benefits as a normal group, but **are only rendered once the url has matched**
in contrast to a normal group which are always rendered in order to retrieve it's child routes.
Partial groups are therefore more like a hybrid of a traditional route with the benefits of a group.
This can be extremely useful in situations, where you only want special routes to be added, when a certain criteria or logic has been met.
This can be extremely useful in situations where you only want special routes to be added, but only when a certain criteria or logic has been met.
**NOTE:** Use partial groups with caution as routes added within are only rendered and available once the url of the partial-group has matched. This can cause `url()` not to find urls for the routes added within.
**NOTE:** Use partial groups with caution as routes added within are only rendered and available once the url of the partial-group has matched.
This can cause `url()` not to find urls for the routes added within before the partial-group has been matched and is rendered.
**Example:**
```php
SimpleRouter::partialGroup('/lang/{language}', function ($language) {
SimpleRouter::partialGroup('/plugin/{name}', function ($plugin) {
SimpleRouter::get('/', function($language) {
// Matches The "/lang/da" URL
});
// Add routes from plugin
});
```
## Form Method Spoofing
HTML forms do not support `PUT`, `PATCH` or `DELETE` actions. So, when defining `PUT`, `PATCH` or `DELETE` routes that are called from an HTML form, you will need to add a hidden `_method` field to the form. The value sent with the `_method` field will be used as the HTTP request method:
@@ -761,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
@@ -777,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.
@@ -863,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);
```
---
@@ -908,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.
@@ -916,17 +947,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).
@@ -970,6 +1015,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
@@ -1207,8 +1287,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:
@@ -1224,6 +1307,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.
@@ -1240,13 +1341,13 @@ All event callbacks will retrieve a `EventArgument` object as parameter. This ob
| `EVENT_ALL` | - | Fires when a event is triggered. |
| `EVENT_INIT` | - | Fires when router is initializing and before routes are loaded. |
| `EVENT_LOAD` | `loadedRoutes` | Fires when all routes has been loaded and rendered, just before the output is returned. |
| `EVENT_ADD_ROUTE` | `route` | Fires when route is added to the router. |
| `EVENT_ADD_ROUTE` | `route`<br>`isSubRoute` | Fires when route is added to the router. `isSubRoute` is true when sub-route is rendered. |
| `EVENT_REWRITE` | `rewriteUrl`<br>`rewriteRoute` | Fires when a url-rewrite is and just before the routes are re-initialized. |
| `EVENT_BOOT` | `bootmanagers` | Fires when the router is booting. This happens just before boot-managers are rendered and before any routes has been loaded. |
| `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. |
@@ -1368,6 +1469,74 @@ By default the router will try to execute all routes that matches a given url. T
This behavior can be easily disabled by setting `SimpleRouter::enableMultiRouteRendering(false)` in your `routes.php` file. This is the same behavior as version 3 and below.
## Restrict access to IP
You can white and/or blacklist access to IP's using the build in `IpRestrictAccess` middleware.
Create your own custom Middleware and extend the `IpRestrictAccess` class.
The `IpRestrictAccess` class contains two properties `ipBlacklist` and `ipWhitelist` that can be added to your middleware to change which IP's that have access to your routes.
You can use `*` to restrict access to a range of ips.
```php
use \Pecee\Http\Middleware\IpRestrictAccess;
class IpBlockerMiddleware extends IpRestrictAccess
{
protected $ipBlacklist = [
'5.5.5.5',
'8.8.*',
];
protected $ipWhitelist = [
'8.8.2.2',
];
}
```
You can add the middleware to multiple routes by adding your [middleware to a group](#middleware).
## Setting custom base path
Sometimes it can be useful to add a custom base path to all of the routes added.
This can easily be done by taking advantage of the [Event Handlers](#events) support of the project.
```php
$basePath = '/basepath';
$eventHandler = new EventHandler();
$eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function(EventArgument $event) use($basePath) {
$route = $event->route;
// Skip routes added by group as these will inherit the url
if(!$event->isSubRoute) {
return;
}
switch (true) {
case $route instanceof ILoadableRoute:
$route->prependUrl($basePath);
break;
case $route instanceof IGroupRoute:
$route->prependPrefix($basePath);
break;
}
});
SimpleRouter::addEventHandler($eventHandler);
```
In the example shown above, we create a new `EVENT_ADD_ROUTE` event that triggers, when a new route is added.
We skip all subroutes as these will inherit the url from their parent. Then, if the route is a group, we change the prefix
otherwise we change the url.
## Url rewriting
### Changing current route
@@ -1493,6 +1662,18 @@ class MyCustomClassLoader implements IClassLoader
return new $class();
}
/**
* Called when loading class method
* @param object $class
* @param string $method
* @param array $parameters
* @return object
*/
public function loadClassMethod($class, string $method, array $parameters)
{
return call_user_func_array([$class, $method], array_values($parameters));
}
/**
* Load closure
@@ -1503,7 +1684,7 @@ class MyCustomClassLoader implements IClassLoader
*/
public function loadClosure(Callable $closure, array $parameters)
{
return \call_user_func_array($closure, $parameters);
return \call_user_func_array($closure, array_values($parameters));
}
}
@@ -1520,6 +1701,8 @@ SimpleRouter::setCustomClassLoader(new MyCustomClassLoader());
php-di support was discontinued by version 4.3, however you can easily add it again by creating your own class-loader like the example below:
```php
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
class MyCustomClassLoader implements IClassLoader
{
@@ -1528,7 +1711,7 @@ class MyCustomClassLoader implements IClassLoader
public function __construct()
{
// Create our new php-di container
$container = (new \DI\ContainerBuilder())
$this->container = (new \DI\ContainerBuilder())
->useAutowiring(true)
->build();
}
@@ -1546,15 +1729,27 @@ class MyCustomClassLoader implements IClassLoader
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404);
}
if ($this->container !== null) {
try {
return $this->container->get($class);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
return new $class();
try {
return $this->container->get($class);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
/**
* Called when loading class method
* @param object $class
* @param string $method
* @param array $parameters
* @return object
*/
public function loadClassMethod($class, string $method, array $parameters)
{
try {
return $this->container->call([$class, $method], $parameters);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
/**
@@ -1564,19 +1759,14 @@ class MyCustomClassLoader implements IClassLoader
* @param array $parameters
* @return mixed
*/
public function loadClosure(Callable $closure, array $parameters)
public function loadClosure(callable $closure, array $parameters)
{
if ($this->container !== null) {
try {
return $this->container->call($closure, $parameters);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
return \call_user_func_array($closure, $parameters);
try {
return $this->container->call($closure, $parameters);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
}
```
@@ -1633,40 +1823,17 @@ You can read more about adding your own custom regular expression for matching p
### Multiple routes matches? Which one has the priority?
The router will match routes in the order they're added.
The router will match routes in the order they're added and will render multiple routes, if they match.
It's possible to render multiple routes.
If you want the router to stop when a route is matched, you simply return a value in your callback or stop the execution manually (using `response()->json()` etc.).
If you want the router to stop when a route is matched, you simply return a value in your callback or stop the execution manually (using `response()->json()` etc.) or simply by returning a result.
Any returned objects that implements the `__toString()` magic method will also prevent other routes from being rendered.
If you want the router only to execute one route per request, you can [disabling multiple route rendering](#disable-multiple-route-rendering).
### Using the router on sub-paths
Using the library on a sub-path like `localhost/project/` is not officially supported, however it is possible to get it working quite easily.
Add an event that appends your sub-path when a new loadable route is added.
**Example:**
```php
// ... your routes.php file
if($isRunningLocally) {
$eventHandler = new EventHandler();
$eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function (EventArgument $arg) use (&$status) {
if ($arg->route instanceof \Pecee\SimpleRouter\Route\LoadableRoute) {
$arg->route->prependUrl('/local-path');
}
});
TestRouter::addEventHandler($eventHandler);
}
```
Please refer to [Setting custom base path](#setting-custom-base-path) part of the documentation.
## Debugging
+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
+14 -14
View File
@@ -6,43 +6,43 @@ interface IResourceController
{
/**
* @return string|null
* @return mixed
*/
public function index(): ?string;
public function index();
/**
* @param mixed $id
* @return string|null
* @return mixed
*/
public function show($id): ?string;
public function show($id);
/**
* @return string|null
* @return mixed
*/
public function store(): ?string;
public function store();
/**
* @return string|null
* @return mixed
*/
public function create(): ?string;
public function create();
/**
* View
* @param mixed $id
* @return string|null
* @return mixed
*/
public function edit($id): ?string;
public function edit($id);
/**
* @param mixed $id
* @return string|null
* @return mixed
*/
public function update($id): ?string;
public function update($id);
/**
* @param mixed $id
* @return string|null
* @return mixed
*/
public function destroy($id): ?string;
public function destroy($id);
}
@@ -2,7 +2,9 @@
namespace Pecee\Http\Exceptions;
class MalformedUrlException extends \Exception
use Exception;
class MalformedUrlException extends Exception
{
}
+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;
+36 -9
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)
@@ -165,7 +192,7 @@ class InputFile implements IInputItem
* @param string $name
* @return static
*/
public function setFilename($name): IInputItem
public function setFilename(string $name): IInputItem
{
$this->filename = $name;
@@ -188,7 +215,7 @@ class InputFile implements IInputItem
* @param string $destination
* @return bool
*/
public function move($destination): bool
public function move(string $destination): bool
{
return move_uploaded_file($this->tmpName, $destination);
}
@@ -216,20 +243,20 @@ 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;
}
/**
* Set error
*
* @param int $error
* @param int|null $error
* @return static
*/
public function setError($error): IInputItem
public function setError(?int $error): IInputItem
{
$this->errors = (int)$error;
@@ -249,7 +276,7 @@ class InputFile implements IInputItem
* @param string $name
* @return static
*/
public function setTmpName($name): IInputItem
public function setTmpName(string $name): IInputItem
{
$this->tmpName = $name;
@@ -261,7 +288,7 @@ class InputFile implements IInputItem
return $this->getTmpName();
}
public function getValue()
public function getValue(): string
{
return $this->getFilename();
}
+43 -27
View File
@@ -63,7 +63,7 @@ class InputHandler
public function parseInputs(): void
{
/* Parse get requests */
if (\count($_GET) !== 0) {
if (count($_GET) !== 0) {
$this->originalParams = $_GET;
$this->get = $this->parseInputItem($this->originalParams);
}
@@ -85,12 +85,12 @@ class InputHandler
}
}
if (\count($this->originalPost) !== 0) {
if (count($this->originalPost) !== 0) {
$this->post = $this->parseInputItem($this->originalPost);
}
/* Parse get requests */
if (\count($_FILES) !== 0) {
if (count($_FILES) !== 0) {
$this->originalFile = $_FILES;
$this->file = $this->parseFiles($this->originalFile);
}
@@ -108,14 +108,14 @@ class InputHandler
foreach ($files as $key => $value) {
// Parse multi dept file array
if(isset($value['name']) === false && \is_array($value) === true) {
if(isset($value['name']) === false && is_array($value) === true) {
$list[$key] = $this->parseFiles($value, $key);
continue;
}
// Handle array input
if (\is_array($value['name']) === false) {
$values['index'] = $parentKey ?? $key;
if (is_array($value['name']) === false) {
$values = ['index' => $parentKey ?? $key];
try {
$list[$key] = InputFile::createFromArray($values + $value);
@@ -147,7 +147,7 @@ class InputHandler
* @param array|null $original
* @return array
*/
protected function rearrangeFile(array $values, &$index, $original): array
protected function rearrangeFile(array $values, array &$index, ?array $original): array
{
$originalIndex = $index[0];
array_shift($index);
@@ -156,12 +156,12 @@ class InputHandler
foreach ($values as $key => $value) {
if (\is_array($original['name'][$key]) === false) {
if (is_array($original['name'][$key]) === false) {
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],
@@ -210,7 +210,7 @@ class InputHandler
foreach ($array as $key => $value) {
// Handle array input
if (\is_array($value) === true) {
if (is_array($value) === true) {
$value = $this->parseInputItem($value);
}
@@ -231,15 +231,19 @@ class InputHandler
{
$element = null;
if (\count($methods) === 0 || \in_array(Request::REQUEST_TYPE_GET, $methods, true) === true) {
if(count($methods) > 0) {
$methods = is_array(...$methods) ? array_values(...$methods) : $methods;
}
if (count($methods) === 0 || in_array(Request::REQUEST_TYPE_GET, $methods, true) === true) {
$element = $this->get($index);
}
if (($element === null && \count($methods) === 0) || (\count($methods) !== 0 && \in_array(Request::REQUEST_TYPE_POST, $methods, true) === true)) {
if (($element === null && count($methods) === 0) || (count($methods) !== 0 && in_array(Request::REQUEST_TYPE_POST, $methods, true) === true)) {
$element = $this->post($index);
}
if (($element === null && \count($methods) === 0) || (\count($methods) !== 0 && \in_array('file', $methods, true) === true)) {
if (($element === null && count($methods) === 0) || (count($methods) !== 0 && in_array('file', $methods, true) === true)) {
$element = $this->file($index);
}
@@ -256,7 +260,7 @@ class InputHandler
$item = $item->getValue();
}
$output[$key] = \is_array($item) ? $this->getValueFromArray($item) : $item;
$output[$key] = is_array($item) ? $this->getValueFromArray($item) : $item;
}
return $output;
@@ -279,24 +283,36 @@ class InputHandler
}
/* Handle collection */
if (\is_array($input) === true) {
if (is_array($input) === true) {
$output = $this->getValueFromArray($input);
return (\count($output) === 0) ? $defaultValue : $output;
return (count($output) === 0) ? $defaultValue : $output;
}
return ($input === null || (\is_string($input) && trim($input) === '')) ? $defaultValue : $input;
return ($input === null || (is_string($input) && trim($input) === '')) ? $defaultValue : $input;
}
/**
* 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;
}
@@ -304,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;
}
@@ -316,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;
}
@@ -328,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;
}
@@ -344,7 +360,7 @@ class InputHandler
public function all(array $filter = []): array
{
$output = $this->originalParams + $this->originalPost + $this->originalFile;
$output = (\count($filter) > 0) ? \array_intersect_key($output, \array_flip($filter)) : $output;
$output = (count($filter) > 0) ? array_intersect_key($output, array_flip($filter)) : $output;
foreach ($filter as $filterKey) {
if (array_key_exists($filterKey, $output) === false) {
+33 -4
View File
@@ -2,7 +2,11 @@
namespace Pecee\Http\Input;
class InputItem implements IInputItem, \IteratorAggregate
use ArrayAccess;
use ArrayIterator;
use IteratorAggregate;
class InputItem implements ArrayAccess, IInputItem, IteratorAggregate
{
public $index;
public $name;
@@ -72,14 +76,39 @@ 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;
return (is_array($value) === true) ? json_encode($value) : $value;
}
public function getIterator(): \ArrayIterator
public function getIterator(): ArrayIterator
{
return new \ArrayIterator($this->getValue());
return new ArrayIterator($this->getValue());
}
}
+37 -8
View File
@@ -12,7 +12,22 @@ class BaseCsrfVerifier implements IMiddleware
public const POST_KEY = 'csrf_token';
public const HEADER_KEY = 'X-CSRF-TOKEN';
/**
* Urls to ignore. You can use * to exclude all sub-urls on a given path.
* For example: /admin/*
* @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;
/**
@@ -30,24 +45,38 @@ class BaseCsrfVerifier implements IMiddleware
*/
protected function skip(Request $request): bool
{
if ($this->except === null || \count($this->except) === 0) {
if ($this->except === null || count($this->except) === 0) {
return false;
}
$max = \count($this->except) - 1;
for ($i = $max; $i >= 0; $i--) {
$url = $this->except[$i];
foreach($this->except as $url) {
$url = rtrim($url, '/');
if ($url[\strlen($url) - 1] === '*') {
if ($url[strlen($url) - 1] === '*') {
$url = rtrim($url, '*');
$skip = $request->getUrl()->contains($url);
} else {
$skip = ($url === $request->getUrl()->getOriginalUrl());
$skip = ($url === rtrim($request->getUrl()->getRelativeUrl(false), '/'));
}
if ($skip === true) {
if(is_array($this->include) === true && count($this->include) > 0) {
foreach($this->include as $includeUrl) {
$includeUrl = rtrim($includeUrl, '/');
if ($includeUrl[strlen($includeUrl) - 1] === '*') {
$includeUrl = rtrim($includeUrl, '*');
$skip = !$request->getUrl()->contains($includeUrl);
break;
}
$skip = !($includeUrl === rtrim($request->getUrl()->getRelativeUrl(false), '/'));
}
}
if($skip === false) {
continue;
}
return true;
}
}
@@ -2,7 +2,9 @@
namespace Pecee\Http\Middleware\Exceptions;
class TokenMismatchException extends \Exception
use Exception;
class TokenMismatchException extends Exception
{
}
@@ -0,0 +1,47 @@
<?php
namespace Pecee\Http\Middleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\HttpException;
abstract class IpRestrictAccess implements IMiddleware
{
protected $ipBlacklist = [];
protected $ipWhitelist = [];
protected function validate(string $ip): bool
{
// Accept ip that is in white-list
if(in_array($ip, $this->ipWhitelist, true) === true) {
return true;
}
foreach ($this->ipBlacklist as $blackIp) {
// Blocks range (8.8.*)
if ($blackIp[strlen($blackIp) - 1] === '*' && strpos($ip, trim($blackIp, '*')) === 0) {
return false;
}
// Blocks exact match
if ($blackIp === $ip) {
return false;
}
}
return true;
}
/**
* @param Request $request
* @throws HttpException
*/
public function handle(Request $request): void
{
if($this->validate((string)$request->getIp()) === false) {
throw new HttpException(sprintf('Restricted ip. Access to %s has been blocked', $request->getIp()), 403);
}
}
}
+9 -5
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;
@@ -369,7 +369,7 @@ class Request
*/
public function isPostBack(): bool
{
return \in_array($this->getMethod(), static::$requestTypesPost, true);
return in_array($this->getMethod(), static::$requestTypesPost, true);
}
/**
@@ -391,6 +391,10 @@ class Request
if ($this->url->getHost() === null) {
$this->url->setHost((string)$this->getHost());
}
if($this->isSecure() === true) {
$this->url->setScheme('https');
}
}
/**
@@ -475,7 +479,7 @@ class Request
*/
public function getLoadedRoute(): ?ILoadableRoute
{
return (\count($this->loadedRoutes) > 0) ? end($this->loadedRoutes) : null;
return (count($this->loadedRoutes) > 0) ? end($this->loadedRoutes) : null;
}
/**
+3 -2
View File
@@ -2,6 +2,7 @@
namespace Pecee\Http;
use JsonSerializable;
use Pecee\Exceptions\InvalidArgumentException;
class Response
@@ -85,14 +86,14 @@ class Response
/**
* Json encode
* @param array|\JsonSerializable $value
* @param array|JsonSerializable $value
* @param ?int $options JSON options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
* @param int $dept JSON debt.
* @throws InvalidArgumentException
*/
public function json($value, ?int $options = null, int $dept = 512): void
{
if (($value instanceof \JsonSerializable) === false && \is_array($value) === false) {
if (($value instanceof JsonSerializable) === false && is_array($value) === false) {
throw new InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
}
@@ -2,13 +2,21 @@
namespace Pecee\Http\Security;
use Exception;
use Pecee\Http\Security\Exceptions\SecurityException;
class CookieTokenProvider implements ITokenProvider
{
public const CSRF_KEY = 'CSRF-TOKEN';
/**
* @var string
*/
protected $token;
/**
* @var int
*/
protected $cookieTimeoutMinutes = 120;
/**
@@ -34,7 +42,7 @@ class CookieTokenProvider implements ITokenProvider
{
try {
return bin2hex(random_bytes(32));
} catch (\Exception $e) {
} catch (Exception $e) {
throw new SecurityException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
@@ -2,7 +2,9 @@
namespace Pecee\Http\Security\Exceptions;
class SecurityException extends \Exception
use Exception;
class SecurityException extends Exception
{
}
+55 -14
View File
@@ -2,19 +2,54 @@
namespace Pecee\Http;
use JsonSerializable;
use Pecee\Http\Exceptions\MalformedUrlException;
class Url implements \JsonSerializable
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;
/**
@@ -247,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);
}
@@ -340,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;
@@ -385,7 +421,7 @@ class Url implements \JsonSerializable
{
$encodedUrl = preg_replace_callback(
'/[^:\/@?&=#]+/u',
static function ($matches) {
static function ($matches): string {
return urlencode($matches[0]);
},
$url
@@ -409,10 +445,10 @@ class Url implements \JsonSerializable
*/
public static function arrayToParams(array $getParams = [], bool $includeEmpty = true): string
{
if (\count($getParams) !== 0) {
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) !== '');
});
}
@@ -426,14 +462,18 @@ class Url implements \JsonSerializable
/**
* Returns the relative url
*
* @param bool $includeParams
* @return string
*/
public function getRelativeUrl(): string
public function getRelativeUrl(bool $includeParams = true): string
{
$params = $this->getQueryString();
$path = $this->path ?? '/';
$path = $this->path ?? '';
$query = $params !== '' ? '?' . $params : '';
if($includeParams === false) {
return $path;
}
$query = $this->getQueryString() !== '' ? '?' . $this->getQueryString() : '';
$fragment = $this->fragment !== null ? '#' . $this->fragment : '';
return $path . $query . $fragment;
@@ -442,24 +482,25 @@ class Url implements \JsonSerializable
/**
* Returns the absolute url
*
* @param bool $includeParams
* @return string
*/
public function getAbsoluteUrl(): 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();
return $scheme . $user . $pass . $host . $port . $this->getRelativeUrl($includeParams);
}
/**
* 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
*/
@@ -22,6 +22,18 @@ class ClassLoader implements IClassLoader
return new $class();
}
/**
* Called when loading class method
* @param object $class
* @param string $method
* @param array $parameters
* @return object
*/
public function loadClassMethod($class, string $method, array $parameters)
{
return call_user_func_array([$class, $method], array_values($parameters));
}
/**
* Load closure
*
@@ -31,7 +43,7 @@ class ClassLoader implements IClassLoader
*/
public function loadClosure(Callable $closure, array $parameters)
{
return \call_user_func_array($closure, $parameters);
return call_user_func_array($closure, array_values($parameters));
}
}
@@ -12,6 +12,15 @@ interface IClassLoader
*/
public function loadClass(string $class);
/**
* Called when loading class method
* @param object $class
* @param string $method
* @param array $parameters
* @return object
*/
public function loadClassMethod($class, string $method, array $parameters);
/**
* Called when loading method
*
@@ -21,4 +30,4 @@ interface IClassLoader
*/
public function loadClosure(Callable $closure, array $parameters);
}
}
@@ -2,6 +2,7 @@
namespace Pecee\SimpleRouter\Event;
use InvalidArgumentException;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Router;
@@ -23,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;
@@ -91,11 +92,11 @@ class EventArgument implements IEventArgument
/**
* @param string $name
* @param mixed $value
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __set(string $name, $value)
public function __set(string $name, $value): void
{
throw new \InvalidArgumentException('Not supported');
throw new InvalidArgumentException('Not supported');
}
/**
@@ -6,10 +6,17 @@ use Throwable;
class ClassNotFoundHttpException extends NotFoundHttpException
{
/**
* @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);
@@ -2,7 +2,9 @@
namespace Pecee\SimpleRouter\Exceptions;
class HttpException extends \Exception
use Exception;
class HttpException extends Exception
{
}
@@ -2,6 +2,8 @@
namespace Pecee\SimpleRouter\Handlers;
use Closure;
use Exception;
use Pecee\Http\Request;
/**
@@ -15,21 +17,24 @@ use Pecee\Http\Request;
class CallbackExceptionHandler implements IExceptionHandler
{
/**
* @var Closure
*/
protected $callback;
public function __construct(\Closure $callback)
public function __construct(Closure $callback)
{
$this->callback = $callback;
}
/**
* @param Request $request
* @param \Exception $error
* @param Exception $error
*/
public function handleError(Request $request, \Exception $error): void
public function handleError(Request $request, Exception $error): void
{
/* Fire exceptions */
\call_user_func($this->callback,
call_user_func($this->callback,
$request,
$error
);
@@ -2,6 +2,7 @@
namespace Pecee\SimpleRouter\Handlers;
use Closure;
use Pecee\SimpleRouter\Event\EventArgument;
use Pecee\SimpleRouter\Router;
@@ -10,13 +11,13 @@ class DebugEventHandler implements IEventHandler
/**
* Debug callback
* @var \Closure
* @var Closure
*/
protected $callback;
public function __construct()
{
$this->callback = static function (EventArgument $argument) {
$this->callback = static function (EventArgument $argument): void {
// todo: log in database
};
}
@@ -46,15 +47,15 @@ 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));
}
/**
* Set debug callback
*
* @param \Closure $event
* @param Closure $event
*/
public function setCallback(\Closure $event): void
public function setCallback(Closure $event): void
{
$this->callback = $event;
}
@@ -2,6 +2,7 @@
namespace Pecee\SimpleRouter\Handlers;
use Closure;
use Pecee\SimpleRouter\Event\EventArgument;
use Pecee\SimpleRouter\Router;
@@ -125,10 +126,10 @@ class EventHandler implements IEventHandler
* Register new event
*
* @param string $name
* @param \Closure $callback
* @param Closure $callback
* @return static
*/
public function register(string $name, \Closure $callback): IEventHandler
public function register(string $name, Closure $callback): IEventHandler
{
if (isset($this->registeredEvents[$name]) === true) {
$this->registeredEvents[$name][] = $callback;
@@ -175,7 +176,7 @@ class EventHandler implements IEventHandler
{
$events = $this->getEvents(static::EVENT_ALL, $name);
/* @var $event \Closure */
/* @var $event Closure */
foreach ($events as $event) {
$event(new EventArgument($name, $router, $eventArgs));
}
@@ -2,14 +2,15 @@
namespace Pecee\SimpleRouter\Handlers;
use Exception;
use Pecee\Http\Request;
interface IExceptionHandler
{
/**
* @param Request $request
* @param \Exception $error
* @param Exception $error
*/
public function handleError(Request $request, \Exception $error): void;
public function handleError(Request $request, Exception $error): void;
}
@@ -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
*
@@ -53,6 +68,14 @@ interface IGroupRoute extends IRoute
*/
public function setDomains(array $domains): self;
/**
* Prepends prefix while ensuring that the url has the correct formatting.
*
* @param string $url
* @return static
*/
public function prependPrefix(string $url): self;
/**
* Set prefix that child-routes will inherit.
*
@@ -40,7 +40,7 @@ interface ILoadableRoute extends IRoute
public function setUrl(string $url): self;
/**
* Prepend url
* Prepends url while ensuring that the url has the correct formatting.
* @param string $url
* @return ILoadableRoute
*/
+29 -8
View File
@@ -19,6 +19,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
*/
protected $name;
/**
* @var string|null
*/
protected $regex;
/**
@@ -34,7 +37,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
foreach ($this->getMiddlewares() as $middleware) {
if (\is_object($middleware) === false) {
if (is_object($middleware) === false) {
$middleware = $router->getClassLoader()->loadClass($middleware);
}
@@ -42,7 +45,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
throw new HttpException($middleware . ' must be inherit the IMiddleware interface');
}
$className = \get_class($middleware);
$className = get_class($middleware);
$router->debug('Loading middleware "%s"', $className);
$middleware->handle($request);
@@ -55,12 +58,18 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
public function matchRegex(Request $request, $url): ?bool
{
/* Match on custom defined regular expression */
if ($this->regex === null) {
return null;
}
return ((bool)preg_match($this->regex, $url) !== false);
$parameters = [];
if ((bool)preg_match($this->regex, $url, $parameters) !== false) {
$this->setParameters($parameters);
return true;
}
return false;
}
/**
@@ -86,7 +95,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
}
/**
* Prepend url
* Prepends url while ensuring that the url has the correct formatting.
*
* @param string $url
* @return ILoadableRoute
@@ -101,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.
@@ -116,7 +137,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
$group = $this->getGroup();
if ($group !== null && \count($group->getDomains()) !== 0) {
if ($group !== null && count($group->getDomains()) !== 0) {
$url = '//' . $group->getDomains()[0] . $url;
}
@@ -132,7 +153,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
foreach (array_keys($params) as $param) {
if ($parameters === '' || (\is_array($parameters) === true && \count($parameters) === 0)) {
if ($parameters === '' || (is_array($parameters) === true && count($parameters) === 0)) {
$value = '';
} else {
$p = (array)$parameters;
@@ -204,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
{
+58 -26
View File
@@ -2,7 +2,6 @@
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
@@ -31,6 +30,9 @@ abstract class Route implements IRoute
protected $urlRegex = '/^%s\/?$/u';
protected $group;
protected $parent;
/**
* @var string|callable|null
*/
protected $callback;
protected $defaultNamespace;
@@ -52,7 +54,7 @@ abstract class Route implements IRoute
*/
public function renderRoute(Request $request, Router $router): ?string
{
$router->debug('Starting rendering route "%s"', \get_class($this));
$router->debug('Starting rendering route "%s"', get_class($this));
$callback = $this->getCallback();
@@ -68,16 +70,22 @@ 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);
});
}
/* Render callback function */
if (\is_callable($callback) === true) {
if (is_callable($callback) === true) {
$router->debug('Executing callback');
/* Load class from type hinting */
if (is_array($callback) === true && isset($callback[0], $callback[1]) === true) {
$callback[0] = $router->getClassLoader()->loadClass($callback[0]);
}
/* When the callback is a function */
return $router->getClassLoader()->loadClosure($callback, $parameters);
}
@@ -98,9 +106,9 @@ abstract class Route implements IRoute
throw new ClassNotFoundHttpException($className, $method, sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404, null);
}
$router->debug('Executing callback');
$router->debug('Executing callback %s -> %s', $className, $method);
return \call_user_func_array([$class, $method], $parameters);
return $router->getClassLoader()->loadClassMethod($class, $method, $parameters);
}
protected function parseParameters($route, $url, $parameterRegex = null): ?array
@@ -127,7 +135,7 @@ abstract class Route implements IRoute
$regex = '';
if ($key < \count($parameters[1])) {
if ($key < count($parameters[1])) {
$name = $parameters[1][$key];
@@ -153,12 +161,27 @@ abstract class Route implements IRoute
if (isset($parameters[1]) === true) {
$groupParameters = $this->getGroup() !== null ? $this->getGroup()->getParameters() : [];
$lastParams = [];
/* Only take matched parameters with name */
foreach ((array)$parameters[1] as $name) {
// Ignore parent parameters
if (isset($groupParameters[$name]) === true) {
$lastParams[$name] = $matches[$name];
continue;
}
$values[$name] = (isset($matches[$name]) === true && $matches[$name] !== '') ? $matches[$name] : null;
}
$values = array_merge($values, $lastParams);
}
$this->originalParameters = $values;
return $values;
}
@@ -171,7 +194,7 @@ abstract class Route implements IRoute
*/
public function getIdentifier(): string
{
if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
return $this->callback;
}
@@ -261,7 +284,7 @@ abstract class Route implements IRoute
}
/**
* @return string|callable
* @return string|callable|null
*/
public function getCallback()
{
@@ -270,11 +293,11 @@ abstract class Route implements IRoute
public function getMethod(): ?string
{
if (\is_array($this->callback) === true && \count($this->callback) > 1) {
if (is_array($this->callback) === true && count($this->callback) > 1) {
return $this->callback[1];
}
if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback);
return $tmp[1];
@@ -285,11 +308,11 @@ abstract class Route implements IRoute
public function getClass(): ?string
{
if (\is_array($this->callback) === true && \count($this->callback) > 0) {
if (is_array($this->callback) === true && count($this->callback) > 0) {
return $this->callback[0];
}
if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback);
return $tmp[0];
@@ -318,6 +341,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;
@@ -360,15 +399,15 @@ abstract class Route implements IRoute
$values['namespace'] = $this->namespace;
}
if (\count($this->requestMethods) !== 0) {
if (count($this->requestMethods) !== 0) {
$values['method'] = $this->requestMethods;
}
if (\count($this->where) !== 0) {
if (count($this->where) !== 0) {
$values['where'] = $this->where;
}
if (\count($this->middlewares) !== 0) {
if (count($this->middlewares) !== 0) {
$values['middleware'] = $this->middlewares;
}
@@ -388,7 +427,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']);
}
@@ -462,7 +501,7 @@ abstract class Route implements IRoute
/* Sort the parameters after the user-defined param order, if any */
$parameters = [];
if (\count($this->originalParameters) !== 0) {
if (count($this->originalParameters) !== 0) {
$parameters = $this->originalParameters;
}
@@ -477,14 +516,6 @@ abstract class Route implements IRoute
*/
public function setParameters(array $parameters): IRoute
{
/*
* If this is the first time setting parameters we store them so we
* later can organize the array, in case somebody tried to sort the array.
*/
if (\count($parameters) !== 0 && \count($this->originalParameters) === 0) {
$this->originalParameters = $parameters;
}
$this->parameters = array_merge($this->parameters, $parameters);
return $this;
@@ -571,6 +602,7 @@ abstract class Route implements IRoute
public function setFilterEmptyParams(bool $enabled): IRoute
{
$this->filterEmptyParams = $enabled;
return $this;
}
@@ -35,7 +35,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
$method = substr($name, strrpos($name, '.') + 1);
$newName = substr($name, 0, strrpos($name, '.'));
if (\in_array($method, $this->names, true) === true && strtolower($this->name) === strtolower($newName)) {
if (in_array($method, $this->names, true) === true && strtolower($this->name) === strtolower($newName)) {
return true;
}
}
@@ -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;
}
}
@@ -77,7 +77,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
$group = $this->getGroup();
if ($group !== null && \count($group->getDomains()) !== 0) {
if ($group !== null && count($group->getDomains()) !== 0) {
$url .= '//' . $group->getDomains()[0];
}
@@ -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;
}
@@ -102,12 +102,12 @@ class RouteController extends LoadableRoute implements IControllerRoute
$strippedUrl = trim(str_ireplace($this->url, '/', $url), '/');
$path = explode('/', $strippedUrl);
if (\count($path) !== 0) {
if (count($path) !== 0) {
$method = (isset($path[0]) === false || trim($path[0]) === '') ? $this->defaultMethod : $path[0];
$this->method = $request->getMethod() . ucfirst($method);
$this->parameters = \array_slice($path, 1);
$this->parameters = array_slice($path, 1);
// Set callback
$this->setCallback([$this->controller, $this->method]);
+59 -8
View File
@@ -7,10 +7,12 @@ use Pecee\SimpleRouter\Handlers\IExceptionHandler;
class RouteGroup extends Route implements IGroupRoute
{
protected $urlRegex = '/^%s\/?/u';
protected $prefix;
protected $name;
protected $domains = [];
protected $exceptionHandlers = [];
protected $mergeExceptionHandlers = true;
/**
* Method called to check if a domain matches
@@ -20,7 +22,7 @@ class RouteGroup extends Route implements IGroupRoute
*/
public function matchDomain(Request $request): bool
{
if ($this->domains === null || \count($this->domains) === 0) {
if ($this->domains === null || count($this->domains) === 0) {
return true;
}
@@ -33,8 +35,9 @@ class RouteGroup extends Route implements IGroupRoute
$parameters = $this->parseParameters($domain, $request->getHost(), '.*');
if ($parameters !== null && \count($parameters) !== 0) {
if ($parameters !== null && count($parameters) !== 0) {
$this->parameters = $parameters;
return true;
}
}
@@ -55,16 +58,27 @@ class RouteGroup extends Route implements IGroupRoute
return false;
}
// Parse parameter
if ($this->prefix !== null) {
/* Parse parameters from current route */
$parameters = $this->parseParameters($this->prefix, $url);
$prefix = $this->prefix;
/* If no custom regular expression or parameters was found on this route, we stop */
if ($parameters === null) {
return false;
}
/* Set the parameters */
$this->setParameters($parameters);
}
$parsedPrefix = $this->prefix;
foreach ($this->getParameters() as $parameter => $value) {
$prefix = str_ireplace('{' . $parameter . '}', $value, $prefix);
$parsedPrefix = str_ireplace('{' . $parameter . '}', $value, $parsedPrefix);
}
/* Skip if prefix doesn't match */
if ($this->prefix !== null && stripos($url, $prefix) === false) {
if ($this->prefix !== null && stripos($url, rtrim($parsedPrefix, '/') . '/') === false) {
return false;
}
@@ -141,6 +155,17 @@ class RouteGroup extends Route implements IGroupRoute
return $this;
}
/**
* Prepends prefix while ensuring that the url has the correct formatting.
*
* @param string $url
* @return static
*/
public function prependPrefix(string $url): IGroupRoute
{
return $this->setPrefix(rtrim($url, '/') . $this->prefix);
}
/**
* Set prefix that child-routes will inherit.
*
@@ -151,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.
*
@@ -160,11 +208,14 @@ class RouteGroup extends Route implements IGroupRoute
*/
public function setSettings(array $settings, bool $merge = false): IRoute
{
if (isset($settings['prefix']) === true) {
$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']);
}
@@ -204,7 +255,7 @@ class RouteGroup extends Route implements IGroupRoute
$values['as'] = $this->name;
}
if (\count($this->parameters) !== 0) {
if (count($this->parameters) !== 0) {
$values['parameters'] = $this->parameters;
}
@@ -1,47 +1,7 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
class RoutePartialGroup extends RouteGroup implements IPartialGroupRoute
{
/**
* RoutePartialGroup constructor.
*/
public function __construct()
{
$this->urlRegex = '/^%s\/?/u';
}
/**
* Method called to check if route matches
*
* @param string $url
* @param Request $request
* @return bool
*/
public function matchRoute(string $url, Request $request): bool
{
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
if ($this->prefix !== null) {
/* Parse parameters from current route */
$parameters = $this->parseParameters($this->prefix, $url);
/* If no custom regular expression or parameters was found on this route, we stop */
if ($parameters === null) {
return false;
}
/* Set the parameters */
$this->setParameters($parameters);
}
return $this->matchDomain($request);
}
}
@@ -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;
}
@@ -120,7 +120,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
}
// Update
if ($id !== null && \in_array($method, [Request::REQUEST_TYPE_PATCH, Request::REQUEST_TYPE_PUT], true) === true) {
if ($id !== null && in_array($method, [Request::REQUEST_TYPE_PATCH, Request::REQUEST_TYPE_PUT], true) === true) {
return $this->call($this->methodNames['update']);
}
+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);
+58 -51
View File
@@ -2,6 +2,7 @@
namespace Pecee\SimpleRouter;
use Exception;
use Pecee\Exceptions\InvalidArgumentException;
use Pecee\Http\Exceptions\MalformedUrlException;
use Pecee\Http\Middleware\BaseCsrfVerifier;
@@ -43,7 +44,7 @@ class Router
/**
* List of processed routes
* @var array
* @var array|ILoadableRoute[]
*/
protected $processedRoutes = [];
@@ -62,7 +63,7 @@ class Router
/**
* Csrf verifier class
* @var BaseCsrfVerifier
* @var BaseCsrfVerifier|null
*/
protected $csrfVerifier;
@@ -106,7 +107,7 @@ class Router
/**
* Class loader instance
* @var ClassLoader
* @var IClassLoader
*/
protected $classLoader;
@@ -159,7 +160,8 @@ class Router
public function addRoute(IRoute $route): IRoute
{
$this->fireEvents(EventHandler::EVENT_ADD_ROUTE, [
'route' => $route,
'route' => $route,
'isSubRoute' => $this->isProcessingRoute,
]);
/*
@@ -183,12 +185,11 @@ class Router
*/
protected function renderAndProcess(IRoute $route): void
{
$this->isProcessingRoute = true;
$route->renderRoute($this->request, $this);
$this->isProcessingRoute = false;
if (\count($this->routeStack) !== 0) {
if (count($this->routeStack) !== 0) {
/* Pop and grab the routes added when executing group callback earlier */
$stack = $this->routeStack;
@@ -202,7 +203,7 @@ class Router
/**
* Process added routes.
*
* @param array $routes
* @param array|IRoute[] $routes
* @param IGroupRoute|null $group
* @throws NotFoundHttpException
*/
@@ -210,11 +211,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;
@@ -222,10 +220,10 @@ 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));
$this->debug('Processing route "%s"', get_class($route));
if ($group !== null) {
/* Add the parent group */
@@ -238,14 +236,23 @@ class Router
if ($route->matchRoute($url, $this->request) === true) {
/* Add exception handlers */
if (\count($route->getExceptionHandlers()) !== 0) {
/** @noinspection AdditionOperationOnArraysInspection */
$exceptionHandlers += $route->getExceptionHandlers();
if (count($route->getExceptionHandlers()) !== 0) {
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;
}
}
@@ -263,8 +270,6 @@ class Router
$this->processedRoutes[] = $route;
}
}
$this->exceptionHandlers = array_merge($exceptionHandlers, $this->exceptionHandlers);
}
/**
@@ -292,7 +297,7 @@ class Router
/* @var $manager IRouterBootManager */
foreach ($this->bootManagers as $manager) {
$className = \get_class($manager);
$className = get_class($manager);
$this->debug('Rendering bootmanager "%s"', $className);
$this->fireEvents(EventHandler::EVENT_RENDER_BOOTMANAGER, [
'bootmanagers' => $this->bootManagers,
@@ -315,7 +320,7 @@ class Router
* @throws NotFoundHttpException
* @throws \Pecee\Http\Middleware\Exceptions\TokenMismatchException
* @throws HttpException
* @throws \Exception
* @throws Exception
*/
public function start(): ?string
{
@@ -351,7 +356,7 @@ class Router
*
* @return string|null
* @throws HttpException
* @throws \Exception
* @throws Exception
*/
public function routeRequest(): ?string
{
@@ -365,7 +370,7 @@ class Router
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $key => $route) {
$this->debug('Matching route "%s"', \get_class($route));
$this->debug('Matching route "%s"', get_class($route));
/* If the route matches */
if ($route->matchRoute($url, $this->request) === true) {
@@ -375,7 +380,7 @@ class Router
]);
/* Check if request method matches */
if (\count($route->getRequestMethods()) !== 0 && \in_array($this->request->getMethod(), $route->getRequestMethods(), true) === false) {
if (count($route->getRequestMethods()) !== 0 && in_array($this->request->getMethod(), $route->getRequestMethods(), true) === false) {
$this->debug('Method "%s" not allowed', $this->request->getMethod());
// Only set method not allowed is not already set
@@ -425,7 +430,7 @@ class Router
}
}
} catch (\Exception $e) {
} catch (Exception $e) {
$this->handleException($e);
}
@@ -434,7 +439,7 @@ class Router
$this->handleException(new NotFoundHttpException($message, 403));
}
if (\count($this->request->getLoadedRoutes()) === 0) {
if (count($this->request->getLoadedRoutes()) === 0) {
$rewriteUrl = $this->request->getRewriteUrl();
@@ -459,7 +464,7 @@ class Router
* @param string $url
* @return string|null
* @throws HttpException
* @throws \Exception
* @throws Exception
*/
protected function handleRouteRewrite(string $key, string $url): ?string
{
@@ -493,14 +498,14 @@ class Router
}
/**
* @param \Exception $e
* @param Exception $e
* @return string|null
* @throws \Exception
* @throws Exception
* @throws HttpException
*/
protected function handleException(\Exception $e): ?string
protected function handleException(Exception $e): ?string
{
$this->debug('Starting exception handling for "%s"', \get_class($e));
$this->debug('Starting exception handling for "%s"', get_class($e));
$this->fireEvents(EventHandler::EVENT_LOAD_EXCEPTIONS, [
'exception' => $e,
@@ -508,9 +513,9 @@ class Router
]);
/* @var $handler IExceptionHandler */
foreach ($this->exceptionHandlers as $key => $handler) {
foreach (array_reverse($this->exceptionHandlers) as $key => $handler) {
if (\is_object($handler) === false) {
if (is_object($handler) === false) {
$handler = new $handler();
}
@@ -520,7 +525,7 @@ class Router
'exceptionHandlers' => $this->exceptionHandlers,
]);
$this->debug('Processing exception-handler "%s"', \get_class($handler));
$this->debug('Processing exception-handler "%s"', get_class($handler));
if (($handler instanceof IExceptionHandler) === false) {
throw new HttpException('Exception handler must implement the IExceptionHandler interface.', 500);
@@ -549,7 +554,7 @@ class Router
return $this->routeRequest();
}
} catch (\Exception $e) {
} catch (Exception $e) {
}
@@ -574,7 +579,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. */
@@ -592,7 +596,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())) {
@@ -604,7 +608,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)) {
@@ -647,7 +651,7 @@ class Router
*/
public function getUrl(?string $name = null, $parameters = null, ?array $getParams = null): Url
{
$this->debug('Finding url', \func_get_args());
$this->debug('Finding url', func_get_args());
$this->fireEvents(EventHandler::EVENT_GET_URL, [
'name' => $name,
@@ -655,10 +659,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('/');
}
@@ -683,7 +683,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);
@@ -696,27 +696,27 @@ class Router
}
/* Using @ is most definitely a controller@method or alias@method */
if (\is_string($name) === true && strpos($name, '@') !== false) {
if (is_string($name) === true && strpos($name, '@') !== false) {
[$controller, $method] = explode('@', $name);
/* 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);
}
@@ -841,7 +841,7 @@ class Router
/**
* Get class loader
*
* @return ClassLoader
* @return IClassLoader
*/
public function getClassLoader(): IClassLoader
{
@@ -876,7 +876,7 @@ class Router
*/
protected function fireEvents(string $name, array $arguments = []): void
{
if (\count($this->eventHandlers) === 0) {
if (count($this->eventHandlers) === 0) {
return;
}
@@ -943,4 +943,11 @@ class Router
return $this;
}
public function addExceptionHandler(IExceptionHandler $handler): self
{
$this->exceptionHandlers[] = $handler;
return $this;
}
}
+44 -69
View File
@@ -4,12 +4,14 @@
* Router helper class
* ---------------------------
*
* This class is added so calls can be made statically like Router::get() making the code look pretty.
* It also adds some extra functionality like default-namespace.
* This class is added so calls can be made statically like SimpleRouter::get() making the code look pretty.
* It also adds some extra functionality like default-namespace etc.
*/
namespace Pecee\SimpleRouter;
use Closure;
use Exception;
use Pecee\Exceptions\InvalidArgumentException;
use Pecee\Http\Middleware\BaseCsrfVerifier;
use Pecee\Http\Request;
@@ -20,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;
@@ -54,7 +57,7 @@ class SimpleRouter
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
* @throws \Pecee\Http\Middleware\Exceptions\TokenMismatchException
* @throws HttpException
* @throws \Exception
* @throws Exception
*/
public static function start(): void
{
@@ -79,18 +82,18 @@ class SimpleRouter
ob_start();
static::router()->setDebugEnabled(true)->start();
$routerOutput = ob_get_clean();
} catch (\Exception $e) {
} catch (Exception $e) {
}
// Try to parse library version
$composerFile = \dirname(__DIR__, 3) . '/composer.lock';
$composerFile = dirname(__DIR__, 3) . '/composer.lock';
$version = false;
if (is_file($composerFile) === true) {
$composerInfo = json_decode(file_get_contents($composerFile), true);
if (isset($composerInfo['packages']) === true && \is_array($composerInfo['packages']) === true) {
if (isset($composerInfo['packages']) === true && is_array($composerInfo['packages']) === true) {
foreach ($composerInfo['packages'] as $package) {
if (isset($package['name']) === true && strtolower($package['name']) === 'pecee/simple-router') {
$version = $package['version'];
@@ -171,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);
});
}
@@ -180,10 +183,10 @@ class SimpleRouter
* Route the given url to your callback on GET request method.
*
* @param string $url
* @param string|array|\Closure $callback
* @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
{
@@ -194,9 +197,9 @@ class SimpleRouter
* Route the given url to your callback on POST request method.
*
* @param string $url
* @param string|array|\Closure $callback
* @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
{
@@ -207,9 +210,9 @@ class SimpleRouter
* Route the given url to your callback on PUT request method.
*
* @param string $url
* @param string|array|\Closure $callback
* @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
{
@@ -220,9 +223,9 @@ class SimpleRouter
* Route the given url to your callback on PATCH request method.
*
* @param string $url
* @param string|array|\Closure $callback
* @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
{
@@ -233,9 +236,9 @@ class SimpleRouter
* Route the given url to your callback on OPTIONS request method.
*
* @param string $url
* @param string|array|\Closure $callback
* @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
{
@@ -246,9 +249,9 @@ class SimpleRouter
* Route the given url to your callback on DELETE request method.
*
* @param string $url
* @param string|array|\Closure $callback
* @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
{
@@ -259,16 +262,12 @@ class SimpleRouter
* Groups allows for encapsulating routes with special settings.
*
* @param array $settings
* @param \Closure $callback
* @return RouteGroup
* @param Closure $callback
* @return RouteGroup|IGroupRoute
* @throws InvalidArgumentException
*/
public static function group(array $settings, \Closure $callback): IGroupRoute
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);
@@ -283,17 +282,13 @@ class SimpleRouter
* parameters and which are only rendered when the url matches.
*
* @param string $url
* @param \Closure $callback
* @param Closure $callback
* @param array $settings
* @return RoutePartialGroup
* @return RoutePartialGroup|IPartialGroupRoute
* @throws InvalidArgumentException
*/
public static function partialGroup(string $url, \Closure $callback, array $settings = []): IPartialGroupRoute
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();
@@ -309,9 +304,9 @@ class SimpleRouter
* Alias for the form method
*
* @param string $url
* @param string|array|\Closure $callback
* @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
@@ -324,9 +319,9 @@ class SimpleRouter
* Route the given url to your callback on POST and GET request method.
*
* @param string $url
* @param string|array|\Closure $callback
* @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
@@ -342,11 +337,11 @@ class SimpleRouter
*
* @param array $requestMethods
* @param string $url
* @param string|array|\Closure $callback
* @param string|array|Closure $callback
* @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);
@@ -362,11 +357,11 @@ class SimpleRouter
* This type will route the given url to your callback and allow any type of request method
*
* @param string $url
* @param string|array|\Closure $callback
* @param string|array|Closure $callback
* @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);
@@ -385,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);
@@ -404,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);
@@ -418,21 +413,14 @@ class SimpleRouter
/**
* Add exception callback handler.
*
* @param \Closure $callback
* @param Closure $callback
* @return CallbackExceptionHandler $callbackHandler
*/
public static function error(\Closure $callback): CallbackExceptionHandler
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;
}
@@ -458,7 +446,7 @@ class SimpleRouter
{
try {
return static::router()->getUrl($name, $parameters, $getParams);
} catch (\Exception $e) {
} catch (Exception $e) {
return new Url('/');
}
}
@@ -504,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;
@@ -0,0 +1,66 @@
<?php
require_once 'Dummy/CsrfVerifier/DummyCsrfVerifier.php';
require_once 'Dummy/Security/SilentTokenProvider.php';
class CsrfVerifierTest extends \PHPUnit\Framework\TestCase
{
public function testTokenPass()
{
global $_POST;
$tokenProvider = new SilentTokenProvider();
$_POST[DummyCsrfVerifier::POST_KEY] = $tokenProvider->getToken();
TestRouter::router()->reset();
$router = TestRouter::router();
$router->getRequest()->setMethod(\Pecee\Http\Request::REQUEST_TYPE_POST);
$router->getRequest()->setUrl(new \Pecee\Http\Url('/page'));
$csrf = new DummyCsrfVerifier();
$csrf->setTokenProvider($tokenProvider);
$csrf->handle($router->getRequest());
// If handle doesn't throw exception, the test has passed
$this->assertTrue(true);
}
public function testTokenFail()
{
$this->expectException(\Pecee\Http\Middleware\Exceptions\TokenMismatchException::class);
global $_POST;
$tokenProvider = new SilentTokenProvider();
$router = TestRouter::router();
$router->getRequest()->setMethod(\Pecee\Http\Request::REQUEST_TYPE_POST);
$router->getRequest()->setUrl(new \Pecee\Http\Url('/page'));
$csrf = new DummyCsrfVerifier();
$csrf->setTokenProvider($tokenProvider);
$csrf->handle($router->getRequest());
}
public function testExcludeInclude()
{
$router = TestRouter::router();
$csrf = new DummyCsrfVerifier();
$request = $router->getRequest();
$request->setUrl(new \Pecee\Http\Url('/exclude-page'));
$this->assertTrue($csrf->testSkip($router->getRequest()));
$request->setUrl(new \Pecee\Http\Url('/exclude-all/page'));
$this->assertTrue($csrf->testSkip($router->getRequest()));
$request->setUrl(new \Pecee\Http\Url('/exclude-all/include-page'));
$this->assertFalse($csrf->testSkip($router->getRequest()));
$request->setUrl(new \Pecee\Http\Url('/include-page'));
$this->assertFalse($csrf->testSkip($router->getRequest()));
}
}
@@ -0,0 +1,71 @@
<?php
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Middleware/IpRestrictMiddleware.php';
class CustomMiddlewareTest extends \PHPUnit\Framework\TestCase
{
public function testIpBlock() {
$this->expectException(\Pecee\SimpleRouter\Exceptions\HttpException::class);
global $_SERVER;
// Test exact ip
$_SERVER['remote-addr'] = '5.5.5.5';
TestRouter::group(['middleware' => IpRestrictMiddleware::class], function() {
TestRouter::get('/fail', 'DummyController@method1');
});
TestRouter::debug('/fail');
// Test ip-range
$_SERVER['remote-addr'] = '8.8.4.4';
TestRouter::router()->reset();
TestRouter::group(['middleware' => IpRestrictMiddleware::class], function() {
TestRouter::get('/fail', 'DummyController@method1');
});
TestRouter::debug('/fail');
}
public function testIpSuccess() {
global $_SERVER;
// Test ip that is not blocked
$_SERVER['remote-addr'] = '6.6.6.6';
TestRouter::router()->reset();
TestRouter::group(['middleware' => IpRestrictMiddleware::class], function() {
TestRouter::get('/success', 'DummyController@method1');
});
TestRouter::debug('/success');
// Test ip in whitelist
$_SERVER['remote-addr'] = '8.8.2.2';
TestRouter::router()->reset();
TestRouter::group(['middleware' => IpRestrictMiddleware::class], function() {
TestRouter::get('/success', 'DummyController@method1');
});
TestRouter::debug('/success');
$this->assertTrue(true);
}
}
@@ -7,8 +7,20 @@ class CustomClassLoader implements \Pecee\SimpleRouter\ClassLoader\IClassLoader
return new DummyController();
}
/**
* Called when loading class method
* @param object $class
* @param string $method
* @param array $parameters
* @return object
*/
public function loadClassMethod($class, string $method, array $parameters)
{
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,18 @@
<?php
class DummyCsrfVerifier extends \Pecee\Http\Middleware\BaseCsrfVerifier {
protected $except = [
'/exclude-page',
'/exclude-all/*',
];
protected $include = [
'/exclude-all/include-page',
];
public function testSkip(\Pecee\Http\Request $request) {
return $this->skip($request);
}
}
@@ -0,0 +1,14 @@
<?php
class IpRestrictMiddleware extends \Pecee\Http\Middleware\IpRestrictAccess {
protected $ipBlacklist = [
'5.5.5.5',
'8.8.*',
];
protected $ipWhitelist = [
'8.8.2.2',
];
}
@@ -0,0 +1,11 @@
<?php
namespace MyNamespace;
class NSController {
public function method()
{
return true;
}
}
@@ -103,4 +103,49 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase
}
public function testCustomBasePath() {
$basePath = '/basepath/';
$eventHandler = new EventHandler();
$eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function(EventArgument $data) use($basePath) {
// Skip routes added by group
if($data->isSubRoute === false) {
switch (true) {
case $data->route instanceof \Pecee\SimpleRouter\Route\ILoadableRoute:
$data->route->prependUrl($basePath);
break;
case $data->route instanceof \Pecee\SimpleRouter\Route\IGroupRoute:
$data->route->prependPrefix($basePath);
break;
}
}
});
$results = [];
TestRouter::addEventHandler($eventHandler);
TestRouter::get('/about', function() use(&$results) {
$results[] = 'about';
});
TestRouter::group(['prefix' => '/admin'], function() use(&$results) {
TestRouter::get('/', function() use(&$results) {
$results[] = 'admin';
});
});
TestRouter::router()->setRenderMultipleRoutes(false);
TestRouter::debugNoReset('/basepath/about');
TestRouter::debugNoReset('/basepath/admin');
$this->assertEquals(['about', 'admin'], $results);
}
}
@@ -122,7 +122,20 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
$_GET = [];
}
public function testFindInput() {
global $_POST;
$_POST['hello'] = 'motto';
$router = TestRouter::router();
$router->reset();
$router->getRequest()->setMethod('post');
$inputHandler = TestRouter::request()->getInputHandler();
$value = $inputHandler->value('hello', null, \Pecee\Http\Request::$requestTypesPost);
$this->assertEquals($_POST['hello'], $value);
}
public function testFile()
{
@@ -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);
}
}
@@ -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);
}
}
@@ -70,4 +70,47 @@ class RouterPartialGroupTest extends \PHPUnit\Framework\TestCase
}
public function testPhp8CallUserFunc() {
TestRouter::router()->reset();
$result = false;
$lang = 'de';
TestRouter::group(['prefix' => '/lang'], function() use(&$result) {
TestRouter::get('/{lang}', function ($lang) use(&$result) {
$result = $lang;
});
});
TestRouter::debug("/lang/$lang");
$this->assertEquals($lang, $result);
// Test partial group
$lang = 'de';
$userId = 22;
$result1 = false;
$result2 = false;
TestRouter::partialGroup(
'/lang/{lang}/',
function ($lang) use(&$result1, &$result2) {
$result1 = $lang;
TestRouter::get('/user/{userId}', function ($userId) use(&$result2) {
$result2 = $userId;
});
});
TestRouter::debug("/lang/$lang/user/$userId");
$this->assertEquals($lang, $result1);
$this->assertEquals($userId, $result2);
}
}
+32 -5
View File
@@ -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
@@ -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()
@@ -271,4 +287,81 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
TestRouter::router()->reset();
}
public function testGroupPrefix() {
$result = false;
TestRouter::group(['prefix' => '/lang/{lang}'], function () use(&$result) {
TestRouter::get('/test', function() use(&$result) {
$result = true;
});
});
TestRouter::debug('/lang/da/test');
$this->assertTrue($result);
// Test group prefix sub-route
$result = null;
$expectedResult = 28;
TestRouter::group(['prefix' => '/lang/{lang}'], function () use(&$result) {
TestRouter::get('/horse/{horseType}', function($horseType) use(&$result) {
$result = false;
});
TestRouter::get('/user/{userId}', function($userId) use(&$result) {
$result = $userId;
});
});
TestRouter::debug("/lang/da/user/$expectedResult");
$this->assertEquals($expectedResult, $result);
}
public function testPassParameter() {
$result = false;
$expectedLanguage = 'da';
TestRouter::group(['prefix' => '/lang/{lang}'], function ($language) use(&$result) {
TestRouter::get('/test', function($language) use(&$result) {
$result = $language;
});
});
TestRouter::debug("/lang/$expectedLanguage/test");
$this->assertEquals($expectedLanguage, $result);
}
public function testPassParameterDeep() {
$result = false;
$expectedLanguage = 'da';
TestRouter::group(['prefix' => '/lang/{lang}'], function ($language) use(&$result) {
TestRouter::group(['prefix' => '/admin'], function($language) use(&$result) {
TestRouter::get('/test', function($language) use(&$result) {
$result = $language;
});
});
});
TestRouter::debug("/lang/$expectedLanguage/admin/test");
$this->assertEquals($expectedLanguage, $result);
}
}
+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;