diff --git a/src/Commands/SyncModuleStatusesCommand.php b/src/Commands/SyncModuleStatusesCommand.php new file mode 100644 index 0000000..3e0898e --- /dev/null +++ b/src/Commands/SyncModuleStatusesCommand.php @@ -0,0 +1,102 @@ +error('Modules directory does not exist: ' . $modulesPath); + + return self::FAILURE; + } + + /** @var Collection $moduleNames */ + $moduleNames = $this->getModuleNames($modulesPath); + + if ($moduleNames->isEmpty()) { + $this->warn('No modules were found in the Modules directory.'); + + return self::SUCCESS; + } + + $createdCount = 0; + + foreach ($moduleNames as $moduleName) { + $moduleStatus = ModuleStatus::firstOrCreate( + ['module' => $moduleName], + ['enabled' => true] + ); + + if ($moduleStatus->wasRecentlyCreated) { + $createdCount++; + + $this->line(sprintf('Added module: %s', $moduleName)); + } else { + $this->line(sprintf('Already exists: %s', $moduleName)); + } + } + + $this->info(sprintf( + 'Module sync complete. %d module(s) found, %d new record(s) added.', + $moduleNames->count(), + $createdCount + )); + + return self::SUCCESS; + } + + /** + * Get all module directory names from the Modules path. + * + * @param string $modulesPath + * @return Collection + */ + protected function getModuleNames(string $modulesPath): Collection + { + return collect(File::directories($modulesPath)) + ->map(static function (string $directory): string { + return basename($directory); + }) + ->filter(static function (string $moduleName): bool { + return $moduleName !== ''; + }) + ->sort() + ->values(); + } +} diff --git a/src/DevelopmentSupport/Entities/ModuleStatus.php b/src/DevelopmentSupport/Entities/ModuleStatus.php new file mode 100644 index 0000000..45d14d0 --- /dev/null +++ b/src/DevelopmentSupport/Entities/ModuleStatus.php @@ -0,0 +1,39 @@ + + */ + protected $fillable = [ + 'module', + 'enabled', + ]; + + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'enabled' => 'boolean', + ]; +} diff --git a/src/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index 5f10f3b..6d1d708 100755 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -42,6 +42,7 @@ use mpba\Modules\Commands\SeedCommand; use mpba\Modules\Commands\SeedMakeCommand; use mpba\Modules\Commands\SetupCommand; use mpba\Modules\Commands\SupportMakeCommand; +use mpba\Modules\Commands\SyncModuleStatusesCommand; use mpba\Modules\Commands\TestMakeCommand; use mpba\Modules\Commands\TraitMakeCommand; use mpba\Modules\Commands\UnUseCommand; @@ -100,6 +101,7 @@ class ConsoleServiceProvider extends ServiceProvider TestMakeCommand::class, ComponentMakeCommand::class, TraitMakeCommand::class, + SyncModuleStatusesCommand::class, ]; /**