Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a "two way" configuration file

I am writing a PHP application targeted at non-geeks, non-programmers. I need to create an option page with a bunch of "options" and then store those options...somewhere. Using a database application (MySQL/PostgreSQL/SQLite) is out of the question because it will require more configuration than the user needs to do (I don't want the user to do any kind of configuration if he doesn't want to). So the only solution left is to write the configuration to a configuration file. On the other hand, I also want that configuration file to be human-readable in case the user is a geek and he wants to edit the config file directly (or if he wants to edit the file remotely via SSH or any kind of reason...)

Here are the couple of potential solutions I found:

Using a JSON file...

...Retrieve the data from the file, using json_decode to convert the data, output it into HTML, retrieve any changes, encode back using json_encode, etc. You get the picture. There are a couple things that I don't like about this method, the main one being that the encoded JSON data using PHP will no be well formatted and very hard to edit without being reformatted beforehand.

Using an XML file

I won't describe that solution because I don't really like it either...and I don't know how to use XSLT and don't really want to learn...and because it's a pretty heavyweight solution, at least compared to the JSON solution. Correct me if I'm wrong.

Using an INI file

I love INI files, really I love them! I think they're really the most readable, and it's hard to mess up (ie: syntax errors). The problem with that solution is that there is no native way to write/edit an ini file. I found a topic showing a custom method to write one...that might be the solution I will adopt if I don't find anything better...

Using two files

That last solution seems as reasonable as the INI solution. In fact, I could use an INI file as "input" (the file that the user would edit if he wants to) and an XML/JSON file as output (the file that will be edited by PHP every time the user changes options using the web front-end). At this point, the best solution would be to ask the user to reload the configuration manually if he edited the config file directly, so that the "output" file is always up to date.


I know none of the solutions above are perfect, and that's why I created this topic to ask for advice. What is the best solution? Maybe (probably) I missed yet another solution.

One last thing: YAML isn't a valid solution because it's a lot easier to mess up the syntax if you're not used to it. PHP is not a solution either because editing PHP with PHP is a pain. PHP is only a good solution if I want to retrieve some configuration but not edit it directly via a web front-end.

like image 640
Robert Audi Avatar asked Jun 21 '10 03:06

Robert Audi


2 Answers

ini

I'd write ini files, myself. As you said, the syntax is very simple, and that's what you want in a config file. The ini format's "key+value" pairing is exactly what you'd get with a database—without the database.

Related SO you may have seen already: create ini file, write values in PHP

Plus you can use parse_ini_file() to read it.

XML

XML isn't all that bad. It may be more work to write it (and may not be as clear to the user as an ini file), but reading it is really easy.

Create it:


<?php
// Create file
$xml = new SimpleXMLElement( '<?xml version="1.0" ?><config></config>' );

// Add stuff to it
$xml->addChild( 'option1' );
$xml->option1->addAttribute( 'first_name', 'billy' );
$xml->option1->addAttribute( 'middle_name', 'bob' );
$xml->option1->addAttribute( 'last_name', 'thornton' );
$xml->addChild( 'option2' );
$xml->option2->addAttribute( 'fav_dessert', 'cookies' );

// Save
$xml->asXML( 'config.xml' );
?>

Read it:


<?php
// Load
$config = new SimpleXMLElement( file_get_contents( 'config.xml' ) );

// Grab parts of option1
foreach( $config->option1->attributes() as $var )
{
    echo $var.' ';
}

// Grab option2
echo 'likes '.$config->option2['fav_dessert'];
?>

Which gives you:

billy bob thornton likes cookies

Documentation for SimpleXML

  • SimpleXML Docs index
  • Basic Examples
  • Details on addChild() and addAttribute(), showing how to generate various XML structures (nested tags vs. attributes, for example)
like image 109
cbednarski Avatar answered Sep 19 '22 23:09

cbednarski


I'd go with the ini. They're really not that hard to write. I personally hate XML. It's so bloated... even if the file size doesn't matter, it still makes me cringe at it's wordiness and the amount of typing I have to do. Plus, people are dumb. They won't close their tags.

like image 29
mpen Avatar answered Sep 19 '22 23:09

mpen