From a755900fa8ba74c5856dbe3b924396e59e0d63dc Mon Sep 17 00:00:00 2001 From: Sam Maosa Date: Mon, 8 Apr 2024 17:48:26 +0300 Subject: [PATCH 1/3] Code cleanup using pint --- src/Commands/ConsoleMakeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ConsoleMakeCommand.php b/src/Commands/ConsoleMakeCommand.php index 6649d55..096f5a9 100644 --- a/src/Commands/ConsoleMakeCommand.php +++ b/src/Commands/ConsoleMakeCommand.php @@ -2,13 +2,13 @@ namespace Savannabits\Modular\Commands; - use Savannabits\Modular\Facades\Modular; use Savannabits\Modular\Support\Concerns\GeneratesModularFiles; class ConsoleMakeCommand extends \Illuminate\Foundation\Console\ConsoleMakeCommand { use GeneratesModularFiles; + protected $name = 'modular:make-command'; protected $description = 'Create a new Artisan command in a modular package'; From 8fa2120b09eb45e5c19e2d5f951497762eed8ec1 Mon Sep 17 00:00:00 2001 From: Sam Maosa Date: Mon, 8 Apr 2024 19:02:14 +0300 Subject: [PATCH 2/3] New Feature: Service Provider Generation - Updated the module.provider generator to discover and register other providers under Providers/ - Added a modular:make-provider command --- src/Commands/ProviderMakeCommand.php | 64 ++++++++++++++++++++++++++++ stubs/module.provider.stub | 28 ++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/Commands/ProviderMakeCommand.php diff --git a/src/Commands/ProviderMakeCommand.php b/src/Commands/ProviderMakeCommand.php new file mode 100644 index 0000000..c1c7d5f --- /dev/null +++ b/src/Commands/ProviderMakeCommand.php @@ -0,0 +1,64 @@ +resolveStubPath('/stubs/provider.stub'); + } + + protected function getRelativeNamespace(): string + { + return '\\Providers'; + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getOptions(): array + { + return [ + ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the provider already exists'], + ]; + } +} + diff --git a/stubs/module.provider.stub b/stubs/module.provider.stub index 4be1326..009eb99 100644 --- a/stubs/module.provider.stub +++ b/stubs/module.provider.stub @@ -25,6 +25,13 @@ class {{ class }} extends PackageServiceProvider $this->loadMigrationsFrom($this->package->basePath('/../database/migrations')); } + public function packageRegistered(): void + { + foreach ($this->getProviders() as $provider) { + $this->app->register($provider); + } + } + private function furtherInstallationSteps(InstallCommand $command) { // @@ -44,6 +51,13 @@ class {{ class }} extends PackageServiceProvider ]); } + private function getProviders(): array + { + return array_merge($this->discoverProviders(), [ + // Your other providers + ]); + } + private function discoverMigrations(): array { // Get an array of file names from the migrations directory @@ -74,6 +88,20 @@ class {{ class }} extends PackageServiceProvider ->map(fn ($filename) => $this->getNamespaceFromFile($filename)->toString()) ->toArray(); } + private function discoverProviders(): array + { + // automatically include all namespace classes in the Console directory + // use glob to return full paths to all files in the Console directory + + $paths = array_merge( + glob($this->package->basePath('/Providers/*.php')), + glob($this->package->basePath('/Providers/**/*.php')) + ); + + return collect($paths) + ->map(fn ($filename) => $this->getNamespaceFromFile($filename)->toString()) + ->toArray(); + } private function getNamespaceFromFile($path): Stringable { From d0baef6a99611615021509524bb378e9708b1105 Mon Sep 17 00:00:00 2001 From: Sam Maosa Date: Mon, 8 Apr 2024 19:03:54 +0300 Subject: [PATCH 3/3] Ran laravel pint on the package to fix code style --- src/Commands/ProviderMakeCommand.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Commands/ProviderMakeCommand.php b/src/Commands/ProviderMakeCommand.php index c1c7d5f..4fe85c4 100644 --- a/src/Commands/ProviderMakeCommand.php +++ b/src/Commands/ProviderMakeCommand.php @@ -3,7 +3,6 @@ namespace Savannabits\Modular\Commands; use Illuminate\Console\GeneratorCommand; -use Illuminate\Support\ServiceProvider; use Savannabits\Modular\Support\Concerns\GeneratesModularFiles; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; @@ -12,6 +11,7 @@ class ProviderMakeCommand extends GeneratorCommand { use GeneratesModularFiles; + /** * The console command name. * @@ -36,8 +36,6 @@ public function handle(): ?bool /** * Get the stub file for the generator. - * - * @return string */ protected function getStub(): string { @@ -51,8 +49,6 @@ protected function getRelativeNamespace(): string /** * Get the console command arguments. - * - * @return array */ protected function getOptions(): array { @@ -61,4 +57,3 @@ protected function getOptions(): array ]; } } -