bartlomiejbeta/di-env-loader-bundle
Installation:
composer require bartlomiejbeta/di-env-loader-bundle
Add the bundle to your config/bundles.php:
return [
// ...
BartlomiejBeta\DIEnvLoaderBundle\DIEnvLoaderBundle::class => ['all' => true],
];
First Use Case:
Extension (e.g., MyCustomExtension) in a bundle.DIEnvLoaderTrait to load environment-specific configs.services.yml, services_dev.yml) in Resources/config/.Example:
use BartlomiejBeta\DIEnvLoaderBundle\Extension\DIEnvLoaderTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class MyCustomExtension extends \Symfony\Component\HttpKernel\DependencyInjection\Extension\Extension
{
use DIEnvLoaderTrait;
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
self::loadByEnv($loader, $container, 'services', 'yml');
}
}
services.yml (default) and services_<env>.yml (e.g., services_dev.yml for APP_ENV=dev).Default Environment Loading:
Use loadByEnv() without conditions to load configs for all environments (e.g., services.yml, services_test.yml).
self::loadByEnv($loader, $container, 'services', 'yml');
services.yml, services_<env>.yml (e.g., services_dev.yml).Environment-Specific Loading:
Check the current environment (self::getEnv($container)) and load configs conditionally.
if ('dev' === self::getEnv($container)) {
self::loadByEnv($loader, $container, 'services', 'yml', '/dev');
}
services_dev.yml (if placed in Resources/config/dev/).Custom File Locators:
Override the default FileLocator path for granular control:
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/custom_path'));
Combining with Other Loaders:
Chain loadByEnv() with manual loader calls for hybrid configs:
$loader->load('base_config.yml'); // Always loaded
self::loadByEnv($loader, $container, 'env_specific', 'yml');
Resources/config/ and reference them in extensions.# services_dev.yml
parameters:
app.debug: true
self::getEnv() in tests to simulate environments:
$container->set('kernel.environment', 'test');
File Naming Conflicts:
services.yml and services_test.yml in the same dir). The bundle prioritizes <name>_<env>.yml over <name>.yml.Resources/config/dev/services.yml).Environment Detection:
self::getEnv($container) returns the kernel environment (APP_ENV), not the runtime environment (e.g., PHP_SAPI). For runtime-specific logic, use getenv() or $container->getParameter('kernel.environment').Loader Initialization:
FileLocator path is correct. A 404 error suggests the path is misconfigured.var_dump($locator->locate('services.yml')) to verify file resolution.Circular Dependencies:
ignore_errors: true in YAML or lazy-loading services:
services:
MyService:
class: App\Service
calls:
- [setConfig, ['%env_config%']]
lazy: true
APP_DEBUG=1 to see detailed loader errors in Symfony’s profiler or logs.php bin/console debug:container | grep "env_specific"
file_put_contents(
__DIR__ . '/../var/log/debug.env',
'Current env: ' . self::getEnv($container) . PHP_EOL
);
Custom Trait Methods:
Extend DIEnvLoaderTrait to add logic (e.g., validate environments):
protected static function isValidEnv(string $env): bool
{
return in_array($env, ['dev', 'prod']);
}
Then modify loadByEnv() to skip invalid environments.
Loader Decorators:
Decorate the YamlFileLoader to preprocess configs:
$loader = new class($container, $locator) extends YamlFileLoader {
public function load($resource, string $type = null)
{
$content = file_get_contents($resource);
$content = str_replace('%placeholder%', 'value', $content);
file_put_contents($resource, $content);
parent::load($resource, $type);
}
};
Parameter-Based Paths: Use container parameters to externalize config paths:
$configPath = $container->getParameter('my_bundle.config_path');
$loader = new YamlFileLoader($container, new FileLocator($configPath));
<name>_<env>.yml exists, the bundle falls back to <name>.yml. To disable this, override loadByEnv().dev ≠ Dev). Use strtolower() if needed:
$env = strtolower(self::getEnv($container));
How can I help you explore Laravel packages today?