Skip to content

Commit

Permalink
feat: support passing except/only args to ziggy command
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmanders committed Jan 19, 2025
1 parent 6612c8c commit e2fb9ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/CommandRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Filesystem\Filesystem;
use Tighten\Ziggy\Output\File;
use Tighten\Ziggy\Output\Types;
use Tighten\Ziggy\Ziggy;

class CommandRouteGenerator extends Command
{
Expand All @@ -15,13 +14,20 @@ class CommandRouteGenerator extends Command
{--types : Generate a TypeScript declaration file.}
{--types-only : Generate only a TypeScript declaration file.}
{--url=}
{--group=}';
{--group=}
{--except=}
{--only=}';

protected $description = 'Generate a JavaScript file containing Ziggy’s routes and configuration.';

public function handle(Filesystem $filesystem)
{
$ziggy = new Ziggy($this->option('group'), $this->option('url') ? url($this->option('url')) : null);
$ziggy = new Ziggy(
$this->option('group'),
$this->option('url') ? url($this->option('url')) : null,
$this->option('except') ? explode(',', $this->option('except')) : null,
$this->option('only') ? explode(',', $this->option('only')) : null,
);

$path = $this->argument('path') ?? config('ziggy.output.path', 'resources/js/ziggy.js');

Expand Down
14 changes: 9 additions & 5 deletions src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ class Ziggy implements JsonSerializable
public function __construct(
protected $group = null,
protected ?string $url = null,
protected ?array $except = null,
protected ?array $only = null
) {
$this->url = rtrim($url ?? url('/'), '/');
$this->except = $except ?? config('ziggy.except', []);
$this->only = $only ?? config('ziggy.only', []);

$this->routes = static::$cache ??= $this->nameKeyedRoutes();
}
Expand All @@ -43,12 +47,12 @@ private function applyFilters($group)
return $this->group($group);
}

if (config()->has('ziggy.except') && ! config()->has('ziggy.only')) {
return $this->filter(config('ziggy.except'), false)->routes;
if (count($this->except) > 0 && count($this->only) === 0) {
return $this->filter($this->except, false)->routes;
}

if (config()->has('ziggy.only') && ! config()->has('ziggy.except')) {
return $this->filter(config('ziggy.only'))->routes;
if (count($this->only) > 0 && count($this->except) === 0) {
return $this->filter($this->only)->routes;
}

return $this->routes;
Expand Down Expand Up @@ -204,7 +208,7 @@ private function folioRoutes(): Collection

// Use existing named Folio routes (instead of searching view files) to respect route caching
return collect(app(FolioRoutes::class)->routes())->map(function (array $route) {
$uri = rtrim($route['baseUri'], '/') . str_replace([$route['mountPath'], '.blade.php'], '', $route['path']);
$uri = rtrim($route['baseUri'], '/').str_replace([$route['mountPath'], '.blade.php'], '', $route['path']);

$segments = explode('/', $uri);
$parameters = [];
Expand Down
12 changes: 11 additions & 1 deletion tests/Unit/CommandRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
});

test('generate file respecting exclude options', function () {
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by options

artisan('ziggy:generate --except=admin.*');

expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
});

test('generate file with custom output formatter', function () {
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by config
Expand Down Expand Up @@ -162,7 +172,7 @@

class CustomFile extends File
{
function __toString(): string
public function __toString(): string
{
return <<<JAVASCRIPT
// This is a custom template
Expand Down

0 comments on commit e2fb9ae

Please sign in to comment.