Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple model parameters in routes. #33

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion src/PendingRouteTransformers/HandleUrisOfNestedControllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ReflectionParameter;
use Spatie\RouteDiscovery\PendingRoutes\PendingRoute;
use Spatie\RouteDiscovery\PendingRoutes\PendingRouteAction;

Expand Down Expand Up @@ -33,7 +34,20 @@ public function transform(Collection $pendingRoutes): Collection
}

$childNode->actions->each(function (PendingRouteAction $action) use ($parentPendingRoute, $parentAction) {
$result = Str::replace($parentPendingRoute->uri, $parentAction->uri, $action->uri);
$paramsToRemove = $action->modelParameters()
->filter(
fn (ReflectionParameter $parameter) => $parentAction
->modelParameters()
->contains(
fn (ReflectionParameter $parentParameter) => $parentParameter->getName() === $parameter->getName())
);
$result = Str::of($action->uri)
->replace(
$paramsToRemove->map(fn (ReflectionParameter $parameter) => "{{$parameter->getName()}}")->toArray(),
''
)
->replace('//', '/')
->replace($parentPendingRoute->uri, $parentAction->uri);

$action->uri = $result;
});
Expand Down
25 changes: 18 additions & 7 deletions src/PendingRoutes/PendingRouteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ReflectionAttribute;
use ReflectionMethod;
Expand All @@ -20,6 +21,9 @@ class PendingRouteAction
/** @var array<int, string> */
public array $methods = [];

/** @var Collection<ReflectionParameter> */
public Collection $modelParameters;

/** @var array{class-string, string} */
public array $action;

Expand All @@ -40,35 +44,42 @@ public function __construct(ReflectionMethod $method, string $controllerClass)
{
$this->method = $method;

$this->modelParameters = $this->modelParameters();

$this->uri = $this->relativeUri();

$this->methods = $this->discoverHttpMethods();

$this->action = [$controllerClass, $method->name];
}

public function relativeUri(): string
/**
* @return Collection<ReflectionParameter>
*/
public function modelParameters(): Collection
{
/** @var ReflectionParameter $modelParameter */
$modelParameter = collect($this->method->getParameters())->first(function (ReflectionParameter $parameter) {
return collect($this->method->getParameters())->filter(function (ReflectionParameter $parameter) {
$type = $parameter->getType();

return $type instanceof ReflectionNamedType && is_a($type->getName(), Model::class, true);
});
}

public function relativeUri(): string
{
$uri = '';

if (! in_array($this->method->getName(), $this->commonControllerMethodNames())) {
$uri = Str::kebab($this->method->getName());
}

/** @phpstan-ignore-next-line */
if ($modelParameter) {
if ($this->modelParameters->isNotEmpty()) {
if ($uri !== '') {
$uri .= '/';
}

$uri .= "{{$modelParameter->getName()}}";
$uri .= $this->modelParameters
->map(fn(ReflectionParameter $parameter) => "{{$parameter->getName()}}")
->implode('/');
}

return $uri;
Expand Down
54 changes: 54 additions & 0 deletions tests/RouteDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Middleware\MiddlewareOnControllerController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Middleware\MiddlewareOnMethodController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Model\ModelController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\MultipleModel\MultipleModelController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\NestedWithMultipleParametersController\PhotosController as MpPhotosController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\NestedWithMultipleParametersController\Photos\CommentsController as MpCommentsController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\NestedWithParametersController\Photos\CommentsController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\NestedWithParametersController\PhotosController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Nesting\Nested\ChildController;
Expand Down Expand Up @@ -120,6 +123,43 @@
);
});

it('can automatically discover a nested route with multiple model parameters', function () {
$this
->routeRegistrar
->registerDirectory(controllersPath('NestedWithMultipleParametersController'));

$this->assertRegisteredRoutesCount(5);

$this->assertRouteRegistered(
MpPhotosController::class,
controllerMethod: 'show',
uri: 'photos/{photo}',
);

$this->assertRouteRegistered(
MpPhotosController::class,
controllerMethod: 'edit',
uri: 'photos/edit/{photo}',
);

$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'show',
uri: 'photos/{photo}/comments/{comment}',
);
$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'edit',
uri: 'photos/{photo}/comments/edit/{comment}',
);
$this->assertRouteRegistered(
MpCommentsController::class,
controllerMethod: 'store',
httpMethods: 'post',
uri: 'photos/{photo}/comments/{comment}',
);
});

it('can automatically discovery a model route', function () {
$this
->routeRegistrar
Expand All @@ -134,6 +174,20 @@
);
});

it('can automatically discovery multiple model route', function () {
$this
->routeRegistrar
->registerDirectory(controllersPath('MultipleModel'));

$this
->assertRegisteredRoutesCount(1)
->assertRouteRegistered(
MultipleModelController::class,
controllerMethod: 'edit',
uri: 'multiple-model/edit/{user}/{photo}',
);
});

it('will only automatically register public methods', function () {
$this
->routeRegistrar
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\MultipleModel;

use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\Photo;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\User;

class MultipleModelController
{
public function edit(User $user, Photo $photo)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\NestedWithMultipleParametersController\Photos;

use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\Comment;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\Photo;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\User;

class CommentsController
{
public function show(Photo $photo, Comment $comment)
{
}

public function edit(Photo $photo, Comment $comment)
{
}

public function store(Photo $photo, Comment $comment)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\NestedWithMultipleParametersController;

use Spatie\RouteDiscovery\Tests\Support\TestClasses\Models\Photo;

class PhotosController
{
public function show(Photo $photo)
{
}

public function edit(Photo $photo)
{
}
}
Loading