ibrostudio / laravel-neon-config
Add Neon config values to Laravel config'
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0
- nette/neon: ^3.4
- spatie/laravel-data: ^4.9
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
Neon config management for Laravel packages.
The goal of this package is to provide a config file available outside the context of a Laravel app.
For example : a package used as a dev dependency of another package.
This package allows you to use a .neon file to overwrite a Laravel config file.
Installation
composer require ibrostudio/laravel-neon-config
Add the trait UseNeonConfig
to your package service provider and define your "neon to config" mapping:
use IBroStudio\NeonConfig\Concerns\UseNeonConfig; class PackageServiceProvider extends ServiceProvider { use UseNeonConfig; public function register() { $this->handleNeon('package-neon-file')->forConfig('package-config-key'); } ...
If your package uses PackageServiceProvider from Spatie's package-skeleton-laravel and you are only dealing with a single config file, you can omit the name parameters:
use IBroStudio\NeonConfig\Concerns\UseNeonConfig; class PackageServiceProvider extends PackageServiceProvider { use UseNeonConfig; public function packageRegistered(): void { $this->handleNeon()->forConfig(); } ...
Usage
Creating a .neon file at the root of the package that uses yours overwrites your package configuration.
config/your-package.php:
return [ 'key1' => 'value1', 'key2' => 'value2', 'array' => [ 'array_key1' => 'array_value1', 'array_key2' => 'array_value2', ], ];
your-package.neon:
key1: new value array: - new_array_key: new_value - array_key2: array_value
Values are merged by keys, which means : values with the same keys are replaces, omitted keys are kept, new keys are added.
Casting and dynamic values
Enums
If your config uses Enums, it is possible to reflect and cast it in the neon file.
config/your-package.php:
return [ 'key1' => SomeEnumClass::VALUE, ];
At the root of your package, create a file named neon-config.neon:
neon-config.neon:
casts: - key1: \Namespace\SomeEnumClass
Then, the overwrite is written in plain text with the value of the enum to use: your-package.neon:
key1: newValue
Now, calling config('your-package.key1')
gives an enum \Namespace\SomeEnumClass::NEW_VALUE instance.
Array of Enums
config/your-package.php:
return [ 'key1' => [ SomeEnumClass::VALUE1, SomeEnumClass::VALUE2, SomeEnumClass::VALUE3, ], ];
neon-config.neon:
casts: - key1: AsEnumCollection::\Namespace\SomeEnumClass
your-package.neon:
key1: - value4 - value5 - value6
Dynamic values from other config or env variables
config/your-package.php:
return [ 'key1' => config('some.config', 'default value'), 'key2' => env('MY_ENV_VAR', 'default value' ), ];
It is possible to keep the usage of the config() and env() methods and give the possibility of overwriting their keys:
neon-config.neon:
casts: - key1: AsConfig::some.config - key2: AsEnv::MY_ENV_VAR
your-package.neon:
key1: other.config.key(type: config, default: 'other default value') key2: OTHER_ENV(type: env)
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.