mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-19 17:51:26 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 847cb3e273 | |||
| b937b610de | |||
| fadb783d3c | |||
| 8477ea19d4 | |||
| 4121011ef2 | |||
| 5dbfc3dbfe | |||
| 0aea8673d9 | |||
| 9c66a4dfd8 | |||
| d5bf77cbd4 | |||
| 1e9fa9c6a1 | |||
| 0630569f56 |
@@ -89,7 +89,6 @@ class InputItem implements ArrayAccess, IInputItem, IteratorAggregate
|
||||
return isset($this->value[$offset]);
|
||||
}
|
||||
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($offset): ?self
|
||||
{
|
||||
if ($this->offsetExists($offset) === true) {
|
||||
|
||||
@@ -82,18 +82,15 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
{
|
||||
$this->url = ($url === '/') ? '/' : '/' . trim($url, '/') . '/';
|
||||
|
||||
$parameters = [];
|
||||
if (strpos($this->url, $this->paramModifiers[0]) !== false) {
|
||||
|
||||
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
|
||||
|
||||
if ((bool)preg_match_all('/' . $regex . '/u', $this->url, $matches) !== false) {
|
||||
$parameters = array_fill_keys($matches[1], null);
|
||||
$this->parameters = array_fill_keys($matches[1], null);
|
||||
}
|
||||
}
|
||||
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,22 +7,22 @@ use Pecee\Http\Request;
|
||||
class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
{
|
||||
protected array $urls = [
|
||||
'index' => '',
|
||||
'create' => 'create',
|
||||
'store' => '',
|
||||
'show' => '',
|
||||
'edit' => 'edit',
|
||||
'update' => '',
|
||||
'index' => '',
|
||||
'create' => 'create',
|
||||
'store' => '',
|
||||
'show' => '',
|
||||
'edit' => 'edit',
|
||||
'update' => '',
|
||||
'destroy' => '',
|
||||
];
|
||||
|
||||
protected array $methodNames = [
|
||||
'index' => 'index',
|
||||
'create' => 'create',
|
||||
'store' => 'store',
|
||||
'show' => 'show',
|
||||
'edit' => 'edit',
|
||||
'update' => 'update',
|
||||
'index' => 'index',
|
||||
'create' => 'create',
|
||||
'store' => 'store',
|
||||
'show' => 'show',
|
||||
'edit' => 'edit',
|
||||
'update' => 'update',
|
||||
'destroy' => 'destroy',
|
||||
];
|
||||
|
||||
@@ -69,18 +69,11 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
|
||||
{
|
||||
$url = array_search($name, $this->names, true);
|
||||
|
||||
$parametersUrl = '';
|
||||
|
||||
if ($parameters !== null && count($parameters) > 0) {
|
||||
$parametersUrl = join('/', $parameters) . '/';
|
||||
}
|
||||
|
||||
if ($url !== false) {
|
||||
return rtrim($this->url . $parametersUrl . $this->urls[$url], '/') . '/';
|
||||
return rtrim($this->url . $this->urls[$url], '/') . '/';
|
||||
}
|
||||
|
||||
return $this->url . $parametersUrl;
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
protected function call($method): bool
|
||||
@@ -179,12 +172,12 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
$this->name = $name;
|
||||
|
||||
$this->names = [
|
||||
'index' => $this->name . '.index',
|
||||
'create' => $this->name . '.create',
|
||||
'store' => $this->name . '.store',
|
||||
'show' => $this->name . '.show',
|
||||
'edit' => $this->name . '.edit',
|
||||
'update' => $this->name . '.update',
|
||||
'index' => $this->name . '.index',
|
||||
'create' => $this->name . '.create',
|
||||
'store' => $this->name . '.store',
|
||||
'show' => $this->name . '.show',
|
||||
'edit' => $this->name . '.edit',
|
||||
'update' => $this->name . '.update',
|
||||
'destroy' => $this->name . '.destroy',
|
||||
];
|
||||
|
||||
|
||||
@@ -443,8 +443,12 @@ class Router
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
return $this->handleException($e);
|
||||
} catch (\Throwable $e) {
|
||||
if ($e instanceof Exception) {
|
||||
return $this->handleException($e);
|
||||
}
|
||||
|
||||
return $this->handleException(new Exception($e->getMessage(), $e->getCode()));
|
||||
}
|
||||
|
||||
if ($methodNotAllowed === true) {
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class DummyLoadableRoute extends Pecee\SimpleRouter\Route\LoadableRoute {
|
||||
|
||||
public function matchRoute(string $url, Request $request): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/Route/DummyLoadableRoute.php';
|
||||
|
||||
class LoadableRouteTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testSetUrlUpdatesParameters()
|
||||
{
|
||||
$route = new DummyLoadableRoute();
|
||||
$this->assertEmpty($route->getParameters());
|
||||
|
||||
$route->setUrl('/');
|
||||
$this->assertEmpty($route->getParameters());
|
||||
|
||||
$expected = ['param' => null, 'optionalParam' => null];
|
||||
$route->setUrl('/{param}/{optionalParam?}');
|
||||
$this->assertEquals($expected, $route->getParameters());
|
||||
|
||||
$expected = ['otherParam' => null];
|
||||
$route->setUrl('/{otherParam}');
|
||||
$this->assertEquals($expected, $route->getParameters());
|
||||
|
||||
$expected = [];
|
||||
$route->setUrl('/');
|
||||
$this->assertEquals($expected, $route->getParameters());
|
||||
}
|
||||
}
|
||||
@@ -63,22 +63,7 @@ class RouterResourceTest extends \PHPUnit\Framework\TestCase
|
||||
$response = TestRouter::debugOutput('/resource/38', 'get');
|
||||
|
||||
$this->assertEquals('show 38', $response);
|
||||
}
|
||||
|
||||
public function testResourceUrls()
|
||||
{
|
||||
TestRouter::resource('/resource', 'ResourceController')->name('resource');
|
||||
|
||||
TestRouter::debugNoReset('/resource');
|
||||
|
||||
$this->assertEquals('/resource/3/create/', TestRouter::router()->getUrl('resource.create', ['id' => 3]));
|
||||
$this->assertEquals('/resource/5/edit/', TestRouter::router()->getUrl('resource.edit', ['id' => 5]));
|
||||
$this->assertEquals('/resource/6/', TestRouter::router()->getUrl('resource.update', ['id' => 6]));
|
||||
$this->assertEquals('/resource/9/', TestRouter::router()->getUrl('resource.destroy', ['id' => 9]));
|
||||
$this->assertEquals('/resource/12/', TestRouter::router()->getUrl('resource.delete', ['id' => 12]));
|
||||
$this->assertEquals('/resource/', TestRouter::router()->getUrl('resource'));
|
||||
|
||||
TestRouter::router()->reset();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -184,7 +184,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
|
||||
// Should match /?jackdaniels=true&cola=yeah
|
||||
$this->assertEquals('/?jackdaniels=true&cola=yeah', TestRouter::getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']));
|
||||
|
||||
TestRouter::reset();
|
||||
TestRouter::router()->reset();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,6 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
|
||||
static::request()->setHost('testhost.com');
|
||||
}
|
||||
|
||||
public static function reset(): void
|
||||
{
|
||||
static::$router = null;
|
||||
}
|
||||
|
||||
public static function debugNoReset(string $testUrl, string $testMethod = 'get'): void
|
||||
{
|
||||
$request = static::request();
|
||||
|
||||
Reference in New Issue
Block a user