Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/RestModelConfigProviderAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __invoke()
{
return [
'routes' => $this->routeGroup([
'path' => $this->pathPrefix,
'middleware' => $this->getMiddleware(),
],
$this->getRoutes()
Expand Down Expand Up @@ -97,6 +96,8 @@ protected function createRoute(
$handler = $middleware;
}

$path = $this->pathPrefix . '/' . ltrim($path, '/');

$route = [
'name' => $name,
'path' => $path,
Expand Down Expand Up @@ -150,8 +151,15 @@ protected function createModelRoute(
if ($handler === null) {
$handler = $this->defaultHandler;
}

$trimmedEndpoint = trim($endpoint, '/');
$endpointPrefix = trim($this->pathPrefix, '/');
$fullEndpoint = $endpointPrefix . '/' . $trimmedEndpoint;
$trimmedFullEndpoint = strtolower(trim($fullEndpoint, '/'));
$cleanEndpointName = strtolower(str_replace(['/'], '.', trim($trimmedFullEndpoint, '/')));

if ($privilege === null) {
$privilege = "pr.api.$endpoint";
$privilege = "pr.$cleanEndpointName";
}

$routeParameters = '/[{' . $idField . ':' . $idRegex . '}]';
Expand All @@ -177,14 +185,20 @@ protected function createModelRoute(
$settings = array_merge($settings, $options);
}

$privilegeLabel = $trimmedFullEndpoint;
if (str_starts_with($privilegeLabel, 'api/')) {
$privilegeLabel = substr($privilegeLabel, 4);
}

$routes = [];

if (!empty($methods)) {
$name = "api.$endpoint.structure";
$name = "$cleanEndpointName.structure";
$settings['privilege'] = $privilege . '.structure';
$settings['privilegeLabel'] = "API: $privilegeLabel -> /structure";
$routes[$name] = [
'name' => $name,
'path' => '/' . $endpoint . '/structure',
'path' => '/' . $trimmedFullEndpoint . '/structure',
'middleware' => $handler,
'options' => $settings,
'allowed_methods' => ['GET']
Expand All @@ -193,22 +207,23 @@ protected function createModelRoute(

foreach($methods as $method) {
$path = match ($method) {
'GET' => '/' . $endpoint . '['.$routeParameters.']',
'POST' => '/' . $endpoint,
'PATCH', 'PUT', 'DELETE' => '/' . $endpoint . $routeParameters,
'GET' => '/' . $trimmedEndpoint . '['.$routeParameters.']',
'POST' => '/' . $trimmedEndpoint,
'PATCH', 'PUT', 'DELETE' => '/' . $trimmedEndpoint . $routeParameters,
default => null,
};
$name = "api.$endpoint.$method";
$name = "$cleanEndpointName.$method";

$settings['privilege'] = "$privilege.$method";
$settings['privilegeLabel'] = "API: $endpoint -> $method";
$settings['privilegeLabel'] = "API: $privilegeLabel -> $method";

[$methodRoute] = $this->createRoute(
name: $name,
path: $path,
handler: $handler,
allowedMethods: [$method],
options: $settings,
privilege: $settings['privilege'],
);
$routes[$name] = $methodRoute;
}
Expand Down
Loading