Skip to content

Commit 4cbdf55

Browse files
committed
Move getBootOptions (etc) from BaseApplication to BootTrait
1 parent e1e3d45 commit 4cbdf55

File tree

3 files changed

+74
-22
lines changed

3 files changed

+74
-22
lines changed

Diff for: lib/src/BaseApplication.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function getDefaultInputDefinition() {
7272
$c = new class() {
7373
use BootTrait;
7474
};
75-
$c->configureDefinition($definition);
75+
$c->mergeDefaultBootDefinition($definition);
7676

7777
return $definition;
7878
}

Diff for: lib/src/Command/BaseCommand.php

+2-20
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,17 @@ class BaseCommand extends Command {
2020
use OptionCallbackTrait;
2121
use BootTrait;
2222

23-
public function getBootOptions(): array {
24-
return [
25-
'auto' => TRUE,
26-
'default' => 'full|cms-full',
27-
'allow' => ['full|cms-full', 'full', 'cms-full', 'settings', 'classloader', 'cms-only', 'none'],
28-
];
29-
}
30-
3123
public function mergeApplicationDefinition($mergeArgs = TRUE) {
3224
parent::mergeApplicationDefinition($mergeArgs);
33-
$bootOptions = $this->getBootOptions();
34-
$this->getDefinition()->getOption('level')->setDefault($bootOptions['default']);
25+
$this->mergeBootDefinition($this->getDefinition());
3526
}
3627

3728
/**
3829
* @param \Symfony\Component\Console\Input\InputInterface $input
3930
* @param \Symfony\Component\Console\Output\OutputInterface $output
4031
*/
4132
protected function initialize(InputInterface $input, OutputInterface $output) {
42-
$bootOptions = $this->getBootOptions();
43-
if (!in_array($input->getOption('level'), $bootOptions['allow'])) {
44-
throw new \LogicException(sprintf("Command called with with level (%s) but only accepts levels (%s)",
45-
$input->getOption('level'), implode(', ', $bootOptions['allow'])));
46-
}
47-
48-
if (!$this->isBooted() && ($bootOptions['auto'] ?? TRUE)) {
49-
$this->boot($input, $output);
50-
}
51-
33+
$this->autoboot($input, $output);
5234
parent::initialize($input, $output);
5335
$this->runOptionCallbacks($input, $output);
5436
}

Diff for: lib/src/Util/BootTrait.php

+71-1
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,70 @@
1616
*/
1717
trait BootTrait {
1818

19+
/**
20+
* Describe the expected bootstrap behaviors for this command.
21+
*
22+
* - For most commands, you will want to automatically boot CiviCRM/CMS.
23+
* The default implementation will do this.
24+
* - For some special commands (e.g. core-installer or PHP-script-runner), you may
25+
* want more fine-grained control over when/how the system boots.
26+
*
27+
* @var array
28+
*/
29+
protected $bootOptions = [
30+
// Whether to automatically boot Civi during `initialize()` phase.
31+
'auto' => TRUE,
32+
33+
// Default boot level.
34+
'default' => 'full|cms-full',
35+
36+
// List of all boot levels that are allowed in this command.
37+
'allow' => ['full|cms-full', 'full', 'cms-full', 'settings', 'classloader', 'cms-only', 'none'],
38+
];
39+
1940
/**
2041
* @internal
2142
*/
22-
public function configureDefinition($definition, $defaultLevel = 'full|cms-full') {
43+
public function mergeDefaultBootDefinition($definition, $defaultLevel = 'full|cms-full') {
44+
// If we were only dealing with built-in/global commands, then these options could be defined at the command-level.
45+
// However, we also have extension-based commands. The system will boot before we have a chance to discover them.
46+
// By putting these options at the application level, we ensure they will be defined+used.
2347
$definition->addOption(new InputOption('level', NULL, InputOption::VALUE_REQUIRED, 'Bootstrap level (none,classloader,settings,full,cms-only,cms-full)', $defaultLevel));
2448
$definition->addOption(new InputOption('hostname', NULL, InputOption::VALUE_REQUIRED, 'Hostname (for a multisite system)'));
2549
$definition->addOption(new InputOption('test', 't', InputOption::VALUE_NONE, 'Bootstrap the test database (CIVICRM_UF=UnitTests)'));
2650
$definition->addOption(new InputOption('user', 'U', InputOption::VALUE_REQUIRED, 'CMS user'));
2751
}
2852

53+
/**
54+
* @internal
55+
*/
56+
public function mergeBootDefinition($definition) {
57+
$bootOptions = $this->getBootOptions();
58+
$definition->getOption('level')->setDefault($bootOptions['default']);
59+
}
60+
61+
/**
62+
* Evaluate the $bootOptions.
63+
*
64+
* - If we've already booted, do nothing.
65+
* - If the configuration looks reasonable and if we haven't booted yet, then boot().
66+
* - If the configuration looks unreasonable, then abort.
67+
*/
68+
protected function autoboot(InputInterface $input, OutputInterface $output): void {
69+
$bootOptions = $this->getBootOptions();
70+
if (!in_array($input->getOption('level'), $bootOptions['allow'])) {
71+
throw new \LogicException(sprintf("Command called with with level (%s) but only accepts levels (%s)",
72+
$input->getOption('level'), implode(', ', $bootOptions['allow'])));
73+
}
74+
75+
if (!$this->isBooted() && ($bootOptions['auto'] ?? TRUE)) {
76+
$this->boot($input, $output);
77+
}
78+
}
79+
80+
/**
81+
* Start CiviCRM and/or CMS. Respect options like --user and --level.
82+
*/
2983
public function boot(InputInterface $input, OutputInterface $output) {
3084
$logger = $this->bootLogger($output);
3185
$logger->debug('Start');
@@ -306,4 +360,20 @@ protected function assertBooted() {
306360
}
307361
}
308362

363+
/**
364+
* @return array{auto: bool, default: string, allow: string[]}
365+
*/
366+
public function getBootOptions(): array {
367+
return $this->bootOptions;
368+
}
369+
370+
/**
371+
* @param array{auto: bool, default: string, allow: string[]} $bootOptions
372+
* @return $this
373+
*/
374+
public function setBootOptions(array $bootOptions) {
375+
$this->bootOptions = $bootOptions;
376+
return $this;
377+
}
378+
309379
}

0 commit comments

Comments
 (0)