diff --git a/features/not_crashing_behat.feature b/features/not_crashing_behat.feature index fbad0c3..69f0135 100644 --- a/features/not_crashing_behat.feature +++ b/features/not_crashing_behat.feature @@ -3,7 +3,7 @@ Feature: Not crashing Behat As a Behat User I want to have Behat up and running after enabling this extension - Scenario: Not crashing Behat + Scenario: Successful boot the Symfony kernel with autoconfiguration Given a Behat configuration containing: """ default: @@ -29,7 +29,7 @@ Feature: Not crashing Behat When I run Behat Then it should pass - Scenario: Not crashing Behat with CrossContainerExtension + Scenario: Successful boot the Symfony kernel with explicit configuration Given a Behat configuration containing: """ default: @@ -37,17 +37,19 @@ Feature: Not crashing Behat FriendsOfBehat\SymfonyExtension: kernel: bootstrap: ~ - - FriendsOfBehat\CrossContainerExtension: ~ + path: app/MyKernel.php + class: MyKernel + env: test + debug: true """ - And a file "app/AppKernel.php" containing: + And a file "app/MyKernel.php" containing: """ null, + 'kernel' => [ + 'bootstrap' => 'app/autoload.php', + 'path' => 'app/AppKernel.php', + 'class' => 'AppKernel', + 'env' => self::DEFAULT_ENV, + 'debug' => self::DEFAULT_DEBUG_MODE + ] + ]; + + /** + * Default Symfony 4 configuration + */ + private const SYMFONY_4_DEFAULTS = [ + 'env_file' => '.env', + 'kernel' => [ + 'bootstrap' => null, + 'path' => 'src/Kernel.php', + 'class' => 'App\Kernel', + 'env' => self::DEFAULT_ENV, + 'debug' => self::DEFAULT_DEBUG_MODE + ] + ]; /** * @var CrossContainerProcessor|null @@ -91,18 +119,15 @@ final class SymfonyExtension implements Extension public function configure(ArrayNodeDefinition $builder): void { $builder - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('env_file')->defaultNull()->end() - ->arrayNode('kernel') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('bootstrap')->defaultValue('app/autoload.php')->end() - ->scalarNode('path')->defaultValue('app/AppKernel.php')->end() - ->scalarNode('class')->defaultValue('AppKernel')->end() - ->scalarNode('env')->defaultValue('test')->end() - ->booleanNode('debug')->defaultTrue()->end() - ->end() + ->children() + ->scalarNode('env_file')->end() + ->arrayNode('kernel') + ->children() + ->scalarNode('bootstrap')->end() + ->scalarNode('path')->end() + ->scalarNode('class')->end() + ->scalarNode('env')->end() + ->booleanNode('debug')->end() ->end() ->end() ->end() @@ -114,17 +139,7 @@ final class SymfonyExtension implements Extension */ public function load(ContainerBuilder $container, array $config): void { - if (null !== $config['env_file']) { - $envFilePath = sprintf('%s/%s', $container->getParameter('paths.base'), $config['env_file']); - $envFilePath = file_exists($envFilePath) ? $envFilePath : $envFilePath.'.dist'; - (new Dotenv())->load($envFilePath); - - $environment = false !== getenv('APP_ENV') ? getenv('APP_ENV') : self::DEFAULT_ENV; - $debugMode = false !== getenv('APP_DEBUG') ? getenv('APP_DEBUG') : self::DEBUG_MODE; - - $config['kernel']['env'] = $environment; - $config['kernel']['kernel'] = $debugMode; - } + $config = $this->autoconfigure($container, $config); $this->loadKernel($container, $config['kernel']); $this->loadKernelContainer($container); @@ -145,6 +160,47 @@ final class SymfonyExtension implements Extension { } + /** + * @param ContainerBuilder $container + * @param array $userConfig + * @return array + */ + private function autoconfigure(ContainerBuilder $container, array $userConfig) { + + $defaults = self::SYMFONY_DEFAULTS; + + $symfonyFlexKernelPath = sprintf('%s/%s', $container->getParameter('paths.base'), self::SYMFONY_4_DEFAULTS['kernel']['path']); + if (file_exists($symfonyFlexKernelPath)) { + $defaults = self::SYMFONY_4_DEFAULTS; + } + + $config = array_replace_recursive($defaults, $userConfig); + + if (null !== $config['env_file']) { + $this->loadEnvVars($container, $config['env_file']); + + if (!isset($userConfig['kernel']['env']) && false !== getenv('APP_ENV')) { + $config['kernel']['env'] = getenv('APP_ENV'); + } + + if (!isset($userConfig['kernel']['debug']) && false !== getenv('APP_DEBUG')) { + $config['kernel']['debug'] = getenv('APP_DEBUG'); + } + } + + return $config; + } + + /** + * @param ContainerBuilder $container + * @param string $fileName + */ + private function loadEnvVars(ContainerBuilder $container, string $fileName): void + { + $envFilePath = sprintf('%s/%s', $container->getParameter('paths.base'), $fileName); + (new Dotenv())->load($envFilePath); + } + /** * @param ContainerBuilder $container */