zerkalica/millwright-configuration-bundle
Installation
Add the bundle to your composer.json:
composer require zerkalica/millwright-configuration-bundle
Register the bundle in config/app.php under providers:
Zerkalica\MillwrightConfigurationBundle\MillwrightConfigurationBundle::class,
Configuration File
Create a config file (e.g., config/millwright.php) with your service container settings:
return [
'services' => [
'database' => [
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
],
'cache' => [
'driver' => env('CACHE_DRIVER'),
'prefix' => 'laravel_',
],
],
];
First Use Case Access configurations in a service provider or controller:
$config = $this->container->get('millwright.configuration');
$dbHost = $config['services']['database']['host'];
Centralized Configuration Use the bundle to consolidate all service-specific configurations (e.g., database, cache, queues) into a single, structured file. Avoid hardcoding values in multiple places.
Environment Overrides
Combine with Laravel’s .env to dynamically override configurations:
// config/millwright.php
return [
'services' => [
'database' => [
'host' => env('DB_HOST', 'localhost'), // Default fallback
],
],
];
Dependency Injection Bind configurations to interfaces or abstract classes for better testability:
$this->app->bind('config.services.database', function ($app) {
return $app['millwright.configuration']['services']['database'];
});
Configuration Validation Validate configurations during bootstrapping (e.g., in a service provider):
$config = $this->container->get('millwright.configuration');
if (!in_array($config['services']['cache']['driver'], ['file', 'redis'])) {
throw new \RuntimeException('Invalid cache driver');
}
Dynamic Configuration Loading Load configurations from external sources (e.g., API, database) and merge them with the bundle’s config:
$externalConfig = Cache::get('dynamic_config');
$mergedConfig = array_merge(
$this->container->get('millwright.configuration'),
$externalConfig
);
Configuration Caching Cache the resolved configurations to avoid repeated file reads:
$config = Cache::remember('millwright_config', 60, function () {
return $this->container->get('millwright.configuration');
});
Outdated Package The last release is from 2015, so expect potential compatibility issues with modern Laravel versions (8+). Test thoroughly or fork the package for updates.
No Built-in Validation
The bundle does not validate configuration structure or values by default. Implement custom validation logic (e.g., using Laravel’s Validator or array_key_exists).
Namespace Conflicts
The bundle uses Zerkalica\MillwrightConfigurationBundle, which may conflict with other packages. Prefix your service bindings to avoid collisions:
$this->app->bind('millwright.database.config', ...);
Configuration Not Loading?
Ensure the bundle is registered in config/app.php after Laravel’s core providers. Check for typos in the config file path (config/millwright.php).
Circular Dependencies If configurations depend on other services (e.g., cache), resolve them in the correct order:
// Resolve cache config first
$cacheConfig = $this->container->get('millwright.configuration')['services']['cache'];
Cache::set($cacheConfig['prefix'] . 'key', 'value');
Custom Configuration Sources Extend the bundle by creating a custom loader (e.g., for YAML or JSON files):
$this->app->extend('millwright.configuration', function ($config) {
$customConfig = json_decode(file_get_contents('config/custom.json'), true);
return array_merge($config, $customConfig);
});
Configuration Events Dispatch events when configurations are loaded or modified:
event(new ConfigLoaded($this->container->get('millwright.configuration')));
Testing Mock configurations in tests:
$this->app->instance('millwright.configuration', [
'services' => ['database' => ['host' => 'test_host']],
]);
How can I help you explore Laravel packages today?