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(); } }