Skip to content

Commit 6db2fe1

Browse files
committed
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
1 parent d5929c8 commit 6db2fe1

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/Commands/ConsoleMakeCommand.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Savannabits\Modular\Commands;
4+
5+
6+
use Savannabits\Modular\Facades\Modular;
7+
use Savannabits\Modular\Support\Concerns\GeneratesModularFiles;
8+
9+
class ConsoleMakeCommand extends \Illuminate\Foundation\Console\ConsoleMakeCommand
10+
{
11+
use GeneratesModularFiles;
12+
protected $name = 'modular:make-command';
13+
14+
protected $description = 'Create a new Artisan command in a modular package';
15+
16+
protected function getStub(): string
17+
{
18+
$relativePath = '/stubs/console.stub';
19+
20+
return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/')))
21+
? $customPath
22+
: (file_exists($packagePath = Modular::packagePath(trim($relativePath, '/'))) ? $packagePath : __DIR__.$relativePath);
23+
}
24+
25+
protected function getRelativeNamespace(): string
26+
{
27+
return '\\Console\\Commands';
28+
}
29+
}

stubs/module.provider.stub

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class {{ class }} extends PackageServiceProvider
1616
{
1717
$package->name(static::$name)
1818
->hasViews(static::$viewNamespace)
19-
->hasConsoleCommands($this->getCommands())
19+
->hasCommands($this->getCommands())
2020
->hasInstallCommand(function (InstallCommand $command) {
2121
$command
2222
->askToRunMigrations()
@@ -48,7 +48,7 @@ class {{ class }} extends PackageServiceProvider
4848
{
4949
// Get an array of file names from the migrations directory
5050
$glob1 = glob($this->package->basePath('/../database/migrations/*.php'));
51-
$glob2 = glob($this->package->basePath('/../database/migrations/*.php.stub'));
51+
$glob2 = glob($this->package->basePath('/../database/migrations/**/*.php'));
5252

5353
return collect($glob1)
5454
->merge($glob2)
@@ -65,7 +65,10 @@ class {{ class }} extends PackageServiceProvider
6565
// automatically include all namespace classes in the Console directory
6666
// use glob to return full paths to all files in the Console directory
6767

68-
$paths = glob($this->package->basePath('/Console/*.php'));
68+
$paths = array_merge(
69+
glob($this->package->basePath('/Console/Commands/*.php')),
70+
glob($this->package->basePath('/Console/Commands/**/*.php'))
71+
);
6972

7073
return collect($paths)
7174
->map(fn ($filename) => $this->getNamespaceFromFile($filename)->toString())
@@ -74,15 +77,15 @@ class {{ class }} extends PackageServiceProvider
7477

7578
private function getNamespaceFromFile($path): Stringable
7679
{
77-
return $this->getFullNamespace(Str::of($path)->afterLast('src/')
80+
return $this->getFullNamespace(Str::of($path)->afterLast('app/')
7881
->replace('/', '\\')
7982
->studly()->rtrim('.php'));
8083
}
8184

8285
private function getFullNamespace(string $relativeNamespace = ''): Stringable
8386
{
8487
return Str::of(static::$name)->studly()
85-
->prepend('Vanadi\\')
88+
->prepend(trim(config('modular.namespace','Modules'),'\\').'\\')
8689
->append('\\')
8790
->append(Str::studly($relativeNamespace));
8891
}

0 commit comments

Comments
 (0)