From 12dd749293d75bf6d91ab701373d742b1e5ee434 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Fri, 3 Dec 2021 08:29:00 +0100 Subject: [PATCH 01/11] feat: allow custom output via templates --- src/BladeRouteGenerator.php | 27 ++++++++++++++---------- src/CommandRouteGenerator.php | 9 ++++---- tests/Unit/CommandRouteGeneratorTest.php | 16 ++++++++++++-- tests/fixtures/ziggy-custom.js | 4 ++++ 4 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 tests/fixtures/ziggy-custom.js diff --git a/src/BladeRouteGenerator.php b/src/BladeRouteGenerator.php index 59f36920..3b5f2274 100644 --- a/src/BladeRouteGenerator.php +++ b/src/BladeRouteGenerator.php @@ -17,29 +17,34 @@ public function generate($group = null, $nonce = null) } $routeFunction = $this->getRouteFunction(); + $ziggy = $payload->toJson(); + + $template = config()->get('ziggy.templates.file', << + const Ziggy = :ziggy; - static::$generated = true; + :routeFunction + +HTML); - return << - const Ziggy = {$payload->toJson()}; + static::$generated = true; - $routeFunction - -HTML; + return strtr($template, [ ':ziggy' => $ziggy, ':nonce' => $nonce, ':routeFunction' => $routeFunction ]); } private function generateMergeJavascript($json, $nonce) { - return << + $template = config()->get('ziggy.templates.javascript', << (function () { - const routes = {$json}; + const routes = :json; Object.assign(Ziggy.routes, routes); })(); -HTML; +HTML); + + return strtr($template, [ ':json' => $json, ':nonce' => $nonce ]); } private function getRouteFilePath() diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index bb8d7e04..08e6a78c 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -38,9 +38,8 @@ public function handle() private function generate($group = false) { $payload = (new Ziggy($group, $this->option('url') ? url($this->option('url')) : null))->toJson(); - - return <<get('ziggy.templates.file', << $payload ]); } protected function makeDirectory($path) diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index 49fd980a..b1b154ea 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -80,7 +80,19 @@ public function can_generate_file_with_custom_pathname() /** @test */ public function can_generate_file_with_config_applied() { - config(['ziggy.except' => ['admin.*']]); + config(['ziggy' => [ + 'except' => ['admin.*'], + 'templates' => [ + 'file' => <<get('posts/{post}/comments', $this->noop())->name('postComments.index'); $router->get('admin', $this->noop())->name('admin.dashboard'); // Excluded, should NOT be present in file @@ -88,7 +100,7 @@ public function can_generate_file_with_config_applied() Artisan::call('ziggy:generate'); - $this->assertFileEquals('./tests/fixtures/ziggy.js', base_path('resources/js/ziggy.js')); + $this->assertFileEquals('./tests/fixtures/ziggy-custom.js', base_path('resources/js/ziggy.js')); } /** @test */ diff --git a/tests/fixtures/ziggy-custom.js b/tests/fixtures/ziggy-custom.js new file mode 100644 index 00000000..f2c5dc8b --- /dev/null +++ b/tests/fixtures/ziggy-custom.js @@ -0,0 +1,4 @@ +// This is a custom template +const Ziggy = {"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"postComments.index":{"uri":"posts\/{post}\/comments","methods":["GET","HEAD"]}}}; + +export { Ziggy }; From 9be734a33d56ffb26a9129f288edb3d10a3c3e5b Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Fri, 3 Dec 2021 10:11:26 +0100 Subject: [PATCH 02/11] feat: use formatters instead of templates --- src/BladeRouteGenerator.php | 34 ++++++++---------------- src/CommandRouteGenerator.php | 17 ++++-------- src/Formatters/FileFormatter.php | 28 +++++++++++++++++++ src/Formatters/MergeScriptFormatter.php | 32 ++++++++++++++++++++++ src/Formatters/ScriptFormatter.php | 31 +++++++++++++++++++++ tests/Formatters/CustomFileFormatter.php | 18 +++++++++++++ tests/Unit/CommandRouteGeneratorTest.php | 14 +++------- 7 files changed, 129 insertions(+), 45 deletions(-) create mode 100644 src/Formatters/FileFormatter.php create mode 100644 src/Formatters/MergeScriptFormatter.php create mode 100644 src/Formatters/ScriptFormatter.php create mode 100644 tests/Formatters/CustomFileFormatter.php diff --git a/src/BladeRouteGenerator.php b/src/BladeRouteGenerator.php index 3b5f2274..0db0a3d1 100644 --- a/src/BladeRouteGenerator.php +++ b/src/BladeRouteGenerator.php @@ -2,49 +2,37 @@ namespace Tightenco\Ziggy; +use Tightenco\Ziggy\Formatters\MergeScriptFormatter; +use Tightenco\Ziggy\Formatters\ScriptFormatter; + class BladeRouteGenerator { public static $generated; public function generate($group = null, $nonce = null) { - $payload = new Ziggy($group); + $ziggy = new Ziggy($group); $nonce = $nonce ? ' nonce="' . $nonce . '"' : ''; if (static::$generated) { - return $this->generateMergeJavascript(json_encode($payload->toArray()['routes']), $nonce); + return $this->generateMergeJavascript($ziggy, $nonce); } $routeFunction = $this->getRouteFunction(); - $ziggy = $payload->toJson(); - - $template = config()->get('ziggy.templates.file', << - const Ziggy = :ziggy; - - :routeFunction - -HTML); static::$generated = true; + + $formatter = config()->get('ziggy.formatters.script', ScriptFormatter::class); - return strtr($template, [ ':ziggy' => $ziggy, ':nonce' => $nonce, ':routeFunction' => $routeFunction ]); + return (new $formatter($ziggy, $routeFunction, $nonce))->format(); } - private function generateMergeJavascript($json, $nonce) + private function generateMergeJavascript(Ziggy $ziggy, $nonce) { - $template = config()->get('ziggy.templates.javascript', << - (function () { - const routes = :json; - - Object.assign(Ziggy.routes, routes); - })(); - -HTML); + $formatter = config()->get('ziggy.formatters.mergeScript', MergeScriptFormatter::class); - return strtr($template, [ ':json' => $json, ':nonce' => $nonce ]); + return (new $formatter($ziggy, $nonce))->format(); } private function getRouteFilePath() diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index 08e6a78c..ae6f4552 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; +use Tightenco\Ziggy\Formatters\FileFormatter; use Tightenco\Ziggy\Ziggy; class CommandRouteGenerator extends Command @@ -37,19 +38,11 @@ public function handle() private function generate($group = false) { - $payload = (new Ziggy($group, $this->option('url') ? url($this->option('url')) : null))->toJson(); - $template = config()->get('ziggy.templates.file', <<option('url') ? url($this->option('url')) : null)); + + $formatter = config()->get('ziggy.formatters.file', FileFormatter::class); -if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') { - Object.assign(Ziggy.routes, window.Ziggy.routes); -} - -export { Ziggy }; - -JAVASCRIPT); - - return strtr($template, [ ':payload' => $payload ]); + return (new $formatter($ziggy))->format(); } protected function makeDirectory($path) diff --git a/src/Formatters/FileFormatter.php b/src/Formatters/FileFormatter.php new file mode 100644 index 00000000..0a1ec9ad --- /dev/null +++ b/src/Formatters/FileFormatter.php @@ -0,0 +1,28 @@ +ziggy = $ziggy; + } + + public function format() { + return <<ziggy->toJson()}; + +if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') { + Object.assign(Ziggy.routes, window.Ziggy.routes); +} + +export { Ziggy }; + +JAVASCRIPT; + } +} \ No newline at end of file diff --git a/src/Formatters/MergeScriptFormatter.php b/src/Formatters/MergeScriptFormatter.php new file mode 100644 index 00000000..0f31c67e --- /dev/null +++ b/src/Formatters/MergeScriptFormatter.php @@ -0,0 +1,32 @@ +ziggy = $ziggy; + $this->nonce = $nonce; + } + + public function format() { + $routes = json_encode($this->ziggy->toArray()['routes']); + + return <<nonce}> + (function () { + const routes = {$routes}; + + Object.assign(Ziggy.routes, routes); + })(); + +HTML; + } +} \ No newline at end of file diff --git a/src/Formatters/ScriptFormatter.php b/src/Formatters/ScriptFormatter.php new file mode 100644 index 00000000..559557e8 --- /dev/null +++ b/src/Formatters/ScriptFormatter.php @@ -0,0 +1,31 @@ +ziggy = $ziggy; + $this->routeFunction = $routeFunction; + $this->nonce = $nonce; + } + + public function format() { + return <<nonce}> + const Ziggy = {$this->ziggy->toJson()}; + + {$this->routeFunction} + +HTML; + } +} \ No newline at end of file diff --git a/tests/Formatters/CustomFileFormatter.php b/tests/Formatters/CustomFileFormatter.php new file mode 100644 index 00000000..d1c12fcf --- /dev/null +++ b/tests/Formatters/CustomFileFormatter.php @@ -0,0 +1,18 @@ +ziggy->toJson()}; + +export { Ziggy }; + +JAVASCRIPT; + } +} \ No newline at end of file diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index b1b154ea..c3f7e327 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -4,8 +4,8 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\URL; -use Illuminate\Support\Str; use Tests\TestCase; +use Tests\Formatters\CustomFileFormatter; class CommandRouteGeneratorTest extends TestCase { @@ -82,15 +82,9 @@ public function can_generate_file_with_config_applied() { config(['ziggy' => [ 'except' => ['admin.*'], - 'templates' => [ - 'file' => << [ + 'file' => CustomFileFormatter::class, + ] ]]); $router = app('router'); From 36d303aa19a4fe1944a9f72685495630534061ea Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Fri, 3 Dec 2021 10:16:33 +0100 Subject: [PATCH 03/11] fix: remove types --- src/Formatters/FileFormatter.php | 2 +- src/Formatters/MergeScriptFormatter.php | 2 +- src/Formatters/ScriptFormatter.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Formatters/FileFormatter.php b/src/Formatters/FileFormatter.php index 0a1ec9ad..886fa323 100644 --- a/src/Formatters/FileFormatter.php +++ b/src/Formatters/FileFormatter.php @@ -6,7 +6,7 @@ class FileFormatter { - protected Ziggy $ziggy; + protected $ziggy; public function __construct(Ziggy $ziggy) { diff --git a/src/Formatters/MergeScriptFormatter.php b/src/Formatters/MergeScriptFormatter.php index 0f31c67e..1e8f6ade 100644 --- a/src/Formatters/MergeScriptFormatter.php +++ b/src/Formatters/MergeScriptFormatter.php @@ -6,7 +6,7 @@ class MergeScriptFormatter { - protected Ziggy $ziggy; + protected $ziggy; protected string $nonce; diff --git a/src/Formatters/ScriptFormatter.php b/src/Formatters/ScriptFormatter.php index 559557e8..919ddfcd 100644 --- a/src/Formatters/ScriptFormatter.php +++ b/src/Formatters/ScriptFormatter.php @@ -6,7 +6,7 @@ class ScriptFormatter { - protected Ziggy $ziggy; + protected $ziggy; protected string $routeFunction; From f5196cee568852f6633afd484a348e8d01414b1f Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Fri, 3 Dec 2021 10:55:16 -0500 Subject: [PATCH 04/11] Formatting --- src/BladeRouteGenerator.php | 2 +- src/CommandRouteGenerator.php | 2 +- src/Formatters/FileFormatter.php | 31 ++++++++-------- src/Formatters/MergeScriptFormatter.php | 48 ++++++++++++------------- src/Formatters/ScriptFormatter.php | 45 ++++++++++++----------- 5 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/BladeRouteGenerator.php b/src/BladeRouteGenerator.php index 0db0a3d1..78b5b2b8 100644 --- a/src/BladeRouteGenerator.php +++ b/src/BladeRouteGenerator.php @@ -22,7 +22,7 @@ public function generate($group = null, $nonce = null) $routeFunction = $this->getRouteFunction(); static::$generated = true; - + $formatter = config()->get('ziggy.formatters.script', ScriptFormatter::class); return (new $formatter($ziggy, $routeFunction, $nonce))->format(); diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index ae6f4552..03c664c5 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -39,7 +39,7 @@ public function handle() private function generate($group = false) { $ziggy = (new Ziggy($group, $this->option('url') ? url($this->option('url')) : null)); - + $formatter = config()->get('ziggy.formatters.file', FileFormatter::class); return (new $formatter($ziggy))->format(); diff --git a/src/Formatters/FileFormatter.php b/src/Formatters/FileFormatter.php index 886fa323..26f2e497 100644 --- a/src/Formatters/FileFormatter.php +++ b/src/Formatters/FileFormatter.php @@ -6,23 +6,24 @@ class FileFormatter { - protected $ziggy; + protected $ziggy; - public function __construct(Ziggy $ziggy) - { - $this->ziggy = $ziggy; - } + public function __construct(Ziggy $ziggy) + { + $this->ziggy = $ziggy; + } - public function format() { - return <<ziggy->toJson()}; + public function format() + { + return <<ziggy->toJson()}; -if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') { - Object.assign(Ziggy.routes, window.Ziggy.routes); -} + if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') { + Object.assign(Ziggy.routes, window.Ziggy.routes); + } -export { Ziggy }; + export { Ziggy }; -JAVASCRIPT; - } -} \ No newline at end of file + JAVASCRIPT; + } +} diff --git a/src/Formatters/MergeScriptFormatter.php b/src/Formatters/MergeScriptFormatter.php index 1e8f6ade..bfc96329 100644 --- a/src/Formatters/MergeScriptFormatter.php +++ b/src/Formatters/MergeScriptFormatter.php @@ -6,27 +6,27 @@ class MergeScriptFormatter { - protected $ziggy; - - protected string $nonce; - - public function __construct(Ziggy $ziggy, string $nonce = '') - { - $this->ziggy = $ziggy; - $this->nonce = $nonce; - } - - public function format() { - $routes = json_encode($this->ziggy->toArray()['routes']); - - return <<nonce}> - (function () { - const routes = {$routes}; - - Object.assign(Ziggy.routes, routes); - })(); - -HTML; - } -} \ No newline at end of file + protected $ziggy; + protected $nonce; + + public function __construct(Ziggy $ziggy, string $nonce = '') + { + $this->ziggy = $ziggy; + $this->nonce = $nonce; + } + + public function format() + { + $routes = json_encode($this->ziggy->toArray()['routes']); + + return <<nonce}> + (function () { + const routes = {$routes}; + + Object.assign(Ziggy.routes, routes); + })(); + + HTML; + } +} diff --git a/src/Formatters/ScriptFormatter.php b/src/Formatters/ScriptFormatter.php index 919ddfcd..63a62555 100644 --- a/src/Formatters/ScriptFormatter.php +++ b/src/Formatters/ScriptFormatter.php @@ -6,26 +6,25 @@ class ScriptFormatter { - protected $ziggy; - - protected string $routeFunction; - - protected string $nonce; - - public function __construct(Ziggy $ziggy, string $routeFunction, string $nonce = '') - { - $this->ziggy = $ziggy; - $this->routeFunction = $routeFunction; - $this->nonce = $nonce; - } - - public function format() { - return <<nonce}> - const Ziggy = {$this->ziggy->toJson()}; - - {$this->routeFunction} - -HTML; - } -} \ No newline at end of file + protected $ziggy; + protected $routeFunction; + protected $nonce; + + public function __construct(Ziggy $ziggy, string $routeFunction, string $nonce = '') + { + $this->ziggy = $ziggy; + $this->routeFunction = $routeFunction; + $this->nonce = $nonce; + } + + public function format() + { + return <<nonce}> + const Ziggy = {$this->ziggy->toJson()}; + + {$this->routeFunction} + + HTML; + } +} From 3fb3bb597b20835b6502ca624610b3228a9f09b1 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Mon, 21 Feb 2022 08:48:20 +0000 Subject: [PATCH 05/11] fix: implement Stringable --- src/BladeRouteGenerator.php | 4 ++-- src/CommandRouteGenerator.php | 2 +- src/Formatters/FileFormatter.php | 5 +++-- src/Formatters/MergeScriptFormatter.php | 5 +++-- src/Formatters/ScriptFormatter.php | 5 +++-- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/BladeRouteGenerator.php b/src/BladeRouteGenerator.php index 78b5b2b8..31e0b6a9 100644 --- a/src/BladeRouteGenerator.php +++ b/src/BladeRouteGenerator.php @@ -25,14 +25,14 @@ public function generate($group = null, $nonce = null) $formatter = config()->get('ziggy.formatters.script', ScriptFormatter::class); - return (new $formatter($ziggy, $routeFunction, $nonce))->format(); + return (string)(new $formatter($ziggy, $routeFunction, $nonce)); } private function generateMergeJavascript(Ziggy $ziggy, $nonce) { $formatter = config()->get('ziggy.formatters.mergeScript', MergeScriptFormatter::class); - return (new $formatter($ziggy, $nonce))->format(); + return (string)(new $formatter($ziggy, $nonce)); } private function getRouteFilePath() diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index 03c664c5..b1a9db5b 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -42,7 +42,7 @@ private function generate($group = false) $formatter = config()->get('ziggy.formatters.file', FileFormatter::class); - return (new $formatter($ziggy))->format(); + return (string)(new $formatter($ziggy)); } protected function makeDirectory($path) diff --git a/src/Formatters/FileFormatter.php b/src/Formatters/FileFormatter.php index 26f2e497..fd44767b 100644 --- a/src/Formatters/FileFormatter.php +++ b/src/Formatters/FileFormatter.php @@ -2,9 +2,10 @@ namespace Tightenco\Ziggy\Formatters; +use Stringable; use Tightenco\Ziggy\Ziggy; -class FileFormatter +class FileFormatter implements Stringable { protected $ziggy; @@ -13,7 +14,7 @@ public function __construct(Ziggy $ziggy) $this->ziggy = $ziggy; } - public function format() + public function __toString() { return <<ziggy->toJson()}; diff --git a/src/Formatters/MergeScriptFormatter.php b/src/Formatters/MergeScriptFormatter.php index bfc96329..c8136466 100644 --- a/src/Formatters/MergeScriptFormatter.php +++ b/src/Formatters/MergeScriptFormatter.php @@ -2,9 +2,10 @@ namespace Tightenco\Ziggy\Formatters; +use Stringable; use Tightenco\Ziggy\Ziggy; -class MergeScriptFormatter +class MergeScriptFormatter implements Stringable { protected $ziggy; protected $nonce; @@ -15,7 +16,7 @@ public function __construct(Ziggy $ziggy, string $nonce = '') $this->nonce = $nonce; } - public function format() + public function __toString() { $routes = json_encode($this->ziggy->toArray()['routes']); diff --git a/src/Formatters/ScriptFormatter.php b/src/Formatters/ScriptFormatter.php index 63a62555..045b6159 100644 --- a/src/Formatters/ScriptFormatter.php +++ b/src/Formatters/ScriptFormatter.php @@ -2,9 +2,10 @@ namespace Tightenco\Ziggy\Formatters; +use Stringable; use Tightenco\Ziggy\Ziggy; -class ScriptFormatter +class ScriptFormatter implements Stringable { protected $ziggy; protected $routeFunction; @@ -17,7 +18,7 @@ public function __construct(Ziggy $ziggy, string $routeFunction, string $nonce = $this->nonce = $nonce; } - public function format() + public function __toString() { return <<nonce}> From a2aee615287f3acbe47c043b24c0864676ed34bc Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 28 Feb 2022 18:13:23 -0500 Subject: [PATCH 06/11] Fix test --- tests/Formatters/CustomFileFormatter.php | 4 ++-- tests/Unit/CommandRouteGeneratorTest.php | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/Formatters/CustomFileFormatter.php b/tests/Formatters/CustomFileFormatter.php index d1c12fcf..a6cf5be6 100644 --- a/tests/Formatters/CustomFileFormatter.php +++ b/tests/Formatters/CustomFileFormatter.php @@ -6,7 +6,7 @@ class CustomFileFormatter extends FileFormatter { - public function format() { + public function __toString() { return <<ziggy->toJson()}; @@ -15,4 +15,4 @@ public function format() { JAVASCRIPT; } -} \ No newline at end of file +} diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index b89ae94e..92645a2e 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -4,8 +4,8 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\URL; -use Tests\TestCase; use Tests\Formatters\CustomFileFormatter; +use Tests\TestCase; class CommandRouteGeneratorTest extends TestCase { @@ -80,17 +80,32 @@ public function can_generate_file_with_custom_pathname() /** @test */ public function can_generate_file_with_config_applied() + { + config(['ziggy.except' => ['admin.*']]); + + $router = app('router'); + $router->get('posts/{post}/comments', $this->noop())->name('postComments.index'); + $router->get('slashes/{slug}', $this->noop())->where('slug', '.*')->name('slashes'); + $router->get('admin', $this->noop())->name('admin.dashboard'); // Excluded, should NOT be present in file + $router->getRoutes()->refreshNameLookups(); + + Artisan::call('ziggy:generate'); + + $this->assertFileEquals('./tests/fixtures/ziggy.js', base_path('resources/js/ziggy.js')); + } + + /** @test */ + public function can_generate_file_with_custom_output_formatter() { config(['ziggy' => [ 'except' => ['admin.*'], 'formatters' => [ 'file' => CustomFileFormatter::class, - ] + ], ]]); $router = app('router'); $router->get('posts/{post}/comments', $this->noop())->name('postComments.index'); - $router->get('slashes/{slug}', $this->noop())->where('slug', '.*')->name('slashes'); $router->get('admin', $this->noop())->name('admin.dashboard'); // Excluded, should NOT be present in file $router->getRoutes()->refreshNameLookups(); From e7def10bb3bb7400c944926e9d2f737acbab3124 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 28 Feb 2022 18:16:43 -0500 Subject: [PATCH 07/11] Formatting --- src/BladeRouteGenerator.php | 6 +++--- src/CommandRouteGenerator.php | 2 +- src/Formatters/FileFormatter.php | 14 +++++++------- src/Formatters/MergeScriptFormatter.php | 16 ++++++++-------- src/Formatters/ScriptFormatter.php | 12 ++++++------ tests/Formatters/CustomFileFormatter.php | 18 ------------------ tests/Unit/CommandRouteGeneratorTest.php | 15 ++++++++++++++- tests/fixtures/ziggy-custom.js | 1 - 8 files changed, 39 insertions(+), 45 deletions(-) delete mode 100644 tests/Formatters/CustomFileFormatter.php diff --git a/src/BladeRouteGenerator.php b/src/BladeRouteGenerator.php index 31e0b6a9..93f7db44 100644 --- a/src/BladeRouteGenerator.php +++ b/src/BladeRouteGenerator.php @@ -16,7 +16,7 @@ public function generate($group = null, $nonce = null) $nonce = $nonce ? ' nonce="' . $nonce . '"' : ''; if (static::$generated) { - return $this->generateMergeJavascript($ziggy, $nonce); + return (string) $this->generateMergeJavascript($ziggy, $nonce); } $routeFunction = $this->getRouteFunction(); @@ -25,14 +25,14 @@ public function generate($group = null, $nonce = null) $formatter = config()->get('ziggy.formatters.script', ScriptFormatter::class); - return (string)(new $formatter($ziggy, $routeFunction, $nonce)); + return (string) new $formatter($ziggy, $routeFunction, $nonce); } private function generateMergeJavascript(Ziggy $ziggy, $nonce) { $formatter = config()->get('ziggy.formatters.mergeScript', MergeScriptFormatter::class); - return (string)(new $formatter($ziggy, $nonce)); + return new $formatter($ziggy, $nonce); } private function getRouteFilePath() diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index b1a9db5b..26720058 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -42,7 +42,7 @@ private function generate($group = false) $formatter = config()->get('ziggy.formatters.file', FileFormatter::class); - return (string)(new $formatter($ziggy)); + return (string) new $formatter($ziggy); } protected function makeDirectory($path) diff --git a/src/Formatters/FileFormatter.php b/src/Formatters/FileFormatter.php index fd44767b..131e4f78 100644 --- a/src/Formatters/FileFormatter.php +++ b/src/Formatters/FileFormatter.php @@ -14,17 +14,17 @@ public function __construct(Ziggy $ziggy) $this->ziggy = $ziggy; } - public function __toString() + public function __toString(): string { return <<ziggy->toJson()}; +const Ziggy = {$this->ziggy->toJson()}; - if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') { - Object.assign(Ziggy.routes, window.Ziggy.routes); - } +if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') { + Object.assign(Ziggy.routes, window.Ziggy.routes); +} - export { Ziggy }; +export { Ziggy }; - JAVASCRIPT; +JAVASCRIPT; } } diff --git a/src/Formatters/MergeScriptFormatter.php b/src/Formatters/MergeScriptFormatter.php index c8136466..c279dd66 100644 --- a/src/Formatters/MergeScriptFormatter.php +++ b/src/Formatters/MergeScriptFormatter.php @@ -16,18 +16,18 @@ public function __construct(Ziggy $ziggy, string $nonce = '') $this->nonce = $nonce; } - public function __toString() + public function __toString(): string { $routes = json_encode($this->ziggy->toArray()['routes']); return <<nonce}> - (function () { - const routes = {$routes}; + - HTML; + Object.assign(Ziggy.routes, routes); + })(); + +HTML; } } diff --git a/src/Formatters/ScriptFormatter.php b/src/Formatters/ScriptFormatter.php index 045b6159..28435a65 100644 --- a/src/Formatters/ScriptFormatter.php +++ b/src/Formatters/ScriptFormatter.php @@ -18,14 +18,14 @@ public function __construct(Ziggy $ziggy, string $routeFunction, string $nonce = $this->nonce = $nonce; } - public function __toString() + public function __toString(): string { return <<nonce}> - const Ziggy = {$this->ziggy->toJson()}; + - HTML; + {$this->routeFunction} + +HTML; } } diff --git a/tests/Formatters/CustomFileFormatter.php b/tests/Formatters/CustomFileFormatter.php deleted file mode 100644 index a6cf5be6..00000000 --- a/tests/Formatters/CustomFileFormatter.php +++ /dev/null @@ -1,18 +0,0 @@ -ziggy->toJson()}; - -export { Ziggy }; - -JAVASCRIPT; - } -} diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index 92645a2e..dfa2603f 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -4,8 +4,8 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\URL; -use Tests\Formatters\CustomFileFormatter; use Tests\TestCase; +use Tightenco\Ziggy\Formatters\FileFormatter; class CommandRouteGeneratorTest extends TestCase { @@ -131,3 +131,16 @@ public function can_generate_file_for_specific_configured_route_group() $this->assertFileEquals('./tests/fixtures/admin.js', base_path('resources/js/admin.js')); } } + +class CustomFileFormatter extends FileFormatter +{ + public function __toString(): string + { + return <<ziggy->toJson()}; + export { Ziggy }; + + JAVASCRIPT; + } +} diff --git a/tests/fixtures/ziggy-custom.js b/tests/fixtures/ziggy-custom.js index f2c5dc8b..db3690c6 100644 --- a/tests/fixtures/ziggy-custom.js +++ b/tests/fixtures/ziggy-custom.js @@ -1,4 +1,3 @@ // This is a custom template const Ziggy = {"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"postComments.index":{"uri":"posts\/{post}\/comments","methods":["GET","HEAD"]}}}; - export { Ziggy }; From 946914481d01befa58f07ce65988a4ef3d881b51 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 28 Feb 2022 18:51:34 -0500 Subject: [PATCH 08/11] Move and rename formatter classes --- src/BladeRouteGenerator.php | 6 +++--- src/CommandRouteGenerator.php | 4 ++-- .../FileFormatter.php => Output/File.php} | 4 ++-- .../MergeScript.php} | 4 ++-- .../ScriptFormatter.php => Output/Script.php} | 12 ++++++------ tests/Unit/CommandRouteGeneratorTest.php | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) rename src/{Formatters/FileFormatter.php => Output/File.php} (85%) rename src/{Formatters/MergeScriptFormatter.php => Output/MergeScript.php} (86%) rename src/{Formatters/ScriptFormatter.php => Output/Script.php} (56%) diff --git a/src/BladeRouteGenerator.php b/src/BladeRouteGenerator.php index 93f7db44..17d8f203 100644 --- a/src/BladeRouteGenerator.php +++ b/src/BladeRouteGenerator.php @@ -2,8 +2,8 @@ namespace Tightenco\Ziggy; -use Tightenco\Ziggy\Formatters\MergeScriptFormatter; -use Tightenco\Ziggy\Formatters\ScriptFormatter; +use Tightenco\Ziggy\Output\MergeScript; +use Tightenco\Ziggy\Output\Script; class BladeRouteGenerator { @@ -30,7 +30,7 @@ public function generate($group = null, $nonce = null) private function generateMergeJavascript(Ziggy $ziggy, $nonce) { - $formatter = config()->get('ziggy.formatters.mergeScript', MergeScriptFormatter::class); + $formatter = config()->get('ziggy.formatters.mergeScript', MergeScript::class); return new $formatter($ziggy, $nonce); } diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index 26720058..b20775c1 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -4,7 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; -use Tightenco\Ziggy\Formatters\FileFormatter; +use Tightenco\Ziggy\Output\File; use Tightenco\Ziggy\Ziggy; class CommandRouteGenerator extends Command @@ -40,7 +40,7 @@ private function generate($group = false) { $ziggy = (new Ziggy($group, $this->option('url') ? url($this->option('url')) : null)); - $formatter = config()->get('ziggy.formatters.file', FileFormatter::class); + $formatter = config()->get('ziggy.formatters.file', File::class); return (string) new $formatter($ziggy); } diff --git a/src/Formatters/FileFormatter.php b/src/Output/File.php similarity index 85% rename from src/Formatters/FileFormatter.php rename to src/Output/File.php index 131e4f78..ef908030 100644 --- a/src/Formatters/FileFormatter.php +++ b/src/Output/File.php @@ -1,11 +1,11 @@ ziggy = $ziggy; - $this->routeFunction = $routeFunction; + $this->function = $function; $this->nonce = $nonce; } @@ -24,7 +24,7 @@ public function __toString(): string HTML; } diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index dfa2603f..b43c28c1 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\URL; use Tests\TestCase; -use Tightenco\Ziggy\Formatters\FileFormatter; +use Tightenco\Ziggy\Output\File; class CommandRouteGeneratorTest extends TestCase { @@ -132,7 +132,7 @@ public function can_generate_file_for_specific_configured_route_group() } } -class CustomFileFormatter extends FileFormatter +class CustomFileFormatter extends File { public function __toString(): string { From 421a5c61f49537011ea5a12754681e525de1a030 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 28 Feb 2022 18:56:02 -0500 Subject: [PATCH 09/11] Get formatter classes from container --- src/BladeRouteGenerator.php | 10 +++------- src/CommandRouteGenerator.php | 4 +--- tests/Unit/CommandRouteGeneratorTest.php | 16 ++++++---------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/BladeRouteGenerator.php b/src/BladeRouteGenerator.php index 17d8f203..2ddf7623 100644 --- a/src/BladeRouteGenerator.php +++ b/src/BladeRouteGenerator.php @@ -19,20 +19,16 @@ public function generate($group = null, $nonce = null) return (string) $this->generateMergeJavascript($ziggy, $nonce); } - $routeFunction = $this->getRouteFunction(); + $function = $this->getRouteFunction(); static::$generated = true; - $formatter = config()->get('ziggy.formatters.script', ScriptFormatter::class); - - return (string) new $formatter($ziggy, $routeFunction, $nonce); + return (string) app(Script::class, ['ziggy' => $ziggy, 'function' => $function, 'nonce' => $nonce]); } private function generateMergeJavascript(Ziggy $ziggy, $nonce) { - $formatter = config()->get('ziggy.formatters.mergeScript', MergeScript::class); - - return new $formatter($ziggy, $nonce); + return app(MergeScript::class, ['ziggy' => $ziggy, 'nonce' => $nonce]); } private function getRouteFilePath() diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index b20775c1..73a07ddf 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -40,9 +40,7 @@ private function generate($group = false) { $ziggy = (new Ziggy($group, $this->option('url') ? url($this->option('url')) : null)); - $formatter = config()->get('ziggy.formatters.file', File::class); - - return (string) new $formatter($ziggy); + return (string) app(File::class, ['ziggy' => $ziggy]); } protected function makeDirectory($path) diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index b43c28c1..9fd3ed85 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -97,12 +97,8 @@ public function can_generate_file_with_config_applied() /** @test */ public function can_generate_file_with_custom_output_formatter() { - config(['ziggy' => [ - 'except' => ['admin.*'], - 'formatters' => [ - 'file' => CustomFileFormatter::class, - ], - ]]); + config(['ziggy' => ['except' => ['admin.*']]]); + app()->bind(File::class, CustomFileFormatter::class); $router = app('router'); $router->get('posts/{post}/comments', $this->noop())->name('postComments.index'); @@ -137,10 +133,10 @@ class CustomFileFormatter extends File public function __toString(): string { return <<ziggy->toJson()}; - export { Ziggy }; +// This is a custom template +const Ziggy = {$this->ziggy->toJson()}; +export { Ziggy }; - JAVASCRIPT; +JAVASCRIPT; } } From 08081b7a197be7f2ffcfc42ab83bd29634dd9e09 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 28 Feb 2022 18:59:00 -0500 Subject: [PATCH 10/11] Formatting --- tests/Unit/CommandRouteGeneratorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index 9fd3ed85..3cb983d7 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -82,7 +82,6 @@ public function can_generate_file_with_custom_pathname() public function can_generate_file_with_config_applied() { config(['ziggy.except' => ['admin.*']]); - $router = app('router'); $router->get('posts/{post}/comments', $this->noop())->name('postComments.index'); $router->get('slashes/{slug}', $this->noop())->where('slug', '.*')->name('slashes'); From 120b7674325817020f5bc5a8d1338540f4957a63 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Fri, 4 Mar 2022 12:40:56 -0500 Subject: [PATCH 11/11] Get output classes from config --- src/BladeRouteGenerator.php | 8 ++++++-- src/CommandRouteGenerator.php | 4 +++- tests/Unit/CommandRouteGeneratorTest.php | 10 ++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/BladeRouteGenerator.php b/src/BladeRouteGenerator.php index 2ddf7623..9af5f2b7 100644 --- a/src/BladeRouteGenerator.php +++ b/src/BladeRouteGenerator.php @@ -23,12 +23,16 @@ public function generate($group = null, $nonce = null) static::$generated = true; - return (string) app(Script::class, ['ziggy' => $ziggy, 'function' => $function, 'nonce' => $nonce]); + $output = config('ziggy.output.script', Script::class); + + return (string) new $output($ziggy, $function, $nonce); } private function generateMergeJavascript(Ziggy $ziggy, $nonce) { - return app(MergeScript::class, ['ziggy' => $ziggy, 'nonce' => $nonce]); + $output = config('ziggy.output.merge_script', MergeScript::class); + + return new $output($ziggy, $nonce); } private function getRouteFilePath() diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index 73a07ddf..92ff9ef0 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -40,7 +40,9 @@ private function generate($group = false) { $ziggy = (new Ziggy($group, $this->option('url') ? url($this->option('url')) : null)); - return (string) app(File::class, ['ziggy' => $ziggy]); + $output = config('ziggy.output.file', File::class); + + return (string) new $output($ziggy); } protected function makeDirectory($path) diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index 3cb983d7..843f1d74 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -96,8 +96,14 @@ public function can_generate_file_with_config_applied() /** @test */ public function can_generate_file_with_custom_output_formatter() { - config(['ziggy' => ['except' => ['admin.*']]]); - app()->bind(File::class, CustomFileFormatter::class); + config([ + 'ziggy' => [ + 'except' => ['admin.*'], + 'output' => [ + 'file' => CustomFileFormatter::class, + ], + ], + ]); $router = app('router'); $router->get('posts/{post}/comments', $this->noop())->name('postComments.index');