From 8346bd83c52144773d2de5090a4b653bcef403b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Wagner?= Date: Thu, 10 Jan 2019 14:59:36 +0100 Subject: [PATCH] Decouple environment variable name from directory structure version (#84) --- src/Configuration/DefaultConfiguration.php | 27 +++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Configuration/DefaultConfiguration.php b/src/Configuration/DefaultConfiguration.php index 44fa960..5f0c110 100644 --- a/src/Configuration/DefaultConfiguration.php +++ b/src/Configuration/DefaultConfiguration.php @@ -71,6 +71,7 @@ public function __construct(string $localProjectDir) parent::__construct(); $this->localProjectDir = $localProjectDir; $this->guessSymfonyDirectoryStructure(Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION); + $this->setEnvironmentVarName(Kernel::MAJOR_VERSION); $this->setDefaultConfiguration(); } @@ -372,7 +373,6 @@ public function setDefaultConfiguration(?int $symfonyDirectoryStructureVersion = } if (self::SYMFONY_2 === $this->_symfonyDirectoryStructureVersion) { - $this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV'; $this->setDirs('app', 'app/config', 'app/cache', 'app/logs', 'src', 'app/Resources/views', 'web'); $this->controllersToRemove(['web/app_*.php']); $this->sharedFiles = ['app/config/parameters.yml']; @@ -380,14 +380,12 @@ public function setDefaultConfiguration(?int $symfonyDirectoryStructureVersion = $this->writableDirs = ['app/cache/', 'app/logs/']; $this->dumpAsseticAssets = true; } elseif (self::SYMFONY_3 === $this->_symfonyDirectoryStructureVersion) { - $this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV'; $this->setDirs('bin', 'app/config', 'var/cache', 'var/logs', 'src', 'app/Resources/views', 'web'); $this->controllersToRemove(['web/app_*.php']); $this->sharedFiles = ['app/config/parameters.yml']; $this->sharedDirs = ['var/logs']; $this->writableDirs = ['var/cache/', 'var/logs/']; } elseif (self::SYMFONY_4 === $this->_symfonyDirectoryStructureVersion) { - $this->_symfonyEnvironmentEnvVarName = 'APP_ENV'; $this->setDirs('bin', 'config', 'var/cache', 'var/log', 'src', 'templates', 'public'); $this->controllersToRemove([]); $this->sharedDirs = ['var/log']; @@ -402,6 +400,15 @@ protected function getReservedServerProperties(): array return [Property::bin_dir, Property::config_dir, Property::console_bin, Property::cache_dir, Property::deploy_dir, Property::log_dir, Property::src_dir, Property::templates_dir, Property::web_dir]; } + /** + * Guess the directory structure of the project based on the framework version. + * Could be manually selected to a different structure on project setup, in that + * case the user should set the correct directory structure version in their + * deployment configuration. + * + * @param int $symfonyMajorVersion + * @param $symfonyMinorVersion + */ private function guessSymfonyDirectoryStructure(int $symfonyMajorVersion, $symfonyMinorVersion): void { // TODO: Be a bit more clever and for example take composer.json extra configuration into account @@ -414,6 +421,20 @@ private function guessSymfonyDirectoryStructure(int $symfonyMajorVersion, $symfo } } + /** + * Set the name of the environment variable for Symfony depending on the framework version + * + * @param int $symfonyMajorVersion + */ + private function setEnvironmentVarName(int $symfonyMajorVersion): void + { + if ($symfonyMajorVersion >= 5) { + $this->_symfonyEnvironmentEnvVarName = 'APP_ENV'; + } else { + $this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV'; + } + } + private function setDirs(string $binDir, string $configDir, string $cacheDir, string $logDir, string $srcDir, string $templatesDir, string $webDir): void { $this->binDir = $binDir;