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

Make internal PHP functionality private #341

Merged
merged 10 commits into from
Oct 24, 2020
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand All @@ -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**

Expand Down
2 changes: 1 addition & 1 deletion src/CommandRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
55 changes: 12 additions & 43 deletions src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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 = [];
Expand All @@ -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) {
Expand All @@ -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()] ?? [])
Expand Down Expand Up @@ -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');

Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/RouteModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/ZiggyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down