Skip to content

Commit 079fb0b

Browse files
Refactor BaseCommand to improve module resolution and add support for statuses file loading
1 parent 6e1f50d commit 079fb0b

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

src/Commands/BaseCommand.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Console\PromptsForMissingInput;
99
use Illuminate\Support\Collection;
1010
use Nwidart\Modules\Contracts\ConfirmableCommand;
11+
use Nwidart\Modules\Module;
1112
use Symfony\Component\Console\Input\InputArgument;
1213
use Symfony\Component\Console\Input\InputInterface;
1314
use Symfony\Component\Console\Input\InputOption;
@@ -75,8 +76,10 @@ public function getConfirmableCallback(): \Closure|bool|null
7576
public function handle()
7677
{
7778
if ($this instanceof ConfirmableCommand) {
78-
if ($this->isProhibited() ||
79-
! $this->confirmToProceed($this->getConfirmableLabel(), $this->getConfirmableCallback())) {
79+
if (
80+
$this->isProhibited() ||
81+
! $this->confirmToProceed($this->getConfirmableLabel(), $this->getConfirmableCallback())
82+
) {
8083
return Command::FAILURE;
8184
}
8285
}
@@ -85,6 +88,7 @@ public function handle()
8588
$this->components->info($info);
8689
}
8790

91+
// Always resolve modules from the argument; ordering is handled in promptForMissingArguments()
8892
$modules = (array) $this->argument('module');
8993

9094
foreach ($modules as $module) {
@@ -94,9 +98,8 @@ public function handle()
9498

9599
protected function promptForMissingArguments(InputInterface $input, OutputInterface $output): void
96100
{
97-
$modules = $this->hasOption('direction')
98-
? array_keys($this->laravel['modules']->getOrdered($input->hasOption('direction')))
99-
: array_keys($this->laravel['modules']->all());
101+
// Prefer the order defined in the statuses file (modules.json) as configured in modules.activators.file.statuses-file
102+
$modules = $this->getModulesOrderedByStatusesFile();
100103

101104
if ($input->getOption(strtolower(self::ALL))) {
102105
$input->setArgument('module', $modules);
@@ -129,13 +132,53 @@ protected function promptForMissingArguments(InputInterface $input, OutputInterf
129132
);
130133
}
131134

132-
protected function getModuleModel($name)
135+
protected function getModuleModel($name): Module
133136
{
134137
return $name instanceof \Nwidart\Modules\Module
135138
? $name
136139
: $this->laravel['modules']->findOrFail($name);
137140
}
138141

142+
/**
143+
* Read the statuses file defined in config('modules.activators.file.statuses-file')
144+
* and return enabled module names preserving the order in that file.
145+
* Falls back to repository ordering if file is missing or invalid.
146+
*/
147+
private function getModulesOrderedByStatusesFile(): array
148+
{
149+
$statusesFile = $this->laravel['config']->get('modules.activators.file.statuses-file');
150+
$statusesFile = is_string($statusesFile) && $statusesFile !== ''
151+
? $statusesFile
152+
: base_path('modules_statuses.json');
153+
154+
$sort = $this->option('direction') ?? 'asc';
155+
156+
if (! is_string($statusesFile) || ! file_exists($statusesFile)) {
157+
return array_keys($this->laravel['modules']->getOrdered($sort));
158+
}
159+
160+
$json = @file_get_contents($statusesFile);
161+
$data = is_string($json) ? json_decode($json, true) : null;
162+
163+
if (! is_array($data)) {
164+
return array_keys($this->laravel['modules']->getOrdered($sort));
165+
}
166+
167+
$modules = [];
168+
foreach ($data as $name => $enabled) {
169+
if ($enabled === true || $enabled === 1) {
170+
$modules[] = $name; // Keep StudlyCase names as defined
171+
}
172+
}
173+
174+
// Fallback if no enabled modules found
175+
if (empty($modules)) {
176+
return array_keys($this->laravel['modules']->getOrdered($sort));
177+
}
178+
179+
return $modules;
180+
}
181+
139182
private function configureConfirmable(): void
140183
{
141184
$this->getDefinition()

0 commit comments

Comments
 (0)