[ROUTER] Add option `is_system_path`, to allow specifying that a route, such as `gsactor_view_nickname` should not be considered a system path, when checking for the collision of nicknames

This commit is contained in:
Hugo Sales 2021-09-15 16:36:14 +01:00 committed by Diogo Peralta Cordeiro
parent e563c393f8
commit 15a2a69274
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 11 additions and 10 deletions

View File

@ -96,7 +96,7 @@ class RouteLoader extends Loader
* @param mixed $target Some kind of callable, typically class with `__invoke` or [object, method] * @param mixed $target Some kind of callable, typically class with `__invoke` or [object, method]
* @param null|array $param_reqs Array of {param} => regex * @param null|array $param_reqs Array of {param} => regex
* @param null|array $options Possible keys are ['condition', 'defaults', 'format', * @param null|array $options Possible keys are ['condition', 'defaults', 'format',
* 'fragment', 'http-methods', 'locale', 'methods', 'schemes', 'accept'] * 'fragment', 'http-methods', 'locale', 'methods', 'schemes', 'accept', 'is_system_path']
* 'http-methods' and 'methods' are aliases * 'http-methods' and 'methods' are aliases
*/ */
public function connect(string $id, string $uri_path, $target, ?array $options = [], ?array $param_reqs = []) public function connect(string $id, string $uri_path, $target, ?array $options = [], ?array $param_reqs = [])
@ -109,12 +109,13 @@ class RouteLoader extends Loader
// and special configuration options // and special configuration options
defaults: array_merge( defaults: array_merge(
[ [
'_controller' => is_array($target) ? $target : [$target, '__invoke'], '_controller' => is_array($target) ? $target : [$target, '__invoke'],
'_format' => $options['format'] ?? 'html', '_format' => $options['format'] ?? 'html',
'_fragment' => $options['fragment'] ?? '', '_fragment' => $options['fragment'] ?? '',
'_locale' => $options['locale'] ?? 'en', '_locale' => $options['locale'] ?? 'en',
'template' => $options['template'] ?? '', 'template' => $options['template'] ?? '',
'accept' => $options['accept'] ?? [], 'accept' => $options['accept'] ?? [],
'is_system_path' => $options['is_system_path'] ?? true,
], ],
$options['defaults'] ?? [] $options['defaults'] ?? []
), ),

View File

@ -43,6 +43,6 @@ abstract class GSActor
public static function load(RouteLoader $r): void public static function load(RouteLoader $r): void
{ {
$r->connect(id: 'gsactor_view_id', uri_path: '/actor/{id<\d+>}', target: [C\GSActor::class, 'GSActorShowId']); $r->connect(id: 'gsactor_view_id', uri_path: '/actor/{id<\d+>}', target: [C\GSActor::class, 'GSActorShowId']);
$r->connect(id: 'gsactor_view_nickname', uri_path: '/{nickname<' . Nickname::DISPLAY_FMT . '>}', target: [C\GSActor::class, 'GSActorShowNickname']); $r->connect(id: 'gsactor_view_nickname', uri_path: '/{nickname<' . Nickname::DISPLAY_FMT . '>}', target: [C\GSActor::class, 'GSActorShowNickname'], options: ['is_system_path' => false]);
} }
} }

View File

@ -154,8 +154,8 @@ abstract class Common
public static function isSystemPath(string $str): bool public static function isSystemPath(string $str): bool
{ {
try { try {
Router::match('/' . $str); $route = Router::match('/' . $str);
return true; return $route['is_system_path'] ?? true;
} catch (ResourceNotFoundException $e) { } catch (ResourceNotFoundException $e) {
return false; return false;
} }