incenteev/composer-parameter-handler
Install the Package
Add to composer.json:
"require": {
"incenteev/composer-parameter-handler": "^2.0"
}
Run composer require incenteev/composer-parameter-handler.
Configure composer.json
Define the extra key and hook into Composer scripts:
"extra": {
"incenteev-parameters": {
"file": "config/parameters.yml" // Path to your target file
}
},
"scripts": {
"post-install-cmd": ["Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"],
"post-update-cmd": ["Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"]
}
Create a Dist File
Place a .dist template (e.g., config/parameters.yml.dist) with your default parameters under a top-level parameters key:
parameters:
app.debug: true
database.host: localhost
First Run
Execute composer install or composer update. The package generates parameters.yml from the .dist file, ignoring any non-parameters keys.
Use this to manage environment variables (e.g., .env-like secrets) without committing sensitive data to version control. Example workflow:
parameters.yml.dist with placeholders (e.g., database.password: "%env(DB_PASSWORD)%")..gitignore to exclude parameters.yml.composer install to auto-generate parameters.yml from the template.Template-Based Generation
parameters.yml.dist in version control. It defines the structure and defaults..dist file with the existing parameters.yml (if it exists), preserving non-parameters keys (e.g., comments or metadata).Environment-Specific Handling
%composer.environment%) in .dist files to switch configs per environment.
parameters:
app.env: "%composer.environment%"
post-install-cmd):
"scripts": {
"post-install-cmd": [
"@vendor/bin/your-custom-script",
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"
]
}
Symfony-Like Parameter Handling
parameters.yml with Laravel’s config() helper or a custom config loader:
$params = yaml_parse(file_get_contents(config_path('parameters.yml'))['parameters'] ?? []);
config(['app' => array_merge(config('app'), $params)]);
Multi-File Support
.dist files (e.g., database.yml.dist, mail.yml.dist) and merge them using a custom script before running the handler.Dynamic Parameter Injection
composer-plugin-api to inject parameters from external sources (e.g., API calls) before generation."scripts": {
"post-install-cmd": [
"vendor/bin/your-plugin inject-params",
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"
]
}
Parameter Validation
symfony/yaml or webmozart/assert to validate the generated parameters.yml against a schema before deployment.CI/CD Pipelines
parameters.yml.dist with placeholders and replace them with secrets via environment variables:
# .github/workflows/deploy.yml
- name: Generate parameters
run: |
sed -i "s|%env(DB_PASSWORD)%|${{ secrets.DB_PASSWORD }}|g" config/parameters.yml.dist
composer install
File Permissions
parameters.yml if permissions are restrictive.chmod -R 775 config/).Key Collisions
parameters.yml exists with a parameters key, the package merges it with the .dist file. Unexpected overrides can occur.parameters.yml.dist as the single source of truth and avoid manual edits to parameters.yml.Non-YAML Files
Script Order Dependency
parameters.yml can lead to race conditions.Incenteev\\ParameterHandler\\ScriptHandler::buildParameters last in post-install-cmd/post-update-cmd.Case Sensitivity
.dist files (e.g., app.Debug vs. app.debug) won’t merge..dist files (e.g., snake_case).Dry Runs
composer install --dry-run
parameters.yml for errors.Verbose Output
composer install -v
Backup Existing Files
composer update, back up parameters.yml to avoid accidental data loss:
cp config/parameters.yml config/parameters.yml.bak
Custom Parameter Sources
buildParameters: Extend the handler by subclassing ScriptHandler and overriding getParameters() to fetch data from APIs, databases, or other sources.
class CustomHandler extends \Incenteev\ParameterHandler\ScriptHandler {
protected function getParameters() {
$params = parent::getParameters();
$params['app.custom'] = $this->fetchFromExternalService();
return $params;
}
}
composer.json:
"scripts": {
"post-install-cmd": ["CustomHandler::buildParameters"]
}
Pre/Post-Processing
pre-scripts to transform .dist files before the handler runs:
"scripts": {
"pre-install-cmd": ["vendor/bin/your-script transform-params"],
"post-install-cmd": ["Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"]
}
Multi-Tenant Parameters
parameters.yml files by dynamically setting the file path in extra based on environment variables:
"extra": {
"incenteev-parameters": {
"file": "config/parameters_%tenant_id%.yml"
}
}
%tenant_id% before running Composer.Default Key Name
parameters key. To use a custom key (e.g., config), override the getParametersKey() method in a custom handler.Non-Standard Dist File Locations
.dist file must be in the same directory as the target file (e.g., parameters.yml.dist next to parameters.yml). Relative paths in extra are resolved from the project root.Windows Line Endings
.dist files (e.g., using dos2unix or Git’s autocrlf).Composer Version Compatibility
How can I help you explore Laravel packages today?