From cce8f19aded0ffb5ff3b11ca464185c4f8a442de Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 2 Nov 2020 12:23:15 -0500 Subject: [PATCH 1/4] Revert outputting generated Blade directive contents directly (#349) * Revert outputting generated Blade directive contents directly * Remove test for reverted behaviour --- src/ZiggyServiceProvider.php | 4 +--- tests/Unit/BladeRouteGeneratorTest.php | 12 ------------ 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/ZiggyServiceProvider.php b/src/ZiggyServiceProvider.php index 2e6e0e1c..46e8c77c 100644 --- a/src/ZiggyServiceProvider.php +++ b/src/ZiggyServiceProvider.php @@ -25,9 +25,7 @@ public function boot() protected function registerDirective(BladeCompiler $bladeCompiler) { $bladeCompiler->directive('routes', function ($group) { - return app()->isLocal() - ? "generate({$group}); ?>" - : app(BladeRouteGenerator::class)->generate($group); + return "generate({$group}); ?>"; }); } } diff --git a/tests/Unit/BladeRouteGeneratorTest.php b/tests/Unit/BladeRouteGeneratorTest.php index ad1c1f54..1f23bf05 100644 --- a/tests/Unit/BladeRouteGeneratorTest.php +++ b/tests/Unit/BladeRouteGeneratorTest.php @@ -105,18 +105,6 @@ public function can_set_csp_nonce() ); } - /** @test */ - public function can_compile_routes_directive() - { - $compiler = app('blade.compiler'); - - BladeRouteGenerator::$generated = false; - $script = (new BladeRouteGenerator)->generate(); - - BladeRouteGenerator::$generated = false; - $this->assertSame($script, $compiler->compileString('@routes')); - } - /** @test */ public function can_output_script_tag() { From 4c76a1cb4fa412c61b4ab4ce06e9f95afcad74f2 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 2 Nov 2020 12:25:22 -0500 Subject: [PATCH 2/4] v1.0.0-beta.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2a04e445..e6f30647 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ziggy-js", - "version": "1.0.0-beta.3", + "version": "1.0.0-beta.4", "description": "Use your Laravel named routes in JavaScript.", "keywords": [ "laravel", From f67575241cd439a0c30595929f1a87761a7ea60f Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Tue, 3 Nov 2020 09:58:54 -0500 Subject: [PATCH 3/4] Fix Ziggy class to allow passing an array of groups (#351) * Add failing test * Remove typehint from Ziggy $group argument --- src/Ziggy.php | 2 +- tests/Unit/BladeRouteGeneratorTest.php | 32 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Ziggy.php b/src/Ziggy.php index 3f29e45f..58d52ba9 100644 --- a/src/Ziggy.php +++ b/src/Ziggy.php @@ -15,7 +15,7 @@ class Ziggy implements JsonSerializable protected $group; protected $routes; - public function __construct(string $group = null, string $url = null) + public function __construct($group = null, string $url = null) { $this->group = $group; diff --git a/tests/Unit/BladeRouteGeneratorTest.php b/tests/Unit/BladeRouteGeneratorTest.php index 1f23bf05..16ef4e3d 100644 --- a/tests/Unit/BladeRouteGeneratorTest.php +++ b/tests/Unit/BladeRouteGeneratorTest.php @@ -96,6 +96,38 @@ public function can_generate_routes_for_custom_domain() $this->assertStringContainsString(json_encode($expected), (new BladeRouteGenerator)->generate()); } + /** @test */ + public function can_generate_routes_for_given_group_or_groups() + { + $router = app('router'); + $router->get('posts', $this->noop())->name('posts.index'); + $router->get('posts/{post}', $this->noop())->name('posts.show'); + $router->get('users/{user}', $this->noop())->name('users.show'); + $router->getRoutes()->refreshNameLookups(); + + config(['ziggy.groups' => [ + 'guest' => ['posts.*'], + 'admin' => ['users.*'], + ]]); + + BladeRouteGenerator::$generated = false; + $output = (new BladeRouteGenerator)->generate('guest'); + $ziggy = json_decode(Str::after(Str::before($output, ";\n\n"), ' = '), true); + + $this->assertCount(2, $ziggy['routes']); + $this->assertArrayHasKey('posts.index', $ziggy['routes']); + $this->assertArrayHasKey('posts.show', $ziggy['routes']); + + BladeRouteGenerator::$generated = false; + $output = (new BladeRouteGenerator)->generate(['guest', 'admin']); + $ziggy = json_decode(Str::after(Str::before($output, ";\n\n"), ' = '), true); + + $this->assertCount(3, $ziggy['routes']); + $this->assertArrayHasKey('posts.index', $ziggy['routes']); + $this->assertArrayHasKey('posts.show', $ziggy['routes']); + $this->assertArrayHasKey('users.show', $ziggy['routes']); + } + /** @test */ public function can_set_csp_nonce() { From 4eb5935363643575df1f3d98628fc5b8ecee5b82 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Tue, 3 Nov 2020 10:00:09 -0500 Subject: [PATCH 4/4] v1.0.0-beta.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e6f30647..21de86a0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ziggy-js", - "version": "1.0.0-beta.4", + "version": "1.0.0-beta.5", "description": "Use your Laravel named routes in JavaScript.", "keywords": [ "laravel",