diff --git a/CHANGELOG.md b/CHANGELOG.md index 4598a282..b42346fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,8 @@ Breaking changes are marked with ⚠️. - ⚠️ Allow getting the route name with `current()` when the current URL has a query string ([#330](https://github.com/tighten/ziggy/pull/330)) - ⚠️ Return a literal string from the `route()` function when any arguments are passed to it ([#336](https://github.com/tighten/ziggy/pull/336)) - ⚠️ Rename `namedRoutes` → `routes`, `defaultParameters` → `defaults`, `baseUrl` → `url`, and `basePort` → `port` ([#338](https://github.com/tighten/ziggy/pull/338)) +- ⚠️ Make the `filter()` method on the `Ziggy` class return an instance of that class instead of a collection of routes ([#341](https://github.com/tighten/ziggy/pull/341)) +- ⚠️ Make the `nameKeyedRoutes()`, `resolveBindings()`, `applyFilters()`, and `group()` methods on the `Ziggy` class, and the `generate()` method on the `CommandRouteGenerator` class, private ([#341](https://github.com/tighten/ziggy/pull/341)) **Deprecated** @@ -44,6 +46,7 @@ Breaking changes are marked with ⚠️. - ⚠️ Remove the `UrlBuilder` class ([#330](https://github.com/tighten/ziggy/pull/330)) - ⚠️ Remove the `url()` method now that `route(...)` returns a string ([#336](https://github.com/tighten/ziggy/pull/336)) - ⚠️ Remove the `baseDomain` and `baseProtocol` properties on the Ziggy config object ([#337](https://github.com/tighten/ziggy/pull/337)) +- ⚠️ Remove the `appendRouteToList()`, `isListedAs()`, `except()`, and `only()` methods from the `Ziggy` class ([#341](https://github.com/tighten/ziggy/pull/341)) **Fixed** diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index 48161a7f..7f6b80c4 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -35,7 +35,7 @@ public function handle() $this->info('File generated!'); } - public function generate($group = false) + private function generate($group = false) { $payload = (new Ziggy($group, url($this->option('url'))))->toJson(); diff --git a/src/Ziggy.php b/src/Ziggy.php index d4ba9148..3f29e45f 100644 --- a/src/Ziggy.php +++ b/src/Ziggy.php @@ -25,7 +25,7 @@ public function __construct(string $group = null, string $url = null) $this->routes = $this->nameKeyedRoutes(); } - public function applyFilters($group) + private function applyFilters($group) { if ($group) { return $this->group($group); @@ -37,11 +37,11 @@ public function applyFilters($group) } if (config()->has('ziggy.except')) { - return $this->except(); + return $this->filter(config('ziggy.except'), false)->routes; } if (config()->has('ziggy.only')) { - return $this->only(); + return $this->filter(config('ziggy.only'))->routes; } return $this->routes; @@ -50,7 +50,7 @@ public function applyFilters($group) /** * Filter routes by group. */ - public function group($group) + private function group($group) { if (is_array($group)) { $filters = []; @@ -59,40 +59,32 @@ public function group($group) $filters = array_merge($filters, config("ziggy.groups.{$groupName}")); } - return $this->filter($filters, true); + return $this->filter($filters, true)->routes; } if (config()->has("ziggy.groups.{$group}")) { - return $this->filter(config("ziggy.groups.{$group}"), true); + return $this->filter(config("ziggy.groups.{$group}"), true)->routes; } return $this->routes; } - public function except() - { - return $this->filter(config('ziggy.except'), false); - } - - public function only() - { - return $this->filter(config('ziggy.only')); - } - /** * Filter routes by name using the given patterns. */ - public function filter($filters = [], $include = true) + public function filter($filters = [], $include = true): self { - return $this->routes->filter(function ($route, $name) use ($filters, $include) { + $this->routes = $this->routes->filter(function ($route, $name) use ($filters, $include) { return Str::is(Arr::wrap($filters), $name) ? $include : ! $include; }); + + return $this; } /** * Get a list of the application's named routes, keyed by their names. */ - protected function nameKeyedRoutes() + private function nameKeyedRoutes() { [$fallbacks, $routes] = collect(app('router')->getRoutes()->getRoutesByName()) ->partition(function ($route) { @@ -103,12 +95,6 @@ protected function nameKeyedRoutes() return $routes->merge($fallbacks) ->map(function ($route) use ($bindings) { - if ($this->isListedAs($route, 'except')) { - $this->appendRouteToList($route->getName(), 'except'); - } elseif ($this->isListedAs($route, 'only')) { - $this->appendRouteToList($route->getName(), 'only'); - } - return collect($route)->only(['uri', 'methods']) ->put('domain', $route->domain()) ->put('bindings', $bindings[$route->getName()] ?? []) @@ -153,27 +139,10 @@ public function toJson(int $options = 0): string return json_encode($this->jsonSerialize(), $options); } - /** - * Add the given route name to the current list of routes. - */ - protected function appendRouteToList($name, $list) - { - config()->push("ziggy.{$list}", $name); - } - - /** - * Check if the given route name is present in the given list. - */ - protected function isListedAs($route, $list) - { - return (isset($route->listedAs) && $route->listedAs === $list) - || Arr::get($route->getAction(), 'listed_as', null) === $list; - } - /** * Resolve route key names for any route parameters using Eloquent route model binding. */ - protected function resolveBindings(array $routes): array + private function resolveBindings(array $routes): array { $scopedBindings = method_exists(head($routes), 'bindingFields'); diff --git a/tests/Unit/RouteModelBindingTest.php b/tests/Unit/RouteModelBindingTest.php index 7cd0cf0d..81aee22c 100644 --- a/tests/Unit/RouteModelBindingTest.php +++ b/tests/Unit/RouteModelBindingTest.php @@ -52,7 +52,7 @@ public function can_register_implicit_route_model_bindings() ], ]; - $this->assertSame($expected, (new Ziggy)->filter('users')->toArray()); + $this->assertSame($expected, (new Ziggy)->filter('users')->toArray()['routes']); } /** @test */ @@ -63,7 +63,7 @@ public function can_ignore_route_parameters_not_bound_to_eloquent_models() 'uri' => 'tokens/{token}', 'methods' => ['GET', 'HEAD'], ], - ], (new Ziggy)->filter(['tokens'])->toArray()); + ], (new Ziggy)->filter(['tokens'])->toArray()['routes']); } /** @test */ @@ -79,7 +79,7 @@ public function can_handle_bound_and_unbound_parameters_in_the_same_route() ], ]; - $this->assertSame($expected, (new Ziggy)->filter('users.numbers')->toArray()); + $this->assertSame($expected, (new Ziggy)->filter('users.numbers')->toArray()['routes']); } /** @test */ @@ -107,7 +107,7 @@ public function can_handle_multiple_scoped_bindings() 'tag' => 'slug', ], ], - ], (new Ziggy)->filter('posts*')->toArray()); + ], (new Ziggy)->filter('posts*')->toArray()['routes']); } /** @test */ diff --git a/tests/Unit/ZiggyTest.php b/tests/Unit/ZiggyTest.php index 5831b537..28447717 100644 --- a/tests/Unit/ZiggyTest.php +++ b/tests/Unit/ZiggyTest.php @@ -28,7 +28,7 @@ protected function setUp(): void } /** - * If running Laravel 7 or higher, the 'postComments.show' route. + * If running Laravel 7 or higher, add the 'postComments.show' route. */ protected function addPostCommentsRouteWithBindings(array &$routes): void { @@ -64,7 +64,7 @@ public function can_filter_to_only_include_routes_matching_a_pattern() ], ]; - $this->assertSame($expected, $routes->toArray()); + $this->assertSame($expected, $routes->toArray()['routes']); } /** @test */ @@ -86,7 +86,7 @@ public function can_filter_to_exclude_routes_matching_a_pattern() $this->addPostCommentsRouteWithBindings($expected); - $this->assertSame($expected, $routes->toArray()); + $this->assertSame($expected, $routes->toArray()['routes']); } /** @test */