⬆️ ⬇️

Flexible configuration with PHPixie

image



When developing it is often necessary to support several environments at once: development, staging. production, etc., which can have quite different settings. The simplest example is the parameters of a database connection, but sometimes you want to change not only one parameter but a whole section, for example, logging or backend for the cache. In addition, when developing various CMS, sometimes you want to put some of the settings in a prominent place to make it easier for the user to find them. Let's see how these problems are solved in PHPixie. And of course, it all works without the framework itself and is much lighter than the symfony / config package.





')

Options



The simplest thing we can do is create several variables that we can use later in the config, for this we create the file /assets/parameters.php :



<?php // /assets/parameters.php return [ 'apiToken' => 'jdf73jdhgj', //     'database' => [ 'connection' => 'mysql:host=localhost;dbname=quickstart', 'user' => 'root', 'password' => 'secret' ] ]; 




Now we can use them in configuration files through % name% , for example:



 <?php // /assets/config/database.php return [ 'default' => [ 'driver' => 'pdo', 'connection' => '%database.connection%', 'user' => '%database.user%', 'password' => '%database.password%' ] ]; 




As a result, we separate the variables themselves from where they are used. It is reasonable then to add parameters.php to .gitignore and then the code can be freely divided into a github.



But what if we need more? For example, add a few routes that only work for developers and are disabled in production? For this we need an overlay.



Overlays



Suppose in one of the configurations we want to also connect the database to MongoDB. To do this, create a folder in with some beautiful name, for example local, and add local changes to it:



 <?php // /assets/config/local/database.php return [ //       //     'default'  . //     array_replace_recursive() 'mongo' => [ 'driver' => 'mongo', 'database' => '%mongo.database%', 'user' => '%mongo.user%', 'password' => '%mongo.password%', ] ]; 




If there is little change, you can not even create a folder but simply put everything in one file:



 <?php // /assets/config/local.php return [ 'database' => [ //          // ,        //   'default' 'mongo' => [ 'driver' => 'mongo', 'database' => '%mongo.database%', 'user' => '%mongo.user%', 'password' => '%mongo.password%', ] ] ]; 




It remains only to enable it, for this we add one line to parameters.php :



 <?php // /assets/parameters.php return [ 'configOverlay' => 'local', // ... ]; 




Of course, you can create several such overlays and switch between them as you like.



How to see the result?



For debugging:



 print_r($frameworkBuilder->configuration()->config()->get()); 




Use without framework



All functionality has been added to the Slice and Config base libraries. When creating a configuration, we can specify a different configuration or Slice from which the parameters will be loaded:



 $configBuilder = new \PHPixie\Config(); $parameterStorage = $configBuilder->file('parameters.php'); $rootDir = ...; //        (  ) $dirName = 'config'; //     $configuration = $configBuilder->directory($rootDir, $dirName, 'php', $parameterStorage); 




The overlay is even simpler; in fact, you can merge two Slice instances or configurations into one. In this case, lazy file upload will continue to work normally.



 $sliceBuilder = new \PHPixie\Slice(); $configBuilder = new \PHPixie\Config(); $configuration = $configBuilder->file('config.php'); $overlay = $configBuilder->file('overlay.php'); $merged = $sliceBuilder->mergeData($configuration, $overlay); 

Source: https://habr.com/ru/post/301152/



All Articles