Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Console Command Generator Command added #23

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Commands/ConsoleMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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';

protected function getStub(): string
{
$relativePath = '/stubs/console.stub';

return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/')))
? $customPath
: (file_exists($packagePath = Modular::packagePath(trim($relativePath, '/'))) ? $packagePath : __DIR__.$relativePath);
}

protected function getRelativeNamespace(): string
{
return '\\Console\\Commands';
}
}
13 changes: 8 additions & 5 deletions stubs/module.provider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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())
Expand All @@ -74,15 +77,15 @@ 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'));
}

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));
}
Expand Down
Loading