Add autoconfiguration for Symfony 4, allow overriding .env setting by using explicit env and debug settings, tweaks
This commit is contained in:
@@ -3,7 +3,7 @@ Feature: Not crashing Behat
|
|||||||
As a Behat User
|
As a Behat User
|
||||||
I want to have Behat up and running after enabling this extension
|
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:
|
Given a Behat configuration containing:
|
||||||
"""
|
"""
|
||||||
default:
|
default:
|
||||||
@@ -29,7 +29,7 @@ Feature: Not crashing Behat
|
|||||||
When I run Behat
|
When I run Behat
|
||||||
Then it should pass
|
Then it should pass
|
||||||
|
|
||||||
Scenario: Not crashing Behat with CrossContainerExtension
|
Scenario: Successful boot the Symfony kernel with explicit configuration
|
||||||
Given a Behat configuration containing:
|
Given a Behat configuration containing:
|
||||||
"""
|
"""
|
||||||
default:
|
default:
|
||||||
@@ -37,17 +37,19 @@ Feature: Not crashing Behat
|
|||||||
FriendsOfBehat\SymfonyExtension:
|
FriendsOfBehat\SymfonyExtension:
|
||||||
kernel:
|
kernel:
|
||||||
bootstrap: ~
|
bootstrap: ~
|
||||||
|
path: app/MyKernel.php
|
||||||
FriendsOfBehat\CrossContainerExtension: ~
|
class: MyKernel
|
||||||
|
env: test
|
||||||
|
debug: true
|
||||||
"""
|
"""
|
||||||
And a file "app/AppKernel.php" containing:
|
And a file "app/MyKernel.php" containing:
|
||||||
"""
|
"""
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Kernel;
|
use Symfony\Component\HttpKernel\Kernel;
|
||||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
|
||||||
class AppKernel extends Kernel
|
class MyKernel extends Kernel
|
||||||
{
|
{
|
||||||
public function registerBundles() { return []; }
|
public function registerBundles() { return []; }
|
||||||
public function registerContainerConfiguration(LoaderInterface $loader) {}
|
public function registerContainerConfiguration(LoaderInterface $loader) {}
|
||||||
@@ -57,7 +59,38 @@ Feature: Not crashing Behat
|
|||||||
When I run Behat
|
When I run Behat
|
||||||
Then it should pass
|
Then it should pass
|
||||||
|
|
||||||
Scenario: This extension boot a Symfony4 kernel
|
|
||||||
|
Scenario: Successful boot the Symfony 4 kernel with autoconfiguration
|
||||||
|
Given a Behat configuration containing:
|
||||||
|
"""
|
||||||
|
default:
|
||||||
|
extensions:
|
||||||
|
FriendsOfBehat\SymfonyExtension: ~
|
||||||
|
"""
|
||||||
|
And a file ".env" containing:
|
||||||
|
"""
|
||||||
|
APP_ENV=dev
|
||||||
|
"""
|
||||||
|
And a file "src/Kernel.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
|
||||||
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
|
||||||
|
class Kernel extends BaseKernel
|
||||||
|
{
|
||||||
|
public function registerBundles() { return []; }
|
||||||
|
public function registerContainerConfiguration(LoaderInterface $loader) {}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
And a feature file with passing scenario
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
|
|
||||||
|
Scenario: Successful boot the Symfony 4 kernel with explicit configuration
|
||||||
Given a Behat configuration containing:
|
Given a Behat configuration containing:
|
||||||
"""
|
"""
|
||||||
default:
|
default:
|
||||||
@@ -98,9 +131,11 @@ Feature: Not crashing Behat
|
|||||||
FriendsOfBehat\SymfonyExtension:
|
FriendsOfBehat\SymfonyExtension:
|
||||||
env_file: .env_in_memory
|
env_file: .env_in_memory
|
||||||
kernel:
|
kernel:
|
||||||
|
bootstrap: ~
|
||||||
path: src/MyKernel.php
|
path: src/MyKernel.php
|
||||||
class: MyKernel
|
class: MyKernel
|
||||||
bootstrap: ~
|
env: dev
|
||||||
|
debug: true
|
||||||
"""
|
"""
|
||||||
And a file ".env_in_memory.dist" containing:
|
And a file ".env_in_memory.dist" containing:
|
||||||
"""
|
"""
|
||||||
@@ -122,3 +157,31 @@ Feature: Not crashing Behat
|
|||||||
And a feature file with passing scenario
|
And a feature file with passing scenario
|
||||||
When I run Behat
|
When I run Behat
|
||||||
Then it should pass
|
Then it should pass
|
||||||
|
|
||||||
|
Scenario: Not crashing Behat with CrossContainerExtension
|
||||||
|
Given a Behat configuration containing:
|
||||||
|
"""
|
||||||
|
default:
|
||||||
|
extensions:
|
||||||
|
FriendsOfBehat\SymfonyExtension:
|
||||||
|
kernel:
|
||||||
|
bootstrap: ~
|
||||||
|
|
||||||
|
FriendsOfBehat\CrossContainerExtension: ~
|
||||||
|
"""
|
||||||
|
And a file "app/AppKernel.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Symfony\Component\HttpKernel\Kernel;
|
||||||
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
|
||||||
|
class AppKernel extends Kernel
|
||||||
|
{
|
||||||
|
public function registerBundles() { return []; }
|
||||||
|
public function registerContainerConfiguration(LoaderInterface $loader) {}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
And a feature file with passing scenario
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
|
|||||||
@@ -61,7 +61,35 @@ final class SymfonyExtension implements Extension
|
|||||||
/**
|
/**
|
||||||
* Enable or disable the debug mode
|
* Enable or disable the debug mode
|
||||||
*/
|
*/
|
||||||
private const DEBUG_MODE = false;
|
private const DEFAULT_DEBUG_MODE = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Symfony configuration
|
||||||
|
*/
|
||||||
|
private const SYMFONY_DEFAULTS = [
|
||||||
|
'env_file' => 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
|
* @var CrossContainerProcessor|null
|
||||||
@@ -91,18 +119,15 @@ final class SymfonyExtension implements Extension
|
|||||||
public function configure(ArrayNodeDefinition $builder): void
|
public function configure(ArrayNodeDefinition $builder): void
|
||||||
{
|
{
|
||||||
$builder
|
$builder
|
||||||
->addDefaultsIfNotSet()
|
|
||||||
->children()
|
->children()
|
||||||
->scalarNode('env_file')->defaultNull()->end()
|
->scalarNode('env_file')->end()
|
||||||
->arrayNode('kernel')
|
->arrayNode('kernel')
|
||||||
->addDefaultsIfNotSet()
|
|
||||||
->children()
|
->children()
|
||||||
->scalarNode('bootstrap')->defaultValue('app/autoload.php')->end()
|
->scalarNode('bootstrap')->end()
|
||||||
->scalarNode('path')->defaultValue('app/AppKernel.php')->end()
|
->scalarNode('path')->end()
|
||||||
->scalarNode('class')->defaultValue('AppKernel')->end()
|
->scalarNode('class')->end()
|
||||||
->scalarNode('env')->defaultValue('test')->end()
|
->scalarNode('env')->end()
|
||||||
->booleanNode('debug')->defaultTrue()->end()
|
->booleanNode('debug')->end()
|
||||||
->end()
|
|
||||||
->end()
|
->end()
|
||||||
->end()
|
->end()
|
||||||
->end()
|
->end()
|
||||||
@@ -114,17 +139,7 @@ final class SymfonyExtension implements Extension
|
|||||||
*/
|
*/
|
||||||
public function load(ContainerBuilder $container, array $config): void
|
public function load(ContainerBuilder $container, array $config): void
|
||||||
{
|
{
|
||||||
if (null !== $config['env_file']) {
|
$config = $this->autoconfigure($container, $config);
|
||||||
$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;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->loadKernel($container, $config['kernel']);
|
$this->loadKernel($container, $config['kernel']);
|
||||||
$this->loadKernelContainer($container);
|
$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
|
* @param ContainerBuilder $container
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user