Skip to content

Commit

Permalink
Merge pull request #23 from savannabits/0.x-dev
Browse files Browse the repository at this point in the history
New Feature: Console Command Generator Command added
  • Loading branch information
coolsam726 authored Apr 8, 2024
2 parents 68ba841 + 6db2fe1 commit c8a616e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
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

0 comments on commit c8a616e

Please sign in to comment.