mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-19 01:31:28 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d4de7fc3df | |||
| 03ef9dfb74 | |||
| 4c5f825c97 | |||
| b7c31ae434 | |||
| 0c329e4c5b | |||
| e057a76153 | |||
| f7f1f1e3de |
@@ -97,7 +97,7 @@ class InputItem implements ArrayAccess, IInputItem, IteratorAggregate
|
||||
|
||||
public function offsetUnset($offset): void
|
||||
{
|
||||
unset($this->data[$offset]);
|
||||
unset($this->value[$offset]);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -431,7 +431,7 @@ class Url implements JsonSerializable
|
||||
* @param bool $includeParams
|
||||
* @return string
|
||||
*/
|
||||
public function getRelativeUrl($includeParams = true): string
|
||||
public function getRelativeUrl(bool $includeParams = true): string
|
||||
{
|
||||
$path = $this->path ?? '/';
|
||||
|
||||
@@ -451,7 +451,7 @@ class Url implements JsonSerializable
|
||||
* @param bool $includeParams
|
||||
* @return string
|
||||
*/
|
||||
public function getAbsoluteUrl($includeParams = true): string
|
||||
public function getAbsoluteUrl(bool $includeParams = true): string
|
||||
{
|
||||
$scheme = $this->scheme !== null ? $this->scheme . '://' : '';
|
||||
$host = $this->host ?? '';
|
||||
|
||||
@@ -944,4 +944,10 @@ class Router
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addExceptionHandler(IExceptionHandler $handler): self
|
||||
{
|
||||
$this->exceptionHandlers[] = $handler;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -425,16 +425,9 @@ class SimpleRouter
|
||||
*/
|
||||
public static function error(Closure $callback): CallbackExceptionHandler
|
||||
{
|
||||
$routes = static::router()->getRoutes();
|
||||
|
||||
$callbackHandler = new CallbackExceptionHandler($callback);
|
||||
|
||||
$group = new RouteGroup();
|
||||
$group->addExceptionHandler($callbackHandler);
|
||||
|
||||
array_unshift($routes, $group);
|
||||
|
||||
static::router()->setRoutes($routes);
|
||||
static::router()->addExceptionHandler($callbackHandler);
|
||||
|
||||
return $callbackHandler;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user