From 6db2fe18737040bc00831f2c63e9fb5fc88cab15 Mon Sep 17 00:00:00 2001 From: Sam Maosa Date: Mon, 8 Apr 2024 17:46:23 +0300 Subject: [PATCH] New Feature: Console Command Generator Command added - Bug Fixes: Proper discovery of commands and namespace generation in the module.provider generator - Added ConsoleMakeCommand and tested with a generation of a couple of commands --- src/Commands/ConsoleMakeCommand.php | 29 +++++++++++++++++++++++++++++ stubs/module.provider.stub | 13 ++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/Commands/ConsoleMakeCommand.php diff --git a/src/Commands/ConsoleMakeCommand.php b/src/Commands/ConsoleMakeCommand.php new file mode 100644 index 0000000..6649d55 --- /dev/null +++ b/src/Commands/ConsoleMakeCommand.php @@ -0,0 +1,29 @@ +laravel->basePath(trim($relativePath, '/'))) + ? $customPath + : (file_exists($packagePath = Modular::packagePath(trim($relativePath, '/'))) ? $packagePath : __DIR__.$relativePath); + } + + protected function getRelativeNamespace(): string + { + return '\\Console\\Commands'; + } +} diff --git a/stubs/module.provider.stub b/stubs/module.provider.stub index fe19587..4be1326 100644 --- a/stubs/module.provider.stub +++ b/stubs/module.provider.stub @@ -16,7 +16,7 @@ class {{ class }} extends PackageServiceProvider { $package->name(static::$name) ->hasViews(static::$viewNamespace) - ->hasConsoleCommands($this->getCommands()) + ->hasCommands($this->getCommands()) ->hasInstallCommand(function (InstallCommand $command) { $command ->askToRunMigrations() @@ -48,7 +48,7 @@ class {{ class }} extends PackageServiceProvider { // Get an array of file names from the migrations directory $glob1 = glob($this->package->basePath('/../database/migrations/*.php')); - $glob2 = glob($this->package->basePath('/../database/migrations/*.php.stub')); + $glob2 = glob($this->package->basePath('/../database/migrations/**/*.php')); return collect($glob1) ->merge($glob2) @@ -65,7 +65,10 @@ class {{ class }} extends PackageServiceProvider // automatically include all namespace classes in the Console directory // use glob to return full paths to all files in the Console directory - $paths = glob($this->package->basePath('/Console/*.php')); + $paths = array_merge( + glob($this->package->basePath('/Console/Commands/*.php')), + glob($this->package->basePath('/Console/Commands/**/*.php')) + ); return collect($paths) ->map(fn ($filename) => $this->getNamespaceFromFile($filename)->toString()) @@ -74,7 +77,7 @@ class {{ class }} extends PackageServiceProvider private function getNamespaceFromFile($path): Stringable { - return $this->getFullNamespace(Str::of($path)->afterLast('src/') + return $this->getFullNamespace(Str::of($path)->afterLast('app/') ->replace('/', '\\') ->studly()->rtrim('.php')); } @@ -82,7 +85,7 @@ class {{ class }} extends PackageServiceProvider private function getFullNamespace(string $relativeNamespace = ''): Stringable { return Str::of(static::$name)->studly() - ->prepend('Vanadi\\') + ->prepend(trim(config('modular.namespace','Modules'),'\\').'\\') ->append('\\') ->append(Str::studly($relativeNamespace)); }