axdlee/laravel-config-writer
Write safely to Laravel config PHP files (Laravel 5.3+), updating nested keys while preserving formatting, comments, and advanced settings. Supports strings, ints, booleans, and single-level arrays. Includes Laravel service provider and standalone Rewrite class.
composer require axdlee/laravel-config-writer
config/app.php under 'providers':
Axdlee\Config\ConfigServiceProvider::class,
use Illuminate\Support\Facades\Config;
Config::write('app.url', 'https://example.com');
This updates config/app.php while preserving comments and structure.Dynamic Configuration Updates:
Use Config::write() to modify config values at runtime (e.g., feature flags, environment-specific settings):
// Update a nested key
Config::write('services.stripe.key', env('STRIPE_KEY'));
// Update an array
Config::write('mail.from.address', ['address' => 'contact@example.com']);
Environment-Specific Configs:
Override configs per environment (e.g., config/local.php):
if (app()->environment('local')) {
Config::write('app.debug', true);
}
Non-Laravel Usage:
Use the standalone Rewrite class for CLI scripts or non-Laravel PHP:
$rewriter = new \Axdlee\Config\Rewrite();
$rewriter->toFile('config/custom.php', [
'custom.setting' => 'value',
'nested.key' => ['a', 'b', 'c'],
]);
$value = request()->input('app.timezone');
if (in_array($value, ['UTC', 'America/New_York'])) {
Config::write('app.timezone', $value);
}
file_put_contents('config/app.php.bak', file_get_contents('config/app.php'));
Config::write('app.url', 'https://new-domain.com');
publishes to manage config files in version control:
// config/app.php
'providers' => [
// ...,
Axdlee\Config\ConfigServiceProvider::class,
],
File Integrity Risks:
file_put_contents to verify the file before writing:
$tempPath = tempnam(sys_get_temp_dir(), 'config_');
file_put_contents($tempPath, $rewriter->rewrite(file_get_contents('config/app.php'), ['app.url' => 'test']));
// Validate $tempPath before overwriting.
Nested Arrays:
// Supported
Config::write('services.stripe.options.timeout', 30);
// Unsupported (may fail)
Config::write('services.stripe.options', ['timeout' => 30, 'retries' => 3]);
Comments and Whitespace:
php-cs-fixer or manual cleanup after writing to enforce consistency.Caching:
Config::write() may not reflect immediately.Config::write('app.debug', true);
Artisan::call('config:clear');
\Log::info('Config updated', ['key' => 'app.url', 'value' => 'https://example.com']);
Config::write('app.url', 'https://example.com');
$original = file_get_contents('config/app.php');
$rewriter = new \Axdlee\Config\Rewrite();
$modified = $rewriter->rewrite($original, ['app.url' => 'test']);
echo diff($original, $modified); // Use a diff tool or library
Rewrite class to support additional data types (e.g., objects, dates):
class CustomRewriter extends \Axdlee\Config\Rewrite {
public function rewrite($content, array $data) {
// Add support for dates
foreach ($data as $key => $value) {
if ($value instanceof \DateTime) {
$data[$key] = $value->format('Y-m-d');
}
}
return parent::rewrite($content, $data);
}
}
// In a service provider
event(new \Axdlee\Config\WritingStarted('app.url'));
Config::write('app.url', 'https://example.com');
event(new \Axdlee\Config\WritingCompleted('app.url'));
$localWriter = new \Axdlee\Config\Rewrite();
$localWriter->setEnvironment('local');
$localWriter->toFile('config/local.php', ['app.debug' => true]);
How can I help you explore Laravel packages today?