|
16 | 16 | */
|
17 | 17 | trait BootTrait {
|
18 | 18 |
|
| 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 | + |
19 | 40 | /**
|
20 | 41 | * @internal
|
21 | 42 | */
|
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. |
23 | 47 | $definition->addOption(new InputOption('level', NULL, InputOption::VALUE_REQUIRED, 'Bootstrap level (none,classloader,settings,full,cms-only,cms-full)', $defaultLevel));
|
24 | 48 | $definition->addOption(new InputOption('hostname', NULL, InputOption::VALUE_REQUIRED, 'Hostname (for a multisite system)'));
|
25 | 49 | $definition->addOption(new InputOption('test', 't', InputOption::VALUE_NONE, 'Bootstrap the test database (CIVICRM_UF=UnitTests)'));
|
26 | 50 | $definition->addOption(new InputOption('user', 'U', InputOption::VALUE_REQUIRED, 'CMS user'));
|
27 | 51 | }
|
28 | 52 |
|
| 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 | + */ |
29 | 83 | public function boot(InputInterface $input, OutputInterface $output) {
|
30 | 84 | $logger = $this->bootLogger($output);
|
31 | 85 | $logger->debug('Start');
|
@@ -306,4 +360,20 @@ protected function assertBooted() {
|
306 | 360 | }
|
307 | 361 | }
|
308 | 362 |
|
| 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 | + |
309 | 379 | }
|
0 commit comments