johnkrovitch / configuration
Configuration abstract class for Symfony projects
Installs: 10 816
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 4
Requires
- php: ^8.0
- symfony/dependency-injection: ^5.3 || ^6.0
- symfony/options-resolver: ^5.4 || ^6.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-10-18 04:50:49 UTC
README
Configuration class for Symfony projects. This package allows you to manipulate immutable configuration object. You can add your custom getters to improve parameters php type, or you the default generic getters.
Installation
composer install johnkrovitch/configuration
v2.0: For Symfony 5.1+
v1.0: For Symfony 3.4, 4.4
Usage
Create a configuration class.
<?php use JK\Configuration\Configuration; use Symfony\Component\OptionsResolver\OptionsResolver; class MyConfiguration extends Configuration { public function configureOptions(OptionsResolver $resolver): void { // Configure your options resolver here $resolver->setDefaults([ 'panda' => 'bamboo', ]); } }
Use it in your service for example :
<?php use MyConfiguration; class MyService { public function myMethod(array $options): void { $configuration = new MyConfiguration(); $configuration->configure($options); $configuration->get('panda'); // "bamboo" $configuration->has('panda'); // true $configuration->toArray(); // ['panda' => 'bamboo'] } }
You can also provide a options resolver :
use MyConfiguration; use Symfony\Component\OptionsResolver\OptionsResolver; class MyService { public function myMethod(array $options): void { $configuration = new MyConfiguration(); $resolver = new OptionsResolver(); // Do something with the resolver // $resolver->setDefaults('...'); // Pass your custom resolver $configuration->configure($options, $resolver); $configuration->get('panda'); // Returns "bamboo" $configuration->has('panda'); // Returns true $configuration->toArray(); // Returns ['panda' => 'bamboo'] } }
OptionsResolver
The Configuration use the Symfony OptionsResolver component