respect/config
Respect\Config is a lightweight PHP configuration package with a clean API for loading, merging, and accessing settings from arrays and files. It supports dot-notation, validation, and framework integration, making application configuration simple and predictable.
Installation Add the package via Composer:
composer require respect/config
Register the service provider in config/app.php under providers:
Respect\Config::class,
Basic Configuration
Define a config file (e.g., config/app.php):
return [
'debug' => env('APP_DEBUG', false),
'database' => [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
],
],
],
];
First Usage
Access config values via the Config facade:
use Respect\Config;
$debug = Config::get('debug'); // false
$host = Config::get('database.connections.mysql.host'); // 'localhost'
Use Config::set() to dynamically override values (e.g., in a service provider):
Config::set('debug', true); // Override for local/dev
Leverage Config for lightweight DI in constructors:
class UserService {
public function __construct(
private string $dbHost,
private bool $debugMode
) {
$this->dbHost = Config::get('database.connections.mysql.host');
$this->debugMode = Config::get('debug');
}
}
Access deeply nested configs fluently:
$timeout = Config::get('http.client.timeouts.connect'); // 5
Merge configs at runtime (e.g., for feature flags):
Config::merge([
'features' => [
'new_ui' => env('FEATURE_NEW_UI', false),
],
]);
Validate configs during bootstrapping (e.g., in AppServiceProvider):
$config = Config::all();
if (!isset($config['database']['connections'])) {
throw new \RuntimeException('Database config missing!');
}
Load environment-specific configs (e.g., config/local.php):
if (app()->environment('local')) {
Config::merge(require __DIR__.'/local.php');
}
No Caching by Default
Config values are not cached between requests. For performance, cache Config::all() in a singleton or Laravel’s cache:
$cachedConfig = Cache::remember('config', 60, fn() => Config::all());
Overwriting vs. Merging
Config::set() overwrites existing keys. Use Config::merge() for partial updates:
Config::set('debug', true); // Overwrites entirely
Config::merge(['debug' => true]); // Preserves other keys
Dot Notation Ambiguity
Avoid keys with dots (e.g., api.v1) if using Laravel’s dot notation helpers (e.g., config('api.v1')). Stick to underscores or validate paths:
Config::get('api_v1.url'); // Safer
Thread Safety
Config is not thread-safe. Avoid modifying configs in queues or parallel processes without synchronization.
Dump Entire Config
Use Config::all() to inspect the full config tree during development:
dd(Config::all());
Validate Paths Check for typos in config paths with:
if (!Config::has('database.connections.mysql')) {
throw new \InvalidArgumentException('Invalid config path');
}
Environment Debugging Log environment-specific configs:
\Log::debug('Current config:', ['env' => app()->environment(), 'config' => Config::all()]);
Custom Config Loaders
Extend Respect\Config by adding a loader for remote configs (e.g., Redis, API):
Config::extend('remote', function () {
return json_decode(file_get_contents('https://api.example.com/config'), true);
});
Config::merge(Config::get('remote'));
Config Events Trigger events on config changes (e.g., for logging or analytics):
Config::onChange(function ($key, $oldValue, $newValue) {
\Log::info("Config changed: {$key} from {$oldValue} to {$newValue}");
});
Integration with Laravel’s Config
Bridge respect/config with Laravel’s config() helper by aliasing:
if (!function_exists('config')) {
function config($key = null, $default = null) {
return Respect\Config::get($key, $default);
}
}
How can I help you explore Laravel packages today?