Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Symfony directory structure configurable from outside (#65, #66) #67

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ matrix:
- CHECK_PHP_SYNTAX="no"
- php: 7.1
env:
- SYMFONY_VERSION="3.2.*"
- SYMFONY_VERSION="3.4.*"
- CHECK_PHP_SYNTAX="yes"
allow_failures:
- php: nightly
Expand Down
69 changes: 57 additions & 12 deletions src/Configuration/DefaultConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
*/
final class DefaultConfiguration extends AbstractConfiguration
{
const SYMFONY_2 = 2;
const SYMFONY_3 = 3;
const SYMFONY_4 = 4;

// variables starting with an underscore are for internal use only
private $_symfonyEnvironmentEnvVarName; // SYMFONY_ENV or APP_ENV
private $_symfonyDirectoryStructureVersion;

// properties are defined as private so the developer doesn't see them when using
// their IDE autocompletion. To simplify things, the builder defines setter
Expand Down Expand Up @@ -65,7 +70,9 @@ public function __construct(string $localProjectDir)
{
parent::__construct();
$this->localProjectDir = $localProjectDir;
$this->setDefaultConfiguration(Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION);
$this->guessSymfonyDirectoryStructure(Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION);
$this->setEnvironmentVarName(Kernel::MAJOR_VERSION);
$this->setDefaultConfiguration();
}

// this proxy method is needed because the autocompletion breaks
Expand Down Expand Up @@ -359,35 +366,73 @@ public function resetOpCacheFor(string $homepageUrl): self
return $this;
}

protected function getReservedServerProperties(): array
public function setDefaultConfiguration(?int $symfonyDirectoryStructureVersion = null): self
{
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];
}
if (in_array($symfonyDirectoryStructureVersion, [self::SYMFONY_2, self::SYMFONY_3, self::SYMFONY_4])) {
$this->_symfonyDirectoryStructureVersion = $symfonyDirectoryStructureVersion;
}

private function setDefaultConfiguration(int $symfonyMajorVersion, $symfonyMinorVersion): void
{
if (2 === $symfonyMajorVersion) {
$this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV';
if (self::SYMFONY_2 === $this->_symfonyDirectoryStructureVersion) {
$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 === $this->_symfonyDirectoryStructureVersion) {
$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 === $this->_symfonyDirectoryStructureVersion) {
$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;
}

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
if (2 === $symfonyMajorVersion) {
$this->_symfonyDirectoryStructureVersion = self::SYMFONY_2;
} elseif (3 === $symfonyMajorVersion && 4 > $symfonyMinorVersion) {
$this->_symfonyDirectoryStructureVersion = self::SYMFONY_3;
} elseif (4 === $symfonyMajorVersion || (3 === $symfonyMajorVersion && 4 <= $symfonyMinorVersion)) {
$this->_symfonyDirectoryStructureVersion = self::SYMFONY_4;
}
}

/**
* 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
Expand Down