Skip to content

Commit

Permalink
BaseApplication - Support for dispatching commands from extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Sep 24, 2024
1 parent ed59c8f commit c6b689b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/src/BaseApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Civi\Cv\Util\BootTrait;
use Civi\Cv\Util\CvArgvInput;
use LesserEvil\ShellVerbosityIsEvil;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand All @@ -13,6 +14,8 @@

class BaseApplication extends \Symfony\Component\Console\Application {

protected $stage = 'new';

/**
* Primary entry point for execution of the standalone command.
*/
Expand Down Expand Up @@ -52,6 +55,37 @@ public function configure() {
'commands' => $this->createCommands(),
])['commands'];
$this->addCommands($commands);
$this->stage = 'configured';
}

public function find($name) {
switch ($this->stage) {
case 'new':
case 'extended':
return parent::find($name);

case 'configured':
try {
return parent::find($name);
}
catch (CommandNotFoundException $e) {
$this->stage = 'extended';
$c = new class() {
use BootTrait;
};

$okLevels = ['full|cms-full', 'full', 'cms-full'];
if (in_array(Cv::input()->getOption('level'), $okLevels)) {
$c->boot(Cv::input(), Cv::output());
return parent::find($name);
}
else {
$output = is_callable([Cv::output(), 'getErrorOutput']) ? Cv::output()->getErrorOutput() : Cv::output();
$output->writeln(sprintf("<error>WARNING: When using bootstrap --level=%s, some commands may be unavailable.</error>", Cv::input()->getOption('level')));
throw $e;
}
}
}
}

/**
Expand Down

0 comments on commit c6b689b

Please sign in to comment.