majermi4 / friendly-config
Provides a friendlier way to define Symfony configuration using plain old PHP objects.
Installs: 30 603
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 2
Open Issues: 0
Requires
- php: ^7.4|^8.0
- nette/utils: ^3.2
- phpstan/phpdoc-parser: ^1.2.0
- symfony/config: ^5.0|^6.0
- symfony/dependency-injection: ^5.0|^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^v2.19.3
- phpstan/phpstan: ^1.4.6
- phpunit/phpunit: ^9.5.16
This package is auto-updated.
Last update: 2025-03-16 13:48:46 UTC
README
Provides a friendlier way to define Symfony configuration using plain old PHP objects.
We love the Symfony configuration component! ❤️ It provides schema, validation, documentation and many more features to our bundle configs. However, many of us don't like defining it ... The goal of this project is to change that.
Documentation
Installation
This is installable via Composer as majermi4/friendly-config:
composer require majermi4/friendly-config
Basic usage:
Instead of having to write configuration such as this:
$rootNode ->children() ->arrayNode('connection') ->children() ->scalarNode('driver') ->isRequired() ->cannotBeEmpty() ->end() ->scalarNode('host') ->defaultValue('localhost') ->end() ->scalarNode('username')->end() ->scalarNode('password')->end() ->booleanNode('memory') ->defaultFalse() ->end() ->end() ->end() ->arrayNode('settings') ->addDefaultsIfNotSet() ->children() ->scalarNode('name') ->isRequired() ->cannotBeEmpty() ->defaultValue('value') ->end() ->end() ->end() ->end() ;
Write plain old PHP objects such as:
class MyConfig { public function __construct(Connection $connection, Settings $settings) { /* your code */ } } class Connection { public function __construct( string $driver, string $username, string $password, string $host = 'localhost', bool $memory = false, ) { /* your code */ } } class Settings { public function __construct(string $name = 'value') { /* your code */ } }
The following few lines will convert your pure PHP objects into valid Symfony configuration that defines schema of your bundle configuration. On top of that, the processed configuration values are used to initialize your pure PHP objects, so you can easily access the processed values.
You can register the initialised config objects as services which will allow you to easily access the initialised config objects anywhere in your application.
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Majermi4\FriendlyConfig\FriendlyConfiguration; use Majermi4\FriendlyConfig\InitializeConfigObject; use Majermi4\FriendlyConfig\RegisterConfigService; class MyBundleExtension extends Extension { /** * {@inheritdoc} */ public function getConfiguration(array $config, ContainerBuilder $container) : ConfigurationInterface { return FriendlyConfiguration::fromClass(MyConfig::class, 'my_config'); } /** * {@inheritdoc} */ public function load(array $configs, ContainerBuilder $container): void { $configuration = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); if ($configuration instanceof FriendlyConfiguration) { // Register config object with processed values as a service RegisterConfigService::fromProcessedConfig($configuration->getConfigClass(), $config, $container); // Or ... initialise config object from processed config immediately if needed $initialisedConfig = InitializeConfigObject::fromProcessedConfig(MyConfig::class, $config); } } }