DEV Community

Cover image for PHP Simple two way config package, and a backstory
Mazen Touati
Mazen Touati

Posted on • Updated on

PHP Simple two way config package, and a backstory

Hi, folks

I'm happy to introduce my first open source project. Which is a two way PHP-based configuration package. This package is designed to make it easy synchronizing the source file with the run-time values. It's suitable for applications that require the use of file-based system to store preferences or configuration.

GitHub logo sunchayn / simple-2way-config

Simple 2 way (read/write) php-based configuration

mazentouati/simple-2way-config

GitHub (pre-)release Build Status Scrutinizer Code Quality Codecov branch StyleCI Maintainability Software License

Simple 2 way configuration is a php-based read and write configuration library. It's suitable for applications that require the use of file system to store preferences or configuration.

Installation

we recommend installing this package through composer :

composer require mazentouati/simple-2way-config
Enter fullscreen mode Exit fullscreen mode

Usage

The simplest way to use it is through the package's factory. The factory's required parameter is the path of the directory that holds your config files.

use MazenTouati\Simple2wayConfig\S2WConfigFactory
$config = S2WConfigFactory::create( __DIR__ . '/demo' );
Enter fullscreen mode Exit fullscreen mode

Now you can access to a config value using dot notation '{filename}.path.to.value'

$host = $config->get('database.drivers.mysql.host');
Enter fullscreen mode Exit fullscreen mode

Note: your config file should be an array-based configuration, check this example

API

the config API implements the S2WConfigInterface.

the examples shown below will assume that you already assigned your config to a variable called $config

get(string $path, mixed $default =

example of usage

use MazenTouati\Simple2wayConfig\S2WConfigFactory;

$config = S2WConfigFactory::create( __DIR__ . '/demo' );

// Get a value from the config
$old_host = $config->get('database.drivers.mysql.host');

// Change the value at the run-time
$config->set('database.drivers.mysql.host', '127.0.0.1');

// Sync the changes with the source file
$config->sync('database');

Enter fullscreen mode Exit fullscreen mode

If you like the project kindly share love and leave a start, it will help me a lot. Suggestions and contributions are welcomed too.

Back story

Somewhere in my full-stack development journey, I decided to be involved in the open source industry and practice my knowledge more and more, especially for the front-end part as it's evolving so fast with a lot of over-engineered libraries and frameworks. So I started planning then creating a project I thought about for a while. Which is a database toolkit dedicated to small and medium project. Well, it's totally worth it as I learnt a lot of new things. From comparing and testing: Vue, Angular and React, later I abandoned them, to creating my own solutions or small packages like: templating, data binding, etc. I don't think that I'm re-inventing the wheel in this case because as I mentioned before there's a plenty of over-engineered solutions that aim to solve all problems and cover all use cases. I'm a simple, pragmatic man, I want only to use what's enough for me and my product. Bombarding it with a lot of unused code or generic solution is against my most prioritized goal (for the toolkit) which is "Performance".

With that said, it brings us to this post subject, I came across a point where I needed to make configuring the database credentials read and write. I don't have another choices, I searched for a simple package that offers the ability to make the configuration two ways but noway.
Here is my use case: I'm building a SQL console that execute commands one by one, when the Developer type use _database_ it should take the database name and update it in the config file then return a success message to the console interface (using AJAX of course). So when he continues throwing SQL commands to the console the Back-end code uses the new database name. Which will become persistent for future use. In a case like this I didn't have the luxury to use a database for dynamic configuration. To solve my issue, I forked an existing package and added the write functionality just to be able to continue working on the console. I used composer VCS repositories to load the fork. Later, after I finished the console I created this package and implemented it in my toolkit because I wasn't comfortable enough with that fork.

The console (part of the toolkit) is ready for a pre-release I'll publish it as soon as I finish creating it's homepage.I'll post more details about my journey creating this toolkit when I release it.

These are the things I have learned till now as far as I can recall (some I heard about but never used them, some I knew them but I practiced them more)

  • Javascript: React, Vue, Reveal Pattern, Sub-pub, Data binding, ES2016, Gulp, More VanillaJS, HTML Tag's templating ( usings custom attribute ) ...
  • CSS: SMACSS, More flexbox
  • PHP: Unit Testing, Continuous Integration, Slim Micro Framework, Code-sniffer, More composer
  • Other: Git, Vim, Lossless coupled components, Modular Programming in general, more command line...

Again, I'm very happy and satisfied for what I passed through till now. Go ahead and start creating your open source projects.

practice makes perfect

I'd like to hear your thoughts, peace !

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.