Installation:
composer require arthem/config-file-bundle
Add the bundle to config/bundles.php:
return [
// ...
Arthem\ConfigFileBundle\ArthemConfigFileBundle::class => ['all' => true],
];
First Use Case:
Define a file in config/packages/arthem_config_file.yaml:
arthem_config_file:
files:
jwt_key:
extension: pem
content: '%env(JWT_PRIVATE_KEY)%'
Reference it in your service configuration:
# config/packages/security.yaml
jwt_provider:
private_key_file: '%arthem_config_file.file.jwt_key%'
Verify:
Clear cache (php bin/console cache:clear) and check the generated file in var/cache/{env}/arthem_config_file/.
Docker/Env-First Deployments:
arthem_config_file to dynamically generate files from these vars during runtime.private_key_file: /path/to/key.pem with %arthem_config_file.file.jwt_key%.Template-Based Configs:
some_service:
config_file: |
&file:config.json
%env(json_string:CONFIG_DATA)%
Multi-Environment Setups:
arthem_config_file per environment (e.g., config/packages/dev/arthem_config_file.yaml).%kernel.environment% in file paths if needed (though the bundle handles hashing).config/packages/parameters.yaml:
parameters:
app.jwt_key_path: '%arthem_config_file.file.jwt_key%'
// src/Service/JwtService.php
public function __construct(string $privateKeyPath) {
$this->privateKeyPath = $privateKeyPath;
}
# config/services.yaml
services:
App\Service\JwtService:
arguments:
$privateKeyPath: '%arthem_config_file.file.jwt_key%'
Cache Dependency:
php bin/console cache:clear) after changing env vars or configs.var/cache/{env}/arthem_config_file/ for generated files. Use php bin/console debug:config arthem_config_file to verify config.Environment Variable Resolution:
.env.local or Docker environment:).ParameterNotFoundException for %env(FOO)% → Verify .env files are loaded or use php bin/console debug:container --parameters to check resolved vars.File Naming Collisions:
jwt_key-c131d7b5.pem). Avoid hardcoding paths in configs.%arthem_config_file.file.jwt_key% (resolved path) instead of raw paths.Multiline Env Vars:
json_string processor for JSON configs to avoid escaping issues:
content: '%env(json_string:MULTILINE_JSON)%'
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class LogConfigFilesPass implements CompilerPassInterface {
public function process(ContainerBuilder $container) {
$files = $container->getParameter('arthem_config_file.files');
foreach ($files as $name => $file) {
$container->getDefinition('logger')->addMethodCall(
'info',
['Generated file: ' . $file['path']]
);
}
}
}
Register it in config/services.yaml:
services:
App\DependencyInjection\LogConfigFilesPass:
tags: [compiler]
config/packages/arthem_config_file.yaml:
arthem_config_file:
cache_dir: '%kernel.project_dir%/var/custom_config_files'
FileGenerator service to modify content before writing:
// src/DependencyInjection/ConfigFileExtension.php
public function load(array $configs, ContainerConfigurator $container) {
$container->services()
->set('arthem_config_file.file_generator')
->call('setCustomProcessor', [fn($content) => str_replace('OLD', 'NEW', $content)]);
}
class DynamicConfigPass implements CompilerPassInterface {
public function process(ContainerBuilder $container) {
if ($container->hasParameter('app.generate_dynamic_files')) {
$container->getExtension('arthem_config_file')->addFile(
'dynamic_file',
['extension' => 'json', 'content' => '%env(DYNAMIC_CONTENT)%']
);
}
}
}
How can I help you explore Laravel packages today?