Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions src/Console/Commands/Modularize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,56 @@

use InterNACHI\Modular\Support\ModuleConfig;
use InterNACHI\Modular\Support\ModuleRegistry;
use function Laravel\Prompts\select;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputOption;

trait Modularize
{
protected ?string $module = null;

private function moduleRegistry(): ModuleRegistry
{
return $this->getLaravel()->make(ModuleRegistry::class);
}

public function handle()
{
if ($this->input->hasParameterOption('--module')) {
$modules = $this->moduleRegistry()->modules()->keys();

$this->module = $this->option('module') ?: (string) select('Which module?', $modules);
}

parent::handle();
}

protected function module(): ?ModuleConfig
{
if ($name = $this->option('module')) {
$registry = $this->getLaravel()->make(ModuleRegistry::class);
if ($module = $registry->module($name)) {
return $module;
}
throw new InvalidOptionException(sprintf('The "%s" module does not exist.', $name));
if ($this->module === null) {
return null;
}

$config = $this->moduleRegistry()->module($this->module);

if ($config === null) {
throw new InvalidOptionException(sprintf('The "%s" module does not exist.', $this->module));
}
return null;

return $config;
}

protected function configure()
{
parent::configure();

$this->getDefinition()->addOption(
new InputOption(
'--module',
null,
InputOption::VALUE_REQUIRED,
'Run inside an application module'
InputOption::VALUE_OPTIONAL,
'Run inside an application module',
false
)
);
}
Expand Down