Last active
September 12, 2018 09:25
-
-
Save dbu/8b210b4b93bafd4f996e0d67f8c812ee to your computer and use it in GitHub Desktop.
Load environment specific configuration in symfony 4 kernel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | |
use Symfony\Component\Config\Loader\LoaderInterface; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\HttpKernel\Kernel as BaseKernel; | |
use Symfony\Component\Routing\RouteCollectionBuilder; | |
class Kernel extends BaseKernel | |
{ | |
use MicroKernelTrait; | |
public const APP_LOCAL_ENVS = [ | |
'test', | |
'dev', | |
'dev_security', | |
]; | |
public const APP_CF_ENVS = [ | |
'prod', | |
'cf_test', | |
'cf_dev', | |
]; | |
private const CONFIG_EXTS = '.{php,xml,yaml,yml}'; | |
private const VAGRANT_USER = 'vagrant'; | |
/** | |
* Use cache directory outside of project dir when in vagrant to not have cache go over NFS. | |
*/ | |
public function getCacheDir() | |
{ | |
if (self::VAGRANT_USER === getenv('USER')) { | |
return '/dev/shm/cache/'.$this->environment; | |
} | |
return $this->getProjectDir().'/var/cache/'.$this->environment; | |
} | |
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader) | |
{ | |
... | |
// [standard] load regular configuration | |
$loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob'); | |
// [custom] load configuration overwrite for local environments | |
if (\in_array($this->environment, self::APP_LOCAL_ENVS, true)) { | |
$loader->load($confDir.'/{packages}/local/*'.self::CONFIG_EXTS, 'glob'); | |
} | |
// [custom] load configuration overwrite for cloud environments | |
if (\in_array($this->environment, self::APP_CF_ENVS, true)) { | |
$loader->load($confDir.'/{packages}/cf/*'.self::CONFIG_EXTS, 'glob'); | |
} | |
// [standard] load environment specific configuration | |
$loader->load($confDir.'/{packages}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob'); | |
// [custom] load regular parameters & environment group & environment specific parameters | |
$loader->load($confDir.'/{parameters}/*'.self::CONFIG_EXTS, 'glob'); | |
if (\in_array($this->environment, self::APP_LOCAL_ENVS, true)) { | |
$loader->load($confDir.'/{parameters}/local/*'.self::CONFIG_EXTS, 'glob'); | |
} | |
if (\in_array($this->environment, self::APP_CF_ENVS, true)) { | |
$loader->load($confDir.'/{parameters}/cf/*'.self::CONFIG_EXTS, 'glob'); | |
} | |
$loader->load($confDir.'/{parameters}/'.$this->environment.'/*'.self::CONFIG_EXTS, 'glob'); | |
// [standard] load services & environment specific services | |
$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob'); | |
$loader->load($confDir.'/{services}/*'.self::CONFIG_EXTS, 'glob'); | |
$loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob'); | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# illustration of the config/ directory structure | |
├── bundles.php | |
├── packages | |
│ ├── cf | |
│ │ └── security.yaml | |
│ ├── dev | |
│ │ ├── routing.yaml | |
│ │ ├── swiftmailer.yaml | |
│ │ └── web_profiler.yaml | |
│ ├── dev_security | |
│ │ └── dev_security.yaml | |
│ ├── doctrine.yaml | |
│ ├── framework.yaml | |
│ ├── jms_serializer.yaml | |
│ ├── rokka.yaml | |
│ ├── routing.yaml | |
│ ├── sensio_framework_extra.yaml | |
│ ├── swiftmailer.yaml | |
│ ├── test | |
│ │ ├── framework.yaml | |
│ │ ├── swiftmailer.yaml | |
│ │ └── web_profiler.yaml | |
│ ├── translation.yaml | |
│ ├── twig.yaml | |
│ └── workers.yml | |
├── parameters | |
│ ├── cf | |
│ │ └── cf.yaml | |
│ ├── cf_dev | |
│ │ ├── cf_dev.yaml | |
│ │ └── database.yaml | |
│ ├── cf_test | |
│ │ ├── cf_test.yaml | |
│ │ └── database.yaml | |
│ ├── classifications.yml | |
│ ├── default.yaml | |
│ ├── local | |
│ │ ├── database.yaml | |
│ │ └── local.yaml | |
│ ├── prod | |
│ │ ├── database.yaml | |
│ │ └── prod.yaml | |
│ ├── test | |
│ │ └── test.yaml | |
│ └── xcampaign_newsletter.yml | |
├── routes | |
│ ├── api_debug.yaml | |
│ ├── api.yaml | |
│ ├── dev | |
│ │ ├── twig.yaml | |
│ │ └── web_profiler.yaml | |
│ ├── dev_security | |
│ │ └── dev_security.yaml | |
│ └── user_content.yaml | |
├── routes.yaml | |
├── services | |
│ ├── api.product.controller.yaml | |
│ ├── api.product.factory.xml | |
│ ├── api.product.yaml | |
│ └── user_content.yaml | |
└── services_test.yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment