Compare commits

..

16 Commits

Author SHA1 Message Date
Simon Sessingø c5c63671ef Merge pull request #383 from skipperbent/v3-development
Development
2018-03-03 23:44:20 +01:00
Simon Sessingo d279d5598d Development
- Fixed issue with subdomains on groups.
- Fixed issue with loadble routes sometimes mistakenly inheriting wrong group.
- Routes will now check if group matches before matching any custom regex.
- Added setValue method to IInputItem class to streamline the InputFile and InputItem classes.
- Other minor optimisations.
- Documentation: fixed broken links and cleaned it up.
2018-03-03 23:37:14 +01:00
Simon Sessingø 6e247f811f Merge pull request #381 from skipperbent/v3-development
Throw correct exception-types.
2018-02-27 09:04:57 +01:00
Simon Sessingo be39010be3 Throw correct exception-types. 2018-02-27 08:58:57 +01:00
Simon Sessingø 8111de48fd Merge pull request #378 from skipperbent/v3-development
Fixed issue with PDO exception not returning correct type for error-code.
2018-02-27 08:36:43 +01:00
Simon Sessingo 79c82c90cc Fixed issue with PDO exception not returning correct type for error-code. 2018-02-27 08:20:25 +01:00
Simon Sessingø 0dbc4e6ba2 Merge pull request #376 from skipperbent/v3-development
Optimisations
2018-02-27 00:20:12 +01:00
Simon Sessingo 6d7d07669b Optimisations 2018-02-27 00:19:44 +01:00
Simon Sessingø a4dfa59a66 Merge pull request #374 from skipperbent/v3-development
Stop router from processing routes if no valid route is found.
2018-02-27 00:14:24 +01:00
Simon Sessingo 98bf95bfc9 Added back getPath. 2018-02-27 00:14:01 +01:00
Simon Sessingo b051bcf02b Stop router from processing routes if no valid route is found. 2018-02-27 00:12:45 +01:00
Simon Sessingo b8d5106f4e Removed getPath from url. 2018-02-27 00:00:54 +01:00
Simon Sessingø 2c9d996437 Merge pull request #372 from skipperbent/v3-development
Fixed setUrl issue.
2018-02-26 23:48:45 +01:00
Simon Sessingo cfc9ac138a Fixed setUrl issue. 2018-02-26 23:48:09 +01:00
Simon Sessingø 98ce5f7635 Merge pull request #370 from skipperbent/v3-development
Development
2018-02-26 23:24:37 +01:00
Simon Sessingo a25be983b8 Development
- Renamed Uri class to Url.
- Renamed setUri and getUri to setUrl and getUrl.
- Added custom Exceptions and ensured that router only throws HttpExceptions.
- Added isAjax method to Request class.
- Added better phpDocs.
- Other minor optimisations.
2018-02-26 23:21:26 +01:00
26 changed files with 310 additions and 166 deletions
+47 -45
View File
@@ -1,45 +1,18 @@
# Simple PHP router
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expandability in mind.
**Note: this documentation is currently work-in-progress. Feel free to contribute.**
### Notes
The goal of this project is to create a router that is more or less 100% compatible with the Laravel documentation, while remaining as simple as possible, and as easy to integrate and change without compromising either speed or complexity. Being lightweight is the #1 priority.
### Feedback and development
If you are missing a feature, experience problems or have ideas or feedback that you want us to hear, please feel free to create an issue.
###### Issues guidelines
- Please be as detailed as possible in the description when creating a new issue. This will help others to more easily understand- and solve your issue.
For example: if you are experiencing issues, you should provide the necessary steps to reproduce the error within your description.
- We love to hear out any ideas or feedback to the library.
[Create a new issue here](https://github.com/skipperbent/simple-php-router/issues/new)
###### Contribution development guidelines
- Please try to follow the PSR-2 codestyle guidelines.
- Please create your pull requests to the development base that matches the version number you want to change.
For example when pushing changes to version 3, the pull request should use the `v3-development` base/branch.
- Create detailed descriptions for your commits, as these will be used in the changelog for new releases.
- When changing existing functionality, please ensure that the unit-tests working.
- When adding new stuff, please remember to add new unit-tests for the functionality.
**Please note that this documentation is currently work-in-progress. Feel free to contribute.**
---
## Table of Contents
- [Getting started](#getting-started)
- [Requirements](#requirements)
- [Notes](#notes-1)
- [Requirements](#requirements)
- [Feedback and development](#feedback-and-development)
- [Issues guidelines](#issues-guidelines)
- [Contribution development guidelines](#contribution-development-guidelines)
- [Features](#features)
- [Installation](#installation)
- [Setting up Apache](#setting-up-apache)
@@ -47,7 +20,6 @@ For example when pushing changes to version 3, the pull request should use the `
- [Setting up IIS](#setting-up-iis)
- [Configuration](#configuration)
- [Helper functions](#helper-functions)
- [Routes](#routes)
- [Basic routing](#basic-routing)
- [Available methods](#available-methods)
@@ -99,8 +71,7 @@ For example when pushing changes to version 3, the pull request should use the `
- [Advanced](#advanced)
- [Url rewriting](#url-rewriting)
- [Rewrite using callback](#rewrite-using-callback)
- [Rewrite using url](#rewrite-using-url)
- [Changing current route](#changing-current-route)
- [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically)
- [Adding routes manually](#adding-routes-manually)
- [Parameters](#parameters)
@@ -119,12 +90,10 @@ Add the latest version of Simple PHP Router running this command.
composer require pecee/simple-router
```
## Requirements
- PHP 5.5 or greater
## Notes
The goal of this project is to create a router that is more or less 100% compatible with the Laravel documentation, while remaining as simple as possible, and as easy to integrate and change without compromising either speed or complexity. Being lightweight is the #1 priority.
We've included a simple demo project for the router which can be found in the `demo-project` folder. This project should give you a basic understanding of how to setup and use simple-php-router project.
Please note that the demo-project only covers how to integrate the `simple-php-router` in a project without an existing framework. If you are using a framework in your project, the implementation might vary.
@@ -143,6 +112,36 @@ You can find the demo-project here: [https://github.com/skipperbent/simple-route
- How to get ExceptionHandlers, Middlewares and Controllers working.
- How to setup your webservers.
## Requirements
- PHP 5.5 or greater
### Feedback and development
If you are missing a feature, experience problems or have ideas or feedback that you want us to hear, please feel free to create an issue.
###### Issues guidelines
- Please be as detailed as possible in the description when creating a new issue. This will help others to more easily understand- and solve your issue.
For example: if you are experiencing issues, you should provide the necessary steps to reproduce the error within your description.
- We love to hear out any ideas or feedback to the library.
[Create a new issue here](https://github.com/skipperbent/simple-php-router/issues/new)
###### Contribution development guidelines
- Please try to follow the PSR-2 codestyle guidelines.
- Please create your pull requests to the development base that matches the version number you want to change.
For example when pushing changes to version 3, the pull request should use the `v3-development` base/branch.
- Create detailed descriptions for your commits, as these will be used in the changelog for new releases.
- When changing existing functionality, please ensure that the unit-tests working.
- When adding new stuff, please remember to add new unit-tests for the functionality.
## Features
- Basic routing (`GET`, `POST`, `PUT`, `PATCH`, `UPDATE`, `DELETE`) with support for custom multiple verbs.
@@ -598,9 +597,9 @@ SimpleRouter::group(['namespace' => 'Admin'], function () {
});
```
### Sub domain-routing
### Subdomain-routing
Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route URIs, 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:
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:
```php
SimpleRouter::group(['domain' => '{account}.myapp.com'], function () {
@@ -612,7 +611,7 @@ SimpleRouter::group(['domain' => '{account}.myapp.com'], function () {
### Route prefixes
The `prefix` group attribute may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with `admin`:
The `prefix` group attribute may be used to prefix each route in the group with a given url. For example, you may want to prefix all route urls within the group with `admin`:
```php
SimpleRouter::group(['prefix' => '/admin'], function () {
@@ -894,7 +893,7 @@ class CustomExceptionHandler implements IExceptionHandler
/* You can use the exception handler to format errors depending on the request and type. */
if (stripos($request->getUri()->getPath(), '/api') !== false) {
if (stripos($request->getUrl()->getPath(), '/api') !== false) {
response()->json([
'error' => $error->getMessage(),
@@ -1135,6 +1134,9 @@ $siteId = input('site_id', 2, ['post', 'get']);
# Advanced
## Url rewriting
### Changing current route
Sometimes it can be useful to manipulate the route about to be loaded.
simple-php-router allows you to easily manipulate and change the routes which are about to be rendered.
All information about the current route is stored in the `\Pecee\SimpleRouter\Router` instance's `loadedRoute` property.
@@ -1171,9 +1173,9 @@ class CustomRouterRules implement IRouterBootManager {
foreach($rewriteRules as $url => $rule) {
// If the current uri matches the url, we use our custom route
// If the current url matches the rewrite url, we use our custom route
if($request->getUri()->getPath() === $url) {
if($request->getUrl()->getPath() === $url) {
$request->setRewriteUrl($rule);
}
}
@@ -0,0 +1,6 @@
<?php
namespace Pecee\Exceptions;
class InvalidArgumentException extends \InvalidArgumentException {
}
@@ -0,0 +1,8 @@
<?php
namespace Pecee\Http\Exceptions;
class MalformedUrlException extends \Exception
{
}
+3
View File
@@ -1,4 +1,5 @@
<?php
namespace Pecee\Http\Input;
interface IInputItem
@@ -14,6 +15,8 @@ interface IInputItem
public function getValue();
public function setValue($value);
public function __toString();
}
+36 -14
View File
@@ -2,6 +2,7 @@
namespace Pecee\Http\Input;
use Pecee\Exceptions\InvalidArgumentException;
use Pecee\Http\Request;
class Input
@@ -26,6 +27,10 @@ class Input
*/
protected $request;
/**
* Input constructor.
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
@@ -33,6 +38,10 @@ class Input
$this->parseInputs();
}
/**
* Parse input values
*
*/
public function parseInputs()
{
/* Parse get requests */
@@ -57,6 +66,9 @@ class Input
}
}
/**
* @return array
*/
public function parseFiles()
{
$list = [];
@@ -66,7 +78,11 @@ class Input
// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $key;
$list[$key] = InputFile::createFromArray($values + $value);
try {
$list[$key] = InputFile::createFromArray($values + $value);
} catch(InvalidArgumentException $e ){
}
continue;
}
@@ -97,22 +113,28 @@ class Input
if (is_array($original['name'][$key]) === false) {
$file = InputFile::createFromArray([
'index' => (empty($key) === true && empty($originalIndex) === false) ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'tmp_name' => $original['tmp_name'][$key],
'type' => $original['type'][$key],
'size' => $original['size'][$key],
]);
try {
if (isset($output[$key]) === true) {
$output[$key][] = $file;
$file = InputFile::createFromArray([
'index' => (empty($key) === true && empty($originalIndex) === false) ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'tmp_name' => $original['tmp_name'][$key],
'type' => $original['type'][$key],
'size' => $original['size'][$key],
]);
if (isset($output[$key]) === true) {
$output[$key][] = $file;
continue;
}
$output[$key] = $file;
continue;
}
$output[$key] = $file;
continue;
} catch(InvalidArgumentException $e) {
}
}
$index[] = $key;
+15 -2
View File
@@ -2,6 +2,8 @@
namespace Pecee\Http\Input;
use Pecee\Exceptions\InvalidArgumentException;
class InputFile implements IInputItem
{
public $index;
@@ -26,13 +28,13 @@ class InputFile implements IInputItem
* Create from array
*
* @param array $values
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
* @return static
*/
public static function createFromArray(array $values)
{
if (isset($values['index']) === false) {
throw new \InvalidArgumentException('Index key is required');
throw new InvalidArgumentException('Index key is required');
}
/* Easy way of ensuring that all indexes-are set and not filling the screen with isset() */
@@ -264,6 +266,17 @@ class InputFile implements IInputItem
return $this->getFilename();
}
/**
* @param string $value
* @return static
*/
public function setValue($value)
{
$this->filename = $value;
return $this;
}
public function toArray()
{
return [
+1
View File
@@ -1,4 +1,5 @@
<?php
namespace Pecee\Http\Input;
class InputItem implements IInputItem
@@ -15,6 +15,10 @@ class BaseCsrfVerifier implements IMiddleware
protected $except;
protected $tokenProvider;
/**
* BaseCsrfVerifier constructor.
* @throws \Pecee\Http\Security\Exceptions\SecurityException
*/
public function __construct()
{
$this->tokenProvider = new CookieTokenProvider();
@@ -39,9 +43,9 @@ class BaseCsrfVerifier implements IMiddleware
$url = rtrim($url, '/');
if ($url[strlen($url) - 1] === '*') {
$url = rtrim($url, '*');
$skip = (stripos($request->getUri()->getPath(), $url) === 0);
$skip = (stripos($request->getUrl()->getOriginalUrl(), $url) === 0);
} else {
$skip = ($url === $request->getUri()->getPath());
$skip = ($url === $request->getUrl()->getOriginalUrl());
}
if ($skip === true) {
+24 -13
View File
@@ -12,7 +12,7 @@ class Request
private $data = [];
protected $headers;
protected $host;
protected $uri;
protected $url;
protected $method;
protected $input;
@@ -29,13 +29,17 @@ class Request
*/
protected $loadedRoute;
/**
* Request constructor.
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public function __construct()
{
$this->parseHeaders();
$this->setHost($this->getHeader('http-host'));
// Check if special IIS header exist, otherwise use default.
$this->setUri(new Uri($this->getHeader('unencoded-url', $this->getHeader('request-uri'))));
$this->setUrl($this->getHeader('unencoded-url', $this->getHeader('request-uri')));
$this->input = new Input($this);
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
@@ -58,11 +62,11 @@ class Request
}
/**
* @return Uri
* @return Url
*/
public function getUri()
public function getUrl()
{
return $this->uri;
return $this->url;
}
/**
@@ -188,6 +192,16 @@ class Request
return ($this->getHeader('http-accept') !== null && stripos($this->getHeader('http-accept'), $format) > -1);
}
/**
* Returns true if the request is made through Ajax
*
* @return bool
*/
public function isAjax()
{
return (strtolower($this->getHeader('http-x-requested-with')) === 'xmlhttprequest');
}
/**
* Get accept formats
* @return array
@@ -198,15 +212,12 @@ class Request
}
/**
* @param Uri|string $uri
* @param string|Url $url
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public function setUri($uri)
public function setUrl($url)
{
if (is_string($uri) === true) {
$uri = new Uri($uri);
}
$this->uri = $uri;
$this->url = ($url instanceof Url) ? $url : new Url($url);
}
/**
@@ -282,7 +293,7 @@ class Request
{
$this->hasRewrite = true;
return $this->setRewriteRoute(new RouteUrl($this->getUri()->getPath(), $callback));
return $this->setRewriteRoute(new RouteUrl($this->getUrl()->getPath(), $callback));
}
/**
+5 -3
View File
@@ -2,6 +2,8 @@
namespace Pecee\Http;
use Pecee\Exceptions\InvalidArgumentException;
class Response
{
protected $request;
@@ -42,7 +44,7 @@ class Response
public function refresh()
{
$this->redirect($this->request->getUri()->getOriginalUrl());
$this->redirect($this->request->getUrl()->getOriginalUrl());
}
/**
@@ -87,12 +89,12 @@ class Response
* @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
* @throws InvalidArgumentException
*/
public function json($value, $options = null, $dept = 512)
{
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.');
throw new InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
}
$this->header('Content-Type: application/json; charset=utf-8');
@@ -2,6 +2,8 @@
namespace Pecee\Http\Security;
use Pecee\Http\Security\Exceptions\SecurityException;
class CookieTokenProvider implements ITokenProvider
{
const CSRF_KEY = 'CSRF-TOKEN';
@@ -9,6 +11,10 @@ class CookieTokenProvider implements ITokenProvider
protected $token;
protected $cookieTimeoutMinutes = 120;
/**
* CookieTokenProvider constructor.
* @throws SecurityException
*/
public function __construct()
{
$this->token = $this->getToken();
@@ -21,20 +27,24 @@ class CookieTokenProvider implements ITokenProvider
/**
* Generate random identifier for CSRF token
*
* @throws \RuntimeException|\Exception
* @return string
* @throws SecurityException
*/
public function generateToken()
{
if (function_exists('random_bytes') === true) {
return bin2hex(random_bytes(32));
try {
return bin2hex(random_bytes(32));
} catch(\Exception $e) {
throw new SecurityException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
}
$isSourceStrong = false;
$random = openssl_random_pseudo_bytes(32, $isSourceStrong);
if ($isSourceStrong === false || $random === false) {
throw new \RuntimeException('IV generation failed');
throw new SecurityException('IV generation failed');
}
return $random;
@@ -0,0 +1,6 @@
<?php
namespace Pecee\Http\Security\Exceptions;
class SecurityException extends \Exception {
}
@@ -2,7 +2,9 @@
namespace Pecee\Http;
class Uri
use Pecee\Http\Exceptions\MalformedUrlException;
class Url
{
private $originalUrl;
private $data = [
@@ -16,6 +18,11 @@ class Uri
'fragment' => null,
];
/**
* Url constructor.
* @param string $url
* @throws MalformedUrlException
*/
public function __construct($url)
{
$this->originalUrl = $url;
@@ -129,7 +136,7 @@ class Uri
* UTF-8 aware parse_url() replacement.
* @param string $url
* @param int $component
* @throws \InvalidArgumentException
* @throws MalformedUrlException
* @return array
*/
public function parseUrl($url, $component = -1)
@@ -145,7 +152,7 @@ class Uri
$parts = parse_url($encodedUrl, $component);
if ($parts === false) {
throw new \InvalidArgumentException('Malformed URL: ' . $url);
throw new MalformedUrlException('Malformed URL: ' . $url);
}
return array_map('urldecode', $parts);
@@ -50,7 +50,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
return null;
}
return (preg_match($this->regex, $request->getHost() . $url) > 0);
return ((bool)preg_match($this->regex, $request->getHost() . $url) !== false);
}
/**
@@ -67,7 +67,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
if (preg_match_all('/' . $regex . '/u', $this->url, $matches) > 0) {
if ((bool)preg_match_all('/' . $regex . '/u', $this->url, $matches) !== false) {
$this->parameters = array_fill_keys($matches[1], null);
}
}
@@ -111,11 +111,8 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
/* Replace any {parameter} in the url with the correct value */
$params = $this->getParameters();
$max = count($params) - 1;
$keys = array_keys($params);
for ($i = $max; $i >= 0; $i--) {
$param = $keys[$i];
foreach (array_keys($params) as $param) {
if ($parameters === '' || (is_array($parameters) === true && count($parameters) === 0)) {
$value = '';
@@ -137,8 +134,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
}
}
$url = '/' . ltrim($url, '/');
$url .= implode('/', $unknownParams);
$url = '/' . ltrim($url, '/') . implode('/', $unknownParams);
return rtrim($url, '/') . '/';
}
+7 -5
View File
@@ -128,7 +128,9 @@ abstract class Route implements IRoute
// Ensures that hostnames/domains will work with parameters
$url = '/' . ltrim($url, '/');
if (preg_match_all('/' . $regex . '/u', $route, $parameters) !== 0) {
if ((bool)preg_match_all('/' . $regex . '/u', $route, $parameters) === false) {
$urlRegex = preg_quote($route, '/');
} else {
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/', $route);
@@ -154,7 +156,6 @@ abstract class Route implements IRoute
}
$regex = sprintf('(?:\/|\-)%1$s(?P<%2$s>%3$s)%1$s', $parameters[2][$key], $name, $regex);
}
$urlParts[$key] = preg_quote($t, '/') . $regex;
@@ -162,11 +163,9 @@ abstract class Route implements IRoute
$urlRegex = implode('', $urlParts);
} else {
$urlRegex = preg_quote($route, '/');
}
if (preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === 0) {
if ((bool)preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === false) {
return null;
}
@@ -250,6 +249,9 @@ abstract class Route implements IRoute
{
$this->group = $group;
/* Add/merge parent settings with child */
$this->setSettings($group->toArray(), true);
return $this;
}
@@ -88,6 +88,10 @@ class RouteController extends LoadableRoute implements IControllerRoute
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
@@ -48,6 +48,10 @@ class RouteGroup extends Route implements IGroupRoute
*/
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
/* Skip if prefix doesn't match */
if ($this->prefix !== null && stripos($url, $this->prefix) === false) {
return false;
@@ -17,6 +17,10 @@ class RoutePartialGroup extends RouteGroup implements IPartialGroupRoute
*/
public function matchRoute($url, Request $request)
{
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);
@@ -79,6 +79,10 @@ class RouteResource extends LoadableRoute implements IControllerRoute
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
@@ -14,6 +14,10 @@ class RouteUrl extends LoadableRoute
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
+58 -49
View File
@@ -2,6 +2,7 @@
namespace Pecee\SimpleRouter;
use Pecee\Exceptions\InvalidArgumentException;
use Pecee\Handlers\IExceptionHandler;
use Pecee\Http\Middleware\BaseCsrfVerifier;
use Pecee\Http\Request;
@@ -65,11 +66,18 @@ class Router
*/
protected $exceptionHandlers;
/**
* Router constructor.
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public function __construct()
{
$this->reset();
}
/**
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public function reset()
{
$this->processingRoute = false;
@@ -89,49 +97,64 @@ class Router
public function addRoute(IRoute $route)
{
/*
* If a route is currently being processed, that means that the
* route being added are rendered from the parent routes callback,
* so we add them to the stack instead.
* If a route is currently being processed, that means that the route being added are rendered from the parent
* routes callback, so we add them to the stack instead.
*/
if ($this->processingRoute === true) {
$this->routeStack[] = $route;
} else {
$this->routes[] = $route;
return $route;
}
$this->routes[] = $route;
return $route;
}
/**
* Render and process any new routes added.
*
* @param IRoute $route
* @throws NotFoundHttpException
*/
protected function renderAndProcess(IRoute $route) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if (count($this->routeStack) !== 0) {
/* Pop and grab the routes added when executing group callback earlier */
$stack = $this->routeStack;
$this->routeStack = [];
/* Route any routes added to the stack */
$this->processRoutes($stack, $route);
}
}
/**
* Process added routes.
*
* @param array $routes
* @param IGroupRoute|null $group
* @param IRoute|null $parent
* @throws NotFoundHttpException
*/
protected function processRoutes(array $routes, IGroupRoute $group = null, IRoute $parent = null)
protected function processRoutes(array $routes, IGroupRoute $group = null)
{
// Loop through each route-request
$exceptionHandlers = [];
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
// Stop processing routes if no valid route is found.
if($this->request->getRewriteRoute() === null && $this->request->getUrl() === null) {
return;
}
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUrl()->getPath();
/* @var $route IRoute */
foreach ($routes as $route) {
if ($parent !== null) {
/* Add the parent route */
$route->setParent($parent);
/* Add/merge parent settings with child */
$route->setSettings($parent->toArray(), true);
}
if ($group !== null) {
/* Add the parent group */
$route->setGroup($group);
}
@@ -139,8 +162,6 @@ class Router
/* @var $route IGroupRoute */
if ($route instanceof IGroupRoute) {
$group = $route;
if ($route->matchRoute($url, $this->request) === true) {
/* Add exception handlers */
@@ -150,36 +171,24 @@ class Router
}
/* Only render partial group if it matches */
if ($route instanceof IPartialGroupRoute) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route instanceof IPartialGroupRoute === true) {
$this->renderAndProcess($route);
}
}
if (($route instanceof IPartialGroupRoute) === false) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route instanceof IPartialGroupRoute === false) {
$this->renderAndProcess($route);
}
continue;
}
if ($route instanceof ILoadableRoute) {
if ($route instanceof ILoadableRoute === true) {
/* Add the route to the map, so we can find the active one when all routes has been loaded */
$this->processedRoutes[] = $route;
}
if (count($this->routeStack) !== 0) {
/* Pop and grab the routes added when executing group callback earlier */
$stack = $this->routeStack;
$this->routeStack = [];
/* Route any routes added to the stack */
$this->processRoutes($stack, $route, $group);
}
}
$this->exceptionHandlers = array_merge($exceptionHandlers, $this->exceptionHandlers);
@@ -228,7 +237,7 @@ class Router
$this->request->setHasRewrite(false);
}
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUrl()->getPath();
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $key => $route) {
@@ -274,7 +283,7 @@ class Router
}
if ($routeNotAllowed === true) {
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUri()->getPath(), $this->request->getMethod());
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUrl()->getPath(), $this->request->getMethod());
$this->handleException(new HttpException($message, 403));
}
@@ -283,9 +292,9 @@ class Router
$rewriteUrl = $this->request->getRewriteUrl();
if ($rewriteUrl !== null) {
$message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUri()->getPath());
$message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUrl()->getPath());
} else {
$message = sprintf('Route not found: "%s"', $this->request->getUri()->getPath());
$message = sprintf('Route not found: "%s"', $this->request->getUrl()->getPath());
}
$this->handleException(new NotFoundHttpException($message, 404));
@@ -436,13 +445,13 @@ class Router
* @param string|null $name
* @param string|array|null $parameters
* @param array|null $getParams
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
* @return string
*/
public function getUrl($name = null, $parameters = null, $getParams = null)
{
if ($getParams !== null && is_array($getParams) === false) {
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
throw new InvalidArgumentException('Invalid type for getParams. Must be array or null');
}
if ($name === '' && $parameters === '') {
@@ -458,7 +467,7 @@ class Router
/* Return current route if no options has been specified */
if ($name === null && $parameters === null) {
return $this->request->getUri()->getPath() . $this->arrayToParams($getParams);
return $this->request->getUrl()->getPath() . $this->arrayToParams($getParams);
}
$loadedRoute = $this->request->getLoadedRoute();
@@ -593,4 +602,4 @@ class Router
return $this;
}
}
}
+32 -10
View File
@@ -10,11 +10,11 @@
namespace Pecee\SimpleRouter;
use Pecee\Exceptions\InvalidArgumentException;
use Pecee\Handlers\CallbackExceptionHandler;
use Pecee\Http\Middleware\BaseCsrfVerifier;
use Pecee\Http\Response;
use Pecee\SimpleRouter\Exceptions\HttpException;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
use Pecee\SimpleRouter\Route\IRoute;
use Pecee\SimpleRouter\Route\RoutePartialGroup;
use Pecee\SimpleRouter\Route\RouteController;
@@ -43,9 +43,9 @@ class SimpleRouter
protected static $router;
/**
* Start/route request
*
* @throws HttpException|NotFoundHttpException|\Exception
* @throws \Pecee\Http\Exceptions\MalformedUrlException
* @throws HttpException
* @throws \Exception
*/
public static function start()
{
@@ -66,6 +66,7 @@ class SimpleRouter
* Base CSRF verifier
*
* @param BaseCsrfVerifier $baseCsrfVerifier
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function csrfVerifier(BaseCsrfVerifier $baseCsrfVerifier)
{
@@ -77,6 +78,7 @@ class SimpleRouter
* Perfect if you want to load pretty-urls from a file or database.
*
* @param IRouterBootManager $bootManager
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function addBootManager(IRouterBootManager $bootManager)
{
@@ -89,7 +91,9 @@ class SimpleRouter
* @param string $url
* @param string|\Closure $callback
* @param array|null $settings
*
* @return RouteUrl
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function get($url, $callback, array $settings = null)
{
@@ -103,6 +107,7 @@ class SimpleRouter
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function post($url, $callback, array $settings = null)
{
@@ -116,6 +121,7 @@ class SimpleRouter
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function put($url, $callback, array $settings = null)
{
@@ -129,6 +135,7 @@ class SimpleRouter
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function patch($url, $callback, array $settings = null)
{
@@ -142,6 +149,7 @@ class SimpleRouter
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function options($url, $callback, array $settings = null)
{
@@ -155,6 +163,7 @@ class SimpleRouter
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function delete($url, $callback, array $settings = null)
{
@@ -166,13 +175,14 @@ class SimpleRouter
*
* @param array $settings
* @param \Closure $callback
* @throws \InvalidArgumentException
* @return RouteGroup
* @throws \Pecee\Http\Exceptions\MalformedUrlException
* @throws InvalidArgumentException
*/
public static function group(array $settings = [], \Closure $callback)
{
if (is_callable($callback) === false) {
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
throw new InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$group = new RouteGroup();
@@ -189,15 +199,16 @@ class SimpleRouter
* parameters and which are only rendered when the url matches.
*
* @param string $url
* @param array $settings
* @param \Closure $callback
* @throws \InvalidArgumentException
* @param array $settings
* @return RoutePartialGroup
* @throws \Pecee\Http\Exceptions\MalformedUrlException
* @throws InvalidArgumentException
*/
public static function partialGroup($url, \Closure $callback, array $settings = [])
{
if (is_callable($callback) === false) {
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
throw new InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$settings['prefix'] = $url;
@@ -219,6 +230,7 @@ class SimpleRouter
* @param array|null $settings
* @see SimpleRouter::form
* @return RouteUrl
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function basic($url, $callback, array $settings = null)
{
@@ -234,6 +246,7 @@ class SimpleRouter
* @param array|null $settings
* @see SimpleRouter::form
* @return RouteUrl
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function form($url, $callback, array $settings = null)
{
@@ -248,6 +261,7 @@ class SimpleRouter
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl|IRoute
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function match(array $requestMethods, $url, $callback, array $settings = null)
{
@@ -271,6 +285,7 @@ class SimpleRouter
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl|IRoute
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function all($url, $callback, array $settings = null)
{
@@ -293,6 +308,7 @@ class SimpleRouter
* @param string $controller
* @param array|null $settings
* @return RouteController|IRoute
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function controller($url, $controller, array $settings = null)
{
@@ -315,6 +331,7 @@ class SimpleRouter
* @param string $controller
* @param array|null $settings
* @return RouteResource|IRoute
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function resource($url, $controller, array $settings = null)
{
@@ -335,6 +352,7 @@ class SimpleRouter
*
* @param \Closure $callback
* @return CallbackExceptionHandler $callbackHandler
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function error(\Closure $callback)
{
@@ -367,8 +385,9 @@ class SimpleRouter
* @param string|null $name
* @param string|array|null $parameters
* @param array|null $getParams
* @throws \InvalidArgumentException
* @throws \Pecee\Exceptions\InvalidArgumentException
* @return string
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function getUrl($name = null, $parameters = null, $getParams = null)
{
@@ -379,6 +398,7 @@ class SimpleRouter
* Get the request
*
* @return \Pecee\Http\Request
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function request()
{
@@ -389,6 +409,7 @@ class SimpleRouter
* Get the response object
*
* @return Response
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function response()
{
@@ -403,6 +424,7 @@ class SimpleRouter
* Returns the router instance
*
* @return Router
* @throws \Pecee\Http\Exceptions\MalformedUrlException
*/
public static function router()
{
+1 -1
View File
@@ -7,7 +7,7 @@ class ExceptionHandlerFirst implements \Pecee\Handlers\IExceptionHandler
global $stack;
$stack[] = static::class;
$request->setUri(new \Pecee\Http\Uri('/'));
$request->setUrl('/');
return $request;
}
@@ -7,7 +7,7 @@ class ExceptionHandlerSecond implements \Pecee\Handlers\IExceptionHandler
global $stack;
$stack[] = static::class;
$request->setUri(new \Pecee\Http\Uri('/'));
$request->setUrl('/');
return $request;
}
+6 -6
View File
@@ -3,18 +3,18 @@
class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
{
public static function debugNoReset($testUri, $testMethod = 'get')
public static function debugNoReset($testUrl, $testMethod = 'get')
{
static::request()->setUri(new \Pecee\Http\Uri($testUri));
static::request()->setUrl($testUrl);
static::request()->setMethod($testMethod);
static::start();
}
public static function debug($testUri, $testMethod = 'get')
public static function debug($testUrl, $testMethod = 'get')
{
try {
static::debugNoReset($testUri, $testMethod);
static::debugNoReset($testUrl, $testMethod);
} catch(\Exception $e) {
static::router()->reset();
throw $e;
@@ -24,13 +24,13 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
}
public static function debugOutput($testUri, $testMethod = 'get')
public static function debugOutput($testUrl, $testMethod = 'get')
{
$response = null;
// Route request
ob_start();
static::debug($testUri, $testMethod);
static::debug($testUrl, $testMethod);
$response = ob_get_contents();
ob_end_clean();
+1 -1
View File
@@ -65,7 +65,7 @@ class RouteRewriteTest extends PHPUnit_Framework_TestCase
TestRouter::error(function (\Pecee\Http\Request $request, \Exception $error) {
if (strtolower($request->getUri()->getPath()) === '/my/test/') {
if (strtolower($request->getUrl()->getPath()) === '/my/test/') {
$request->setRewriteUrl('/another-non-existing');
}