mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
Development
- Updated `helpers.php` and helpers example in documentation. - MalformedUrlException is now handled properly by Router to avoid phpStorm syntax highlights in routes. - Added `getUrlCopy` to `Request` class, used to clone the current route (to keep domain etc.) - `setUrl` in `Request` are now strict and requires `Url` object and no longer accepts strings. - Renamed `hasRewrite` property to `hasPendingRewrite` in `Request` class. - Renamed `hasRewrite` and `setHasRewrite` methods to `hasPendingRewrite` and `setHasPendingRewrite` in `Request` class. - Added better usage of `Url` class. When calling `url` you can now use the methods on the `Url` class to filter params, get relative/absolute url etc. See documentation for more info. - Renamed `get` method to `getValue` in `InputHandler` class. - Renamed `getObject` to `get` and removed `$defaultValue` argument in `InputHandler` class. - Optimized `InputHandler` class. - Fixed issue with `$token` not being proper string in `BaseCsrfVerifier` when token is not found. - Added php.ini configuration settings to `setcookie` in `CookieTokenProvider` for improved security. - Added `$router` parameter to `boot` method in `IRouterBootManager` which allows for further manipulation of the router within the bootmanager. - Renamed `$processingRoute` property to `$isProcessingRoute` in `Router` class. - Fixed `reset` method not resetting CSRF-verifier in `Router` class. - Moved `arrayToParams` helper-method from `Router` to `Url` class. - Began to add Event-functionality to router. - Added `addEventHandler` method to `SimpleRouter` class. - Moved `Pecee\SimpleRouter\Handler\CallbackExceptionHandler` to `Pecee\SimpleRouter\Handlers\CallbackExceptionHandler`. - Moved `Pecee\SimpleRouter\Handler\IExceptionHandler` to `Pecee\SimpleRouter\Handlers\IExceptionHandler`. - Added Events section to documentation. - Added more information on url-handling in documentation. - Optimisations.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class ExceptionHandler implements \Pecee\Handlers\IExceptionHandler
|
||||
class ExceptionHandler implements \Pecee\SimpleRouter\Handlers\IExceptionHandler
|
||||
{
|
||||
public function handleError(\Pecee\Http\Request $request, \Exception $error) : void
|
||||
{
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
|
||||
class ExceptionHandlerFirst implements \Pecee\Handlers\IExceptionHandler
|
||||
class ExceptionHandlerFirst implements \Pecee\SimpleRouter\Handlers\IExceptionHandler
|
||||
{
|
||||
public function handleError(\Pecee\Http\Request $request, \Exception $error) : void
|
||||
{
|
||||
global $stack;
|
||||
$stack[] = static::class;
|
||||
|
||||
$request->setUrl('/');
|
||||
$request->setUrl(new \Pecee\Http\Url('/'));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
|
||||
class ExceptionHandlerSecond implements \Pecee\Handlers\IExceptionHandler
|
||||
class ExceptionHandlerSecond implements \Pecee\SimpleRouter\Handlers\IExceptionHandler
|
||||
{
|
||||
public function handleError(\Pecee\Http\Request $request, \Exception $error) : void
|
||||
{
|
||||
global $stack;
|
||||
$stack[] = static::class;
|
||||
|
||||
$request->setUrl('/');
|
||||
$request->setUrl(new \Pecee\Http\Url('/'));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class ExceptionHandlerThird implements \Pecee\Handlers\IExceptionHandler
|
||||
class ExceptionHandlerThird implements \Pecee\SimpleRouter\Handlers\IExceptionHandler
|
||||
{
|
||||
public function handleError(\Pecee\Http\Request $request, \Exception $error) : void
|
||||
{
|
||||
|
||||
32
tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php
Normal file
32
tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
class TestBootManager implements \Pecee\SimpleRouter\IRouterBootManager
|
||||
{
|
||||
|
||||
protected $routes;
|
||||
protected $aliasUrl;
|
||||
|
||||
public function __construct(array $routes, string $aliasUrl)
|
||||
{
|
||||
$this->routes = $routes;
|
||||
$this->aliasUrl = $aliasUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when router loads it's routes
|
||||
*
|
||||
* @param \Pecee\SimpleRouter\Router $router
|
||||
* @param \Pecee\Http\Request $request
|
||||
*/
|
||||
public function boot(\Pecee\SimpleRouter\Router $router, \Pecee\Http\Request $request): void
|
||||
{
|
||||
foreach ($this->routes as $url) {
|
||||
// If the current url matches the rewrite url, we use our custom route
|
||||
|
||||
if ($request->getUrl()->contains($url) === true) {
|
||||
$request->setRewriteUrl($this->aliasUrl);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class SilentTokenProvider implements \Pecee\Http\Security\ITokenProvider {
|
||||
|
||||
protected $token;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh existing token
|
||||
*/
|
||||
public function refresh(): void
|
||||
{
|
||||
$this->token = uniqid('', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate valid CSRF token
|
||||
*
|
||||
* @param string $token
|
||||
* @return bool
|
||||
*/
|
||||
public function validate(string $token): bool
|
||||
{
|
||||
return ($token === $this->token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get token token
|
||||
*
|
||||
* @param string|null $defaultValue
|
||||
* @return string|null
|
||||
*/
|
||||
public function getToken(?string $defaultValue = null): ?string
|
||||
{
|
||||
return $this->token ?? $defaultValue;
|
||||
}
|
||||
}
|
||||
75
tests/Pecee/SimpleRouter/EventHandlerTest.php
Normal file
75
tests/Pecee/SimpleRouter/EventHandlerTest.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
require_once 'Dummy/Security/SilentTokenProvider.php';
|
||||
|
||||
use \Pecee\SimpleRouter\Handlers\EventHandler;
|
||||
use \Pecee\SimpleRouter\Event\EventArgument;
|
||||
|
||||
class EventHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testMissingEvents() {
|
||||
|
||||
$events = EventHandler::$events;
|
||||
|
||||
// Remove the all event
|
||||
unset($events[\array_search(EventHandler::EVENT_ALL, $events, true)], $events[\array_search(EventHandler::EVENT_REWRITE, $events, true)]);
|
||||
|
||||
$eventHandler = new EventHandler();
|
||||
$eventHandler->register(EventHandler::EVENT_ALL, function(EventArgument $arg) use(&$events) {
|
||||
$key = \array_search($arg->getEventName(), $events, true);
|
||||
unset($events[$key]);
|
||||
});
|
||||
|
||||
TestRouter::addEventHandler($eventHandler);
|
||||
|
||||
TestRouter::error(function (\Pecee\Http\Request $request, \Exception $error) {
|
||||
|
||||
// Trigger rewrite
|
||||
$request->setRewriteUrl('/');
|
||||
|
||||
});
|
||||
|
||||
TestRouter::get('/', 'DummyController@method1')->name('home');
|
||||
|
||||
TestRouter::router()->findRoute('home');
|
||||
TestRouter::router()->getUrl('home');
|
||||
|
||||
$csrfVerifier = new \Pecee\Http\Middleware\BaseCsrfVerifier();
|
||||
$csrfVerifier->setTokenProvider(new SilentTokenProvider());
|
||||
TestRouter::csrfVerifier($csrfVerifier);
|
||||
|
||||
TestRouter::debug('/not-existing');
|
||||
|
||||
$this->assertEquals($events, []);
|
||||
|
||||
TestRouter::router()->reset();
|
||||
|
||||
}
|
||||
|
||||
public function testAllEvent() {
|
||||
|
||||
$status = 0;
|
||||
|
||||
$eventHandler = new EventHandler();
|
||||
$eventHandler->register(EventHandler::EVENT_ALL, function(EventArgument $arg) use(&$status) {
|
||||
$status++;
|
||||
});
|
||||
|
||||
TestRouter::addEventHandler($eventHandler);
|
||||
|
||||
TestRouter::get('/', 'DummyController@method1');
|
||||
TestRouter::debug('/');
|
||||
|
||||
// All event should fire for each other event
|
||||
$this->assertEquals(\count(EventHandler::$events), $status);
|
||||
}
|
||||
|
||||
public function testEvents() {
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
}
|
||||
30
tests/Pecee/SimpleRouter/InputHandlerTest.php
Normal file
30
tests/Pecee/SimpleRouter/InputHandlerTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
|
||||
class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testGet() {
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
public function testPost() {
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
public function testFile() {
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
public function testFiles() {
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
public function testAll() {
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -93,6 +93,7 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
public function testDomainAllowedRoute()
|
||||
{
|
||||
$this->result = false;
|
||||
TestRouter::request()->setHost('hello.world.com');
|
||||
|
||||
TestRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
||||
TestRouter::get('/test', function ($subdomain = null) {
|
||||
@@ -100,7 +101,7 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
});
|
||||
});
|
||||
|
||||
TestRouter::request()->setHost('hello.world.com');
|
||||
|
||||
TestRouter::debug('/test', 'get');
|
||||
|
||||
$this->assertTrue($this->result);
|
||||
@@ -109,6 +110,8 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
public function testDomainNotAllowedRoute()
|
||||
{
|
||||
TestRouter::request()->setHost('other.world.com');
|
||||
|
||||
$this->result = false;
|
||||
|
||||
TestRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
||||
@@ -117,9 +120,6 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
});
|
||||
});
|
||||
|
||||
TestRouter::request()->setHost('other.world.com');
|
||||
|
||||
|
||||
TestRouter::debug('/test', 'get');
|
||||
|
||||
$this->assertFalse($this->result);
|
||||
|
||||
Reference in New Issue
Block a user