From c8c41eddfb2c8cec8341beeecb33e85b99bffebf Mon Sep 17 00:00:00 2001 From: Sam Maosa Date: Mon, 8 Apr 2024 09:11:36 +0300 Subject: [PATCH 1/2] New Feat: Generate Views together with Tests - Added a view generator - Added an optional test generator for the view - BUG FIX: Added missing stubs in Commands - BUG FIX: Look for stubs in Commands as well as a fallback for all generators --- ...eCommand.php => ModuleActivateCommand.php} | 2 +- ...ommand.php => ModuleDeactivateCommand.php} | 2 +- ...oduleCommand.php => ModuleMakeCommand.php} | 2 +- src/Commands/ViewMakeCommand.php | 41 ++++++++++ src/Commands/stubs/api-routes.stub | 8 ++ src/Commands/stubs/broadcasting-routes.stub | 7 ++ src/Commands/stubs/cast.inbound.stub | 19 +++++ src/Commands/stubs/cast.stub | 29 +++++++ src/Commands/stubs/channel.stub | 24 ++++++ src/Commands/stubs/class.invokable.stub | 22 ++++++ src/Commands/stubs/class.stub | 14 ++++ src/Commands/stubs/console.stub | 30 +++++++ src/Commands/stubs/echo-bootstrap-js.stub | 7 ++ src/Commands/stubs/echo-js.stub | 14 ++++ src/Commands/stubs/enum.backed.stub | 8 ++ src/Commands/stubs/enum.stub | 8 ++ src/Commands/stubs/event.stub | 36 +++++++++ .../stubs/exception-render-report.stub | 26 +++++++ src/Commands/stubs/exception-render.stub | 18 +++++ src/Commands/stubs/exception-report.stub | 16 ++++ src/Commands/stubs/exception.stub | 10 +++ src/Commands/stubs/interface.stub | 8 ++ src/Commands/stubs/job.queued.stub | 30 +++++++ src/Commands/stubs/job.stub | 26 +++++++ src/Commands/stubs/listener.queued.stub | 27 +++++++ src/Commands/stubs/listener.stub | 25 ++++++ src/Commands/stubs/listener.typed.queued.stub | 28 +++++++ src/Commands/stubs/listener.typed.stub | 26 +++++++ src/Commands/stubs/mail.stub | 53 +++++++++++++ src/Commands/stubs/maintenance-mode.stub | 78 +++++++++++++++++++ src/Commands/stubs/markdown-mail.stub | 53 +++++++++++++ src/Commands/stubs/markdown-notification.stub | 51 ++++++++++++ src/Commands/stubs/markdown.stub | 12 +++ src/Commands/stubs/model.morph-pivot.stub | 10 +++ src/Commands/stubs/model.pivot.stub | 10 +++ src/Commands/stubs/model.stub | 11 +++ src/Commands/stubs/notification.stub | 54 +++++++++++++ src/Commands/stubs/observer.plain.stub | 8 ++ src/Commands/stubs/observer.stub | 48 ++++++++++++ src/Commands/stubs/pest.stub | 7 ++ src/Commands/stubs/pest.unit.stub | 5 ++ src/Commands/stubs/policy.plain.stub | 16 ++++ src/Commands/stubs/policy.stub | 66 ++++++++++++++++ src/Commands/stubs/provider.stub | 24 ++++++ src/Commands/stubs/request.stub | 28 +++++++ src/Commands/stubs/resource-collection.stub | 19 +++++ src/Commands/stubs/resource.stub | 19 +++++ src/Commands/stubs/routes.stub | 5 ++ src/Commands/stubs/rule.implicit.stub | 26 +++++++ src/Commands/stubs/rule.stub | 19 +++++ src/Commands/stubs/scope.stub | 18 +++++ src/Commands/stubs/test.stub | 20 +++++ src/Commands/stubs/test.unit.stub | 16 ++++ src/Commands/stubs/trait.stub | 8 ++ src/Commands/stubs/view-component.stub | 26 +++++++ src/Commands/stubs/view.pest.stub | 9 +++ src/Commands/stubs/view.stub | 3 + src/Commands/stubs/view.test.stub | 20 +++++ src/Modular.php | 2 +- src/Module.php | 5 ++ .../Concerns/GeneratesModularFiles.php | 9 ++- 61 files changed, 1266 insertions(+), 5 deletions(-) rename src/Commands/{ActivateModuleCommand.php => ModuleActivateCommand.php} (95%) rename src/Commands/{DeactivateModuleCommand.php => ModuleDeactivateCommand.php} (94%) rename src/Commands/{MakeModuleCommand.php => ModuleMakeCommand.php} (99%) create mode 100644 src/Commands/ViewMakeCommand.php create mode 100644 src/Commands/stubs/api-routes.stub create mode 100644 src/Commands/stubs/broadcasting-routes.stub create mode 100644 src/Commands/stubs/cast.inbound.stub create mode 100644 src/Commands/stubs/cast.stub create mode 100644 src/Commands/stubs/channel.stub create mode 100644 src/Commands/stubs/class.invokable.stub create mode 100644 src/Commands/stubs/class.stub create mode 100644 src/Commands/stubs/console.stub create mode 100644 src/Commands/stubs/echo-bootstrap-js.stub create mode 100644 src/Commands/stubs/echo-js.stub create mode 100644 src/Commands/stubs/enum.backed.stub create mode 100644 src/Commands/stubs/enum.stub create mode 100644 src/Commands/stubs/event.stub create mode 100644 src/Commands/stubs/exception-render-report.stub create mode 100644 src/Commands/stubs/exception-render.stub create mode 100644 src/Commands/stubs/exception-report.stub create mode 100644 src/Commands/stubs/exception.stub create mode 100644 src/Commands/stubs/interface.stub create mode 100644 src/Commands/stubs/job.queued.stub create mode 100644 src/Commands/stubs/job.stub create mode 100644 src/Commands/stubs/listener.queued.stub create mode 100644 src/Commands/stubs/listener.stub create mode 100644 src/Commands/stubs/listener.typed.queued.stub create mode 100644 src/Commands/stubs/listener.typed.stub create mode 100644 src/Commands/stubs/mail.stub create mode 100644 src/Commands/stubs/maintenance-mode.stub create mode 100644 src/Commands/stubs/markdown-mail.stub create mode 100644 src/Commands/stubs/markdown-notification.stub create mode 100644 src/Commands/stubs/markdown.stub create mode 100644 src/Commands/stubs/model.morph-pivot.stub create mode 100644 src/Commands/stubs/model.pivot.stub create mode 100644 src/Commands/stubs/model.stub create mode 100644 src/Commands/stubs/notification.stub create mode 100644 src/Commands/stubs/observer.plain.stub create mode 100644 src/Commands/stubs/observer.stub create mode 100644 src/Commands/stubs/pest.stub create mode 100644 src/Commands/stubs/pest.unit.stub create mode 100644 src/Commands/stubs/policy.plain.stub create mode 100644 src/Commands/stubs/policy.stub create mode 100644 src/Commands/stubs/provider.stub create mode 100644 src/Commands/stubs/request.stub create mode 100644 src/Commands/stubs/resource-collection.stub create mode 100644 src/Commands/stubs/resource.stub create mode 100644 src/Commands/stubs/routes.stub create mode 100644 src/Commands/stubs/rule.implicit.stub create mode 100644 src/Commands/stubs/rule.stub create mode 100644 src/Commands/stubs/scope.stub create mode 100644 src/Commands/stubs/test.stub create mode 100644 src/Commands/stubs/test.unit.stub create mode 100644 src/Commands/stubs/trait.stub create mode 100644 src/Commands/stubs/view-component.stub create mode 100644 src/Commands/stubs/view.pest.stub create mode 100644 src/Commands/stubs/view.stub create mode 100644 src/Commands/stubs/view.test.stub diff --git a/src/Commands/ActivateModuleCommand.php b/src/Commands/ModuleActivateCommand.php similarity index 95% rename from src/Commands/ActivateModuleCommand.php rename to src/Commands/ModuleActivateCommand.php index ceed144..8ccfa99 100644 --- a/src/Commands/ActivateModuleCommand.php +++ b/src/Commands/ModuleActivateCommand.php @@ -8,7 +8,7 @@ use function Laravel\Prompts\text; -class ActivateModuleCommand extends Command +class ModuleActivateCommand extends Command { public $signature = 'modular:activate {name?}'; diff --git a/src/Commands/DeactivateModuleCommand.php b/src/Commands/ModuleDeactivateCommand.php similarity index 94% rename from src/Commands/DeactivateModuleCommand.php rename to src/Commands/ModuleDeactivateCommand.php index 11fd989..b4a5205 100644 --- a/src/Commands/DeactivateModuleCommand.php +++ b/src/Commands/ModuleDeactivateCommand.php @@ -8,7 +8,7 @@ use function Laravel\Prompts\text; -class DeactivateModuleCommand extends Command +class ModuleDeactivateCommand extends Command { public $signature = 'modular:deactivate {name?}'; diff --git a/src/Commands/MakeModuleCommand.php b/src/Commands/ModuleMakeCommand.php similarity index 99% rename from src/Commands/MakeModuleCommand.php rename to src/Commands/ModuleMakeCommand.php index ecd04e0..ffcf284 100644 --- a/src/Commands/MakeModuleCommand.php +++ b/src/Commands/ModuleMakeCommand.php @@ -11,7 +11,7 @@ use function Laravel\Prompts\text; -class MakeModuleCommand extends Command +class ModuleMakeCommand extends Command { use CanManipulateFiles; diff --git a/src/Commands/ViewMakeCommand.php b/src/Commands/ViewMakeCommand.php new file mode 100644 index 0000000..368ad4c --- /dev/null +++ b/src/Commands/ViewMakeCommand.php @@ -0,0 +1,41 @@ +viewPath( + $this->getNameInput().'.'.$this->option('extension'), + ); + } + + protected function getTestPath(): string + { + return $this->getModule()->path( + Str::of($this->testClassFullyQualifiedName()) + ->replace('\\', '/') + ->replaceFirst('Tests/Feature', 'tests/Feature') + ->append('Test.php') + ->value() + ); + } + + protected function getTestStub(): string + { + $stubName = 'view.'.($this->usingPest() ? 'pest' : 'test').'.stub'; + $stubName = '/stubs/'.$stubName; + + return parent::resolveStubPath($stubName); + } +} diff --git a/src/Commands/stubs/api-routes.stub b/src/Commands/stubs/api-routes.stub new file mode 100644 index 0000000..ccc387f --- /dev/null +++ b/src/Commands/stubs/api-routes.stub @@ -0,0 +1,8 @@ +user(); +})->middleware('auth:sanctum'); diff --git a/src/Commands/stubs/broadcasting-routes.stub b/src/Commands/stubs/broadcasting-routes.stub new file mode 100644 index 0000000..df2ad28 --- /dev/null +++ b/src/Commands/stubs/broadcasting-routes.stub @@ -0,0 +1,7 @@ +id === (int) $id; +}); diff --git a/src/Commands/stubs/cast.inbound.stub b/src/Commands/stubs/cast.inbound.stub new file mode 100644 index 0000000..e74edf8 --- /dev/null +++ b/src/Commands/stubs/cast.inbound.stub @@ -0,0 +1,19 @@ + $attributes + */ + public function set(Model $model, string $key, mixed $value, array $attributes): mixed + { + return $value; + } +} diff --git a/src/Commands/stubs/cast.stub b/src/Commands/stubs/cast.stub new file mode 100644 index 0000000..35ddc47 --- /dev/null +++ b/src/Commands/stubs/cast.stub @@ -0,0 +1,29 @@ + $attributes + */ + public function get(Model $model, string $key, mixed $value, array $attributes): mixed + { + return $value; + } + + /** + * Prepare the given value for storage. + * + * @param array $attributes + */ + public function set(Model $model, string $key, mixed $value, array $attributes): mixed + { + return $value; + } +} diff --git a/src/Commands/stubs/channel.stub b/src/Commands/stubs/channel.stub new file mode 100644 index 0000000..46c1a53 --- /dev/null +++ b/src/Commands/stubs/channel.stub @@ -0,0 +1,24 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/src/Commands/stubs/exception-render-report.stub b/src/Commands/stubs/exception-render-report.stub new file mode 100644 index 0000000..9209c1d --- /dev/null +++ b/src/Commands/stubs/exception-render-report.stub @@ -0,0 +1,26 @@ + + */ + public function attachments(): array + { + return []; + } +} diff --git a/src/Commands/stubs/maintenance-mode.stub b/src/Commands/stubs/maintenance-mode.stub new file mode 100644 index 0000000..a90b8cd --- /dev/null +++ b/src/Commands/stubs/maintenance-mode.stub @@ -0,0 +1,78 @@ += time()) { + return; + } +} + +// Redirect to the proper path if necessary... +if (isset($data['redirect']) && $_SERVER['REQUEST_URI'] !== $data['redirect']) { + http_response_code(302); + header('Location: '.$data['redirect']); + + exit; +} + +// Output the prerendered template... +http_response_code($data['status'] ?? 503); + +if (isset($data['retry'])) { + header('Retry-After: '.$data['retry']); +} + +if (isset($data['refresh'])) { + header('Refresh: '.$data['refresh']); +} + +echo $data['template']; + +exit; diff --git a/src/Commands/stubs/markdown-mail.stub b/src/Commands/stubs/markdown-mail.stub new file mode 100644 index 0000000..191b46f --- /dev/null +++ b/src/Commands/stubs/markdown-mail.stub @@ -0,0 +1,53 @@ + + */ + public function attachments(): array + { + return []; + } +} diff --git a/src/Commands/stubs/markdown-notification.stub b/src/Commands/stubs/markdown-notification.stub new file mode 100644 index 0000000..cc99eac --- /dev/null +++ b/src/Commands/stubs/markdown-notification.stub @@ -0,0 +1,51 @@ + + */ + public function via(object $notifiable): array + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(object $notifiable): MailMessage + { + return (new MailMessage)->markdown('{{ view }}'); + } + + /** + * Get the array representation of the notification. + * + * @return array + */ + public function toArray(object $notifiable): array + { + return [ + // + ]; + } +} diff --git a/src/Commands/stubs/markdown.stub b/src/Commands/stubs/markdown.stub new file mode 100644 index 0000000..de9a155 --- /dev/null +++ b/src/Commands/stubs/markdown.stub @@ -0,0 +1,12 @@ + +# Introduction + +The body of your message. + + +Button Text + + +Thanks,
+{{ config('app.name') }} +
diff --git a/src/Commands/stubs/model.morph-pivot.stub b/src/Commands/stubs/model.morph-pivot.stub new file mode 100644 index 0000000..eb3f1f5 --- /dev/null +++ b/src/Commands/stubs/model.morph-pivot.stub @@ -0,0 +1,10 @@ + + */ + public function via(object $notifiable): array + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(object $notifiable): MailMessage + { + return (new MailMessage) + ->line('The introduction to the notification.') + ->action('Notification Action', url('/')) + ->line('Thank you for using our application!'); + } + + /** + * Get the array representation of the notification. + * + * @return array + */ + public function toArray(object $notifiable): array + { + return [ + // + ]; + } +} diff --git a/src/Commands/stubs/observer.plain.stub b/src/Commands/stubs/observer.plain.stub new file mode 100644 index 0000000..e2506b7 --- /dev/null +++ b/src/Commands/stubs/observer.plain.stub @@ -0,0 +1,8 @@ +get('/'); + + $response->assertStatus(200); +}); diff --git a/src/Commands/stubs/pest.unit.stub b/src/Commands/stubs/pest.unit.stub new file mode 100644 index 0000000..61cd84c --- /dev/null +++ b/src/Commands/stubs/pest.unit.stub @@ -0,0 +1,5 @@ +toBeTrue(); +}); diff --git a/src/Commands/stubs/policy.plain.stub b/src/Commands/stubs/policy.plain.stub new file mode 100644 index 0000000..c865775 --- /dev/null +++ b/src/Commands/stubs/policy.plain.stub @@ -0,0 +1,16 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/src/Commands/stubs/resource-collection.stub b/src/Commands/stubs/resource-collection.stub new file mode 100644 index 0000000..f75e481 --- /dev/null +++ b/src/Commands/stubs/resource-collection.stub @@ -0,0 +1,19 @@ + + */ + public function toArray(Request $request): array + { + return parent::toArray($request); + } +} diff --git a/src/Commands/stubs/resource.stub b/src/Commands/stubs/resource.stub new file mode 100644 index 0000000..ce8ece4 --- /dev/null +++ b/src/Commands/stubs/resource.stub @@ -0,0 +1,19 @@ + + */ + public function toArray(Request $request): array + { + return parent::toArray($request); + } +} diff --git a/src/Commands/stubs/routes.stub b/src/Commands/stubs/routes.stub new file mode 100644 index 0000000..fc7239f --- /dev/null +++ b/src/Commands/stubs/routes.stub @@ -0,0 +1,5 @@ +setCompiledRoutes( + {{routes}} +); diff --git a/src/Commands/stubs/rule.implicit.stub b/src/Commands/stubs/rule.implicit.stub new file mode 100644 index 0000000..341f842 --- /dev/null +++ b/src/Commands/stubs/rule.implicit.stub @@ -0,0 +1,26 @@ +get('/'); + + $response->assertStatus(200); + } +} diff --git a/src/Commands/stubs/test.unit.stub b/src/Commands/stubs/test.unit.stub new file mode 100644 index 0000000..7accf88 --- /dev/null +++ b/src/Commands/stubs/test.unit.stub @@ -0,0 +1,16 @@ +assertTrue(true); + } +} diff --git a/src/Commands/stubs/trait.stub b/src/Commands/stubs/trait.stub new file mode 100644 index 0000000..e409847 --- /dev/null +++ b/src/Commands/stubs/trait.stub @@ -0,0 +1,8 @@ +view('{{ name }}', [ + // + ]); + + $contents->assertSee(''); +}); diff --git a/src/Commands/stubs/view.stub b/src/Commands/stubs/view.stub new file mode 100644 index 0000000..6ecd80c --- /dev/null +++ b/src/Commands/stubs/view.stub @@ -0,0 +1,3 @@ +
+ +
diff --git a/src/Commands/stubs/view.test.stub b/src/Commands/stubs/view.test.stub new file mode 100644 index 0000000..8b9a9bf --- /dev/null +++ b/src/Commands/stubs/view.test.stub @@ -0,0 +1,20 @@ +view('{{ name }}', [ + // + ]); + + $contents->assertSee(''); + } +} diff --git a/src/Modular.php b/src/Modular.php index 07043c8..d1ab4c1 100755 --- a/src/Modular.php +++ b/src/Modular.php @@ -20,6 +20,6 @@ public function module(string $name): Module public function packagePath(string $path = ''): string { //return the base path of this package - return __DIR__.'/../'.($path ? DIRECTORY_SEPARATOR.trim($path, DIRECTORY_SEPARATOR) : ''); + return dirname(__DIR__.'../').($path ? DIRECTORY_SEPARATOR.trim($path, DIRECTORY_SEPARATOR) : ''); } } diff --git a/src/Module.php b/src/Module.php index 88bd4b8..772e0bd 100644 --- a/src/Module.php +++ b/src/Module.php @@ -106,4 +106,9 @@ public function srcPath(string $path = '', bool $relative = false): string { return $this->path('src'.DIRECTORY_SEPARATOR.trim($path, DIRECTORY_SEPARATOR), $relative); } + + public function resourcePath(string $string, bool $relative = false): string + { + return $this->path('resources'.DIRECTORY_SEPARATOR.trim($string, DIRECTORY_SEPARATOR), $relative); + } } diff --git a/src/Support/Concerns/GeneratesModularFiles.php b/src/Support/Concerns/GeneratesModularFiles.php index e45906a..c98a4d7 100644 --- a/src/Support/Concerns/GeneratesModularFiles.php +++ b/src/Support/Concerns/GeneratesModularFiles.php @@ -21,7 +21,7 @@ protected function resolveStubPath($stub): string { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath - : Modular::packagePath(trim($stub, DIRECTORY_SEPARATOR)); + : (file_exists($packagePath = Modular::packagePath(trim($stub, DIRECTORY_SEPARATOR))) ? $packagePath : Modular::packagePath('src/Commands/'.trim($stub,DIRECTORY_SEPARATOR))); } public function getModule(): Module @@ -66,4 +66,11 @@ protected function possibleModels() ->values() ->all(); } + + protected function viewPath($path = ''): string + { + $views = $this->getModule()->resourcePath('views'); + + return $views.($path ? DIRECTORY_SEPARATOR.$path : $path); + } } From 17dbe1fddd91a6cae75bdaa220fea4fab93c647a Mon Sep 17 00:00:00 2001 From: coolsam726 Date: Mon, 8 Apr 2024 06:13:10 +0000 Subject: [PATCH 2/2] Fix styling --- src/Commands/ViewMakeCommand.php | 2 +- src/Support/Concerns/GeneratesModularFiles.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/ViewMakeCommand.php b/src/Commands/ViewMakeCommand.php index 368ad4c..0f04d3f 100644 --- a/src/Commands/ViewMakeCommand.php +++ b/src/Commands/ViewMakeCommand.php @@ -3,12 +3,12 @@ namespace Savannabits\Modular\Commands; use Illuminate\Support\Str; -use Savannabits\Modular\Facades\Modular; use Savannabits\Modular\Support\Concerns\GeneratesModularFiles; class ViewMakeCommand extends \Illuminate\Foundation\Console\ViewMakeCommand { use GeneratesModularFiles; + protected $name = 'modular:make-view'; protected $description = 'Create a new view file in a modular package'; diff --git a/src/Support/Concerns/GeneratesModularFiles.php b/src/Support/Concerns/GeneratesModularFiles.php index c98a4d7..c8f1e62 100644 --- a/src/Support/Concerns/GeneratesModularFiles.php +++ b/src/Support/Concerns/GeneratesModularFiles.php @@ -21,7 +21,7 @@ protected function resolveStubPath($stub): string { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath - : (file_exists($packagePath = Modular::packagePath(trim($stub, DIRECTORY_SEPARATOR))) ? $packagePath : Modular::packagePath('src/Commands/'.trim($stub,DIRECTORY_SEPARATOR))); + : (file_exists($packagePath = Modular::packagePath(trim($stub, DIRECTORY_SEPARATOR))) ? $packagePath : Modular::packagePath('src/Commands/'.trim($stub, DIRECTORY_SEPARATOR))); } public function getModule(): Module