Add autoconfiguration for Symfony 4, allow overriding .env setting by using explicit env and debug settings, tweaks
This commit is contained in:
@@ -61,7 +61,35 @@ final class SymfonyExtension implements Extension
|
||||
/**
|
||||
* 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
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user