Skip to content

Commit

Permalink
Add module make console command and auto register module command
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru committed Jun 28, 2024
1 parent 33d8dc5 commit 692a5d4
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 22 deletions.
58 changes: 58 additions & 0 deletions src/Console/Commands/ModuleMakeConsoleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace NorbyBaru\Modularize\Console\Commands;

use Illuminate\Support\Str;

class ModuleMakeConsoleCommand extends ModuleMakerCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:make:console
{name : The name of the command}
{--module= : Name of module controller should belong to}
{--force : Create the class even if the component already exists}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate console command for a module';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Console';

public function handle()
{
$module = $this->getModuleInput();
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if (! $path = $this->getFilePath(name: $name, force: $this->option('force'))) {
return true;
}

$this->generateFile($path, $name);

}

protected function logFileCreated(string $path, ?string $type = null)
{
parent::logFileCreated($path, 'Console command');
}

protected function getFolderPath(): string
{
return 'Console';
}
}
17 changes: 12 additions & 5 deletions src/Console/Commands/ModuleMakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ protected function getStub()
return $this->currentStub;
}

private function getTemplatePath(string $file): string
{
return __DIR__."/templates/{$file}sample";
}

public function getModuleInput(): string
{
if (! $this->module = $this->option('module')) {
Expand Down Expand Up @@ -128,12 +133,14 @@ protected function replaceName(&$stub, $name)
'SampleViewTitle',
'SampleUCtitle',
'{{viewFile}}',
'{{command}}',
],
replace: [
strtolower($name),
strtolower(Str::snake($title, '-')),
ucfirst(Str::studly($name)),
strtolower(Str::snake(str_replace(search: '/', replace: '.', subject: $title), '-')),
strtolower(str_replace(search: '/-', replace: ':', subject: Str::snake($title, '-'))),
],
subject: $stub
);
Expand Down Expand Up @@ -302,9 +309,9 @@ protected function getPluralName(string $name): string
->snake();
}

protected function getFilePath(string $name): ?string
protected function getFilePath(string $name, bool $force = false): ?string
{
if ($this->files->exists($path = $this->getPath($name))) {
if ($this->files->exists($path = $this->getPath($name)) && ! $force) {
$this->logFileExist($name);

return null;
Expand All @@ -328,13 +335,13 @@ protected function generateFile(string $path, string $filename, string $stubType

protected function setStubFile(string $file): void
{
$this->currentStub = $this->currentStub.$file.'sample';
$this->currentStub = $this->getTemplatePath($file);
}

protected function logFileCreated(string $path)
protected function logFileCreated(string $path, ?string $type = null)
{
if (! $this->hasOption('quiet') || ! $this->option('quiet')) {
$this->components->info(sprintf('%s [%s] created successfully.', $this->type, $path));
$this->components->info(sprintf('%s [%s] created successfully.', $type ?? $this->type, $path));
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/Console/Commands/templates/console.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace {{ namespace }};

use Illuminate\Console\Command;

class {{ class }} extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = '{{moduleName}}:{{command}}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
//
}
}
74 changes: 61 additions & 13 deletions src/ModularizeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace NorbyBaru\Modularize;

use Illuminate\Console\Command;
use Illuminate\Contracts\Foundation\CachesRoutes;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeComponentCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeConsoleCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeControllerCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeEventCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeJobCommand;
Expand All @@ -22,12 +25,18 @@
use NorbyBaru\Modularize\Console\Commands\ModuleMakeResourceCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeTestCommand;
use NorbyBaru\Modularize\Console\Commands\ModuleMakeViewCommand;
use ReflectionClass;
use Symfony\Component\Finder\Finder;

class ModularizeServiceProvider extends ServiceProvider
{
/** @var Filesystem */
protected $files;

protected string $moduleRootPath;

protected string $rootNamespace = 'Modules\\';

/**
* Bootstrap the application services.
*
Expand All @@ -37,27 +46,25 @@ public function boot()
{
$this->publishConfig();

$moduleRootPath = base_path(config('modularize.root_path'));

if (is_dir($moduleRootPath)) {

if (is_dir($this->moduleRootPath = base_path(config('modularize.root_path')))) {
if (! config('modularize.enable')) {
return;
}

$modules = array_map(
'class_basename',
$this->files->directories($moduleRootPath)
$this->files->directories($this->moduleRootPath)
);

foreach ($modules as $module) {
$this->autoloadServiceProvider($moduleRootPath, $module);
$this->autoloadConfig($moduleRootPath, $module);
$this->autoloadMigration($moduleRootPath, $module);
$this->autoloadRoutes($moduleRootPath, $module);
$this->autoloadHelper($moduleRootPath, $module);
$this->autoloadViews($moduleRootPath, $module);
$this->autoloadTranslations($moduleRootPath, $module);
$this->autoloadServiceProvider($this->moduleRootPath, $module);
$this->autoloadConfig($this->moduleRootPath, $module);
$this->autoloadConsoleCommands($this->moduleRootPath, $module);
$this->autoloadMigration($this->moduleRootPath, $module);
$this->autoloadRoutes($this->moduleRootPath, $module);
$this->autoloadHelper($this->moduleRootPath, $module);
$this->autoloadViews($this->moduleRootPath, $module);
$this->autoloadTranslations($this->moduleRootPath, $module);
$this->autoloadViewComponents($module);
}
}
Expand Down Expand Up @@ -119,6 +126,45 @@ private function autoloadMigration(string $moduleRootPath, string $module)
$this->loadMigrationsFrom("{$moduleRootPath}/{$module}/Database/migrations");
}

/**
* Load module console commands
*/
private function autoloadConsoleCommands(string $moduleRootPath, string $module): void
{
if (! $this->app->runningInConsole()) {
return;
}

$path = "{$moduleRootPath}/{$module}/Console";

$paths = array_unique(Arr::wrap($path));

$paths = array_filter($paths, function ($path) {
return is_dir($path);
});

if (empty($paths)) {
return;
}

$namespace = $this->rootNamespace;

foreach ((new Finder)->in($paths)->files() as $command) {
$command = $namespace.str_replace(
['/', '.php'],
['\\', ''],
Str::after($command->getRealPath(), realpath($this->moduleRootPath).DIRECTORY_SEPARATOR)
);

if (
is_subclass_of($command, Command::class)
&& ! (new ReflectionClass($command))->isAbstract()
) {
$this->commands($command);
}
}
}

/**
* Load module config.php file
*/
Expand Down Expand Up @@ -162,7 +208,8 @@ private function autoloadRoutes(string $moduleRootPath, string $module): void
if (! ($this->app instanceof CachesRoutes && $this->app->routesAreCached())) {
$routeFiles = [
$path.'/routes.php',
$path.'/Routes/',
$path.'/Routes/web.php',
$path.'/Routes/api.php',
];

foreach ($routeFiles as $path) {
Expand Down Expand Up @@ -228,6 +275,7 @@ protected function registerMakeCommand()
{
$this->commands([
ModuleMakeComponentCommand::class,
ModuleMakeConsoleCommand::class,
ModuleMakeControllerCommand::class,
ModuleMakeEventCommand::class,
ModuleMakeJobCommand::class,
Expand Down
68 changes: 68 additions & 0 deletions tests/Commands/MakeConsoleCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace NorbyBaru\Modularize\Tests\Commands;

use NorbyBaru\Modularize\Tests\MakeCommandTestCase;

class MakeConsoleCommandTest extends MakeCommandTestCase
{
public function test_it_should_create_a_console_command()
{
$this->artisan(
command: 'module:make:console',
parameters: [
'name' => 'SendEmails',
'--module' => $this->moduleName,
]
)->assertSuccessful();

$this->assertFileExists(filename: $this->getModulePath().'/Console/SendEmails.php');
}

public function test_it_should_create_a_console_command_with_force_option()
{
$this->artisan(
command: 'module:make:console',
parameters: [
'name' => 'SendEmails',
'--module' => $this->moduleName,
]
)->assertSuccessful();

$this->assertFileExists(filename: $this->getModulePath().'/Console/SendEmails.php');

$this->artisan(
command: 'module:make:console',
parameters: [
'name' => 'SendEmails',
'--module' => $this->moduleName,
'--force' => true,
]
)->assertSuccessful();

$this->assertFileExists(filename: $this->getModulePath().'/Console/SendEmails.php');
}

public function test_it_should_fail_to_create_console_command_on_duplicate_filename()
{
$this->artisan(
command: 'module:make:console',
parameters: [
'name' => 'SendEmails',
'--module' => $this->moduleName,
]
)->assertSuccessful();

$this->assertFileExists(filename: $this->getModulePath().'/Console/SendEmails.php');

$this->artisan(
command: 'module:make:console',
parameters: [
'name' => 'SendEmails',
'--module' => $this->moduleName,
]
)->assertFailed();

$this->assertFileExists(filename: $this->getModulePath().'/Console/SendEmails.php');
}
}
4 changes: 2 additions & 2 deletions tests/Commands/MakeModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_it_creates_a_model_with_migration()
directory: $this->getModulePath($module).'/Database/migrations'
);
$this->assertFileExists(filename: $this->getModulePath($module).'/Models/Video.php');

// TODO: Fix timestamp issue in test
// $this->assertFileExists(filename: $this->getModulePath().'/Database/migration/'.$datetime.'_create_posts_table.php');
}
Expand Down Expand Up @@ -79,7 +79,7 @@ public function test_it_creates_a_model_with_migration_and_factory()
$this->assertFileExists(
filename: $this->getModulePath($module).'/Models/Listing.php'
);

// TODO: Fix timestamp issue in test
// $this->assertFileExists(
// filename: $this->getModulePath().'/Database/migration/'.$datetime.'_create_posts_table.php'
Expand Down
Loading

0 comments on commit 692a5d4

Please sign in to comment.