Skip to content

Commit 2c0cf01

Browse files
committed
Make Symfony directory structure configurable from outside (EasyCorp#65, EasyCorp#66)
1 parent f6c45c2 commit 2c0cf01

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/Configuration/DefaultConfiguration.php

+28-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
*/
2424
final class DefaultConfiguration extends AbstractConfiguration
2525
{
26+
const SYMFONY_2 = 2;
27+
const SYMFONY_3 = 3;
28+
const SYMFONY_4 = 4;
29+
2630
// variables starting with an underscore are for internal use only
2731
private $_symfonyEnvironmentEnvVarName; // SYMFONY_ENV or APP_ENV
32+
private $_symfonyDirectoryStructureVersion;
2833

2934
// properties are defined as private so the developer doesn't see them when using
3035
// their IDE autocompletion. To simplify things, the builder defines setter
@@ -65,7 +70,8 @@ public function __construct(string $localProjectDir)
6570
{
6671
parent::__construct();
6772
$this->localProjectDir = $localProjectDir;
68-
$this->setDefaultConfiguration(Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION);
73+
$this->guessSymfonyDirectoryStructure(Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION);
74+
$this->setDefaultConfiguration();
6975
}
7076

7177
// this proxy method is needed because the autocompletion breaks
@@ -364,24 +370,42 @@ protected function getReservedServerProperties(): array
364370
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];
365371
}
366372

367-
private function setDefaultConfiguration(int $symfonyMajorVersion, $symfonyMinorVersion): void
373+
private function guessSymfonyDirectoryStructure(int $symfonyMajorVersion, $symfonyMinorVersion): void
368374
{
369375
if (2 === $symfonyMajorVersion) {
376+
$this->_symfonyDirectoryStructureVersion = self::SYMFONY_2;
377+
} elseif (3 === $symfonyMajorVersion && 4 < $symfonyMinorVersion) {
378+
$this->_symfonyDirectoryStructureVersion = self::SYMFONY_3;
379+
} elseif (4 === $symfonyMajorVersion || (3 === $symfonyMajorVersion && 4 >= $symfonyMinorVersion)) {
380+
$this->_symfonyDirectoryStructureVersion = self::SYMFONY_4;
381+
}
382+
}
383+
384+
public function setDefaultConfiguration(?int $symfonyDirectoryStructureVersion = null): void
385+
{
386+
if (in_array($symfonyDirectoryStructureVersion, [self::SYMFONY_2, self::SYMFONY_3, self::SYMFONY_4])) {
387+
$this->_symfonyDirectoryStructureVersion = $symfonyDirectoryStructureVersion;
388+
} else {
389+
$symfonyDirectoryStructureVersion = $this->_symfonyDirectoryStructureVersion;
390+
}
391+
392+
// TODO: Be a bit more clever and for example take composer.json extra configuration into account
393+
if (self::SYMFONY_2 === $symfonyDirectoryStructureVersion) {
370394
$this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV';
371395
$this->setDirs('app', 'app/config', 'app/cache', 'app/logs', 'src', 'app/Resources/views', 'web');
372396
$this->controllersToRemove(['web/app_*.php']);
373397
$this->sharedFiles = ['app/config/parameters.yml'];
374398
$this->sharedDirs = ['app/logs'];
375399
$this->writableDirs = ['app/cache/', 'app/logs/'];
376400
$this->dumpAsseticAssets = true;
377-
} elseif (3 === $symfonyMajorVersion && 4 < $symfonyMinorVersion) {
401+
} elseif (self::SYMFONY_3 === $symfonyDirectoryStructureVersion) {
378402
$this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV';
379403
$this->setDirs('bin', 'app/config', 'var/cache', 'var/logs', 'src', 'app/Resources/views', 'web');
380404
$this->controllersToRemove(['web/app_*.php']);
381405
$this->sharedFiles = ['app/config/parameters.yml'];
382406
$this->sharedDirs = ['var/logs'];
383407
$this->writableDirs = ['var/cache/', 'var/logs/'];
384-
} elseif (4 === $symfonyMajorVersion || (3 === $symfonyMajorVersion && 4 >= $symfonyMinorVersion)) {
408+
} elseif (self::SYMFONY_4 === $symfonyDirectoryStructureVersion) {
385409
$this->_symfonyEnvironmentEnvVarName = 'APP_ENV';
386410
$this->setDirs('bin', 'config', 'var/cache', 'var/log', 'src', 'templates', 'public');
387411
$this->controllersToRemove([]);

0 commit comments

Comments
 (0)