diff --git a/src/Commands/ProviderMakeCommand.php b/src/Commands/ProviderMakeCommand.php new file mode 100644 index 0000000..4fe85c4 --- /dev/null +++ b/src/Commands/ProviderMakeCommand.php @@ -0,0 +1,59 @@ +resolveStubPath('/stubs/provider.stub'); + } + + protected function getRelativeNamespace(): string + { + return '\\Providers'; + } + + /** + * Get the console command arguments. + */ + 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 {