From e2814d2a844ba7b054499199a2adac0474a1132e Mon Sep 17 00:00:00 2001 From: fd6130 Date: Fri, 12 Nov 2021 21:54:06 +0800 Subject: [PATCH] fix symfony 3.4 default folder config --- src/Configuration/DefaultConfiguration.php | 41 +++++++++++++++++----- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Configuration/DefaultConfiguration.php b/src/Configuration/DefaultConfiguration.php index 5ea53b0..eeea44b 100644 --- a/src/Configuration/DefaultConfiguration.php +++ b/src/Configuration/DefaultConfiguration.php @@ -23,6 +23,11 @@ */ final class DefaultConfiguration extends AbstractConfiguration { + const SYMFONY_2 = 2; + const SYMFONY_3 = 3; + const SYMFONY_4 = 4; + const SYMFONY_5 = 5; + // variables starting with an underscore are for internal use only private $_symfonyEnvironmentEnvVarName; // SYMFONY_ENV or APP_ENV @@ -65,7 +70,7 @@ public function __construct(string $localProjectDir) { parent::__construct(); $this->localProjectDir = $localProjectDir; - $this->setDefaultConfiguration(Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION); + $this->setDefaultConfiguration(Kernel::MAJOR_VERSION); } // this proxy method is needed because the autocompletion breaks @@ -368,30 +373,50 @@ 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]; } - private function setDefaultConfiguration(int $symfonyMajorVersion, $symfonyMinorVersion): void + private function setDefaultConfiguration(int $symfonyMajorVersion): self { - if (2 === $symfonyMajorVersion) { - $this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV'; + if(self::SYMFONY_2 > $symfonyMajorVersion || self::SYMFONY_5 < $symfonyMajorVersion) + { + throw new \InvalidArgumentException("%d is not a supported Symfony version.", $symfonyMajorVersion); + } + + $this->setEnvironmentVarName($symfonyMajorVersion); + + if (self::SYMFONY_2 === $symfonyMajorVersion) { $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']; $this->sharedDirs = ['app/logs']; $this->writableDirs = ['app/cache/', 'app/logs/']; $this->dumpAsseticAssets = true; - } elseif (3 === $symfonyMajorVersion && 4 < $symfonyMinorVersion) { - $this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV'; + } elseif (self::SYMFONY_3 === $symfonyMajorVersion) { $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 (4 <= $symfonyMajorVersion || (3 === $symfonyMajorVersion && 4 >= $symfonyMinorVersion)) { - $this->_symfonyEnvironmentEnvVarName = 'APP_ENV'; + } elseif (self::SYMFONY_4 <= $symfonyMajorVersion) { // support Symfony 4 or above $this->setDirs('bin', 'config', 'var/cache', 'var/log', 'src', 'templates', 'public'); $this->controllersToRemove([]); $this->sharedDirs = ['var/log']; $this->writableDirs = ['var/cache/', 'var/log/']; } + + return $this; + } + + /** + * 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 > 3) { + $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