From 0e4d90f0625567a32e457a288b6ee1d898c011d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dainius=20Puz=CC=8Cas?= <8631224+dsazup@users.noreply.github.com> Date: Thu, 4 Aug 2022 23:33:00 +0300 Subject: [PATCH] Add support for shallow nested resources --- src/Routing/ResourceBreadcrumbs.php | 24 ++++++++++++--- tests/ResourceRoutesTest.php | 48 +++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/Routing/ResourceBreadcrumbs.php b/src/Routing/ResourceBreadcrumbs.php index d26c021..74927c7 100644 --- a/src/Routing/ResourceBreadcrumbs.php +++ b/src/Routing/ResourceBreadcrumbs.php @@ -99,17 +99,23 @@ protected function makeBreadcrumbForAction(string $action, stdClass $config): Ro protected function getRouteNameForAction(string $action): string { $names = $this->options['names'] ?? []; - $action = $names[$action] ?? "{$this->name}.{$action}"; - + + if ($this->isShallow() && in_array($action, ['show', 'edit'])) { + $action = $this->getShallowName().'.'.$action; + } else { + $action = $names[$action] ?? "{$this->name}.{$action}"; + } + return trim(Route::mergeWithLastGroup(['as' => $action])['as'], '.'); } protected function getParameterNamesForAction(string $action): array { $parameters = $this->getRouteGroupParameters(); - + $name = $this->isShallow() ? $this->getShallowName() : $this->name; + if (in_array($action, ['show', 'edit'])) { - $parameters[] = $this->options['parameters'][$this->name] ?? Str::singular($this->name); + $parameters[] = $this->options['parameters'][$name] ?? Str::singular($name); } return $parameters; @@ -124,4 +130,14 @@ protected function getRouteGroupParameters(): array return $matches[1] ?? []; } + + private function isShallow(): bool + { + return isset($this->options['shallow']) && $this->options['shallow']; + } + + private function getShallowName(): string + { + return last(explode('.', $this->name)); + } } diff --git a/tests/ResourceRoutesTest.php b/tests/ResourceRoutesTest.php index 07f547e..f03c12c 100644 --- a/tests/ResourceRoutesTest.php +++ b/tests/ResourceRoutesTest.php @@ -4,6 +4,7 @@ use Closure; use Glhd\Gretel\Routing\ResourceBreadcrumbs; +use Glhd\Gretel\Tests\Models\Note; use Glhd\Gretel\Tests\Models\User; use Illuminate\Routing\Middleware\SubstituteBindings; use Illuminate\Routing\PendingResourceRegistration; @@ -333,8 +334,43 @@ public function test_grouped_resource_routes(bool $cache): void ['Create', '/movies/1/actors/create'], ); } - - protected function registerResourceRoute(bool $cache, Closure $setup): self + + /** + * @dataProvider cachingProvider + */ + public function test_nested_shallow_resource(bool $cache): void + { + Route::middleware(SubstituteBindings::class) + ->group(function() { + Route::resource('users', ResourceRoutesTestController::class) + ->breadcrumbs([ + 'index' => 'Users', + 'create' => 'New User', + 'edit' => 'Edit', + ]); + + Route::resource('users.notes', NotesController::class) + ->shallow() + ->breadcrumbs(fn(ResourceBreadcrumbs $breadcrumbs) => $breadcrumbs + ->show(fn(Note $note) => $note->note, 'users.index', fn(Note $note) => $note->user) + ->edit('Edit', '.show', fn(Note $note) => $note->user) + ); + }); + + $note = Note::factory()->create(['note' => 'some note']); + + $this->setUpCache($cache); + + $this->get('/notes/'.$note->id.'/edit'); + + $this->assertActiveBreadcrumbs( + ['Users', '/users'], + ['some note', '/notes/'.$note->id], + ['Edit', '/notes/'.$note->id.'/edit'], + ); + } + + protected function registerResourceRoute(bool $cache, Closure $setup): self { Route::middleware(SubstituteBindings::class) ->group(function() use ($setup) { @@ -393,3 +429,11 @@ public function edit(User $jazzy_dancer) return $jazzy_dancer->name; } } + +class NotesController +{ + public function edit(Note $note) + { + return $note; + } +}