From 901eb0bff89cccd67c64f67dd2d45a8925242f92 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 9 Feb 2024 14:30:52 +1300 Subject: [PATCH] FIX Don't bootstrap when running `behat -h` or `behat --help` --- src/Extension.php | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Extension.php b/src/Extension.php index 7658b2b4..88616587 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -16,6 +16,7 @@ use Behat\Testwork\ServiceContainer\ExtensionManager; use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface; use RuntimeException; +use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; @@ -42,6 +43,7 @@ class Extension implements ExtensionInterface */ const SILVERSTRIPE_ID = 'silverstripe_extension'; + private ?bool $shouldBootstrap = null; /** * {@inheritDoc} @@ -78,8 +80,10 @@ public function initialize(ExtensionManager $extensionManager) public function load(ContainerBuilder $container, array $config) { // Load yml config - $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config')); - $loader->load('silverstripe.yml'); + if ($this->getShouldBootstrap($container)) { + $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config')); + $loader->load('silverstripe.yml'); + } // Add CLI substitutions $this->loadSuiteLocator($container); @@ -105,8 +109,10 @@ public function load(ContainerBuilder $container, array $config) */ public function process(ContainerBuilder $container) { - $corePass = new Compiler\CoreInitializationPass(); - $corePass->process($container); + if ($this->getShouldBootstrap($container)) { + $corePass = new Compiler\CoreInitializationPass(); + $corePass->process($container); + } } public function configure(ArrayNodeDefinition $builder) @@ -203,4 +209,23 @@ protected function loadCallHandlers(ContainerBuilder $container, $errorReporting $definition->addTag(CallExtension::CALL_HANDLER_TAG, ['priority' => 50]); $container->setDefinition(CallExtension::CALL_HANDLER_TAG . '.runtime', $definition); } + + /** + * Check whether the extension should bootstrap or not. + * The extension should always bootstrap unless the `-h` or `--help` option is passed. + */ + private function getShouldBootstrap(ContainerBuilder $container): bool + { + if ($this->shouldBootstrap === null) { + if ($container->has('cli.input')) { + /** @var ArgvInput $input */ + $input = $container->get('cli.input'); + $this->shouldBootstrap = !$input->hasParameterOption(['--help', '-h']); + } else { + // If the input isn't there for some bizarre reason, just assume we should bootstrap. + $this->shouldBootstrap = true; + } + } + return $this->shouldBootstrap; + } }