Unnecessary Evils: PHP vs YAML for configuration files part two

When I chose PHP over YAML as in the configuration files for the Content Connection Kit. It was a good choice but it came with some bad memories of awful attempts by other projects. Often in CMS'slike Drupal you'll see mixtures of syntax and no real conventions used for formatting the settings file. Too much chaos for me so I set out to do better with CCK and came up with something special.


 $databases['default']['default'] = array(
      'driver' => 'mysql',
      'database' => 'databasename',
      'username' => 'username',
      'password' => 'password',
      'host' => 'localhost',
      'prefix' => '',
    );
    $databases['default']['default'] = array(
      'driver' => 'pgsql',
      'database' => 'databasename',
      'username' => 'username',
      'password' => 'password',
      'host' => 'localhost',
      'prefix' => '',
    );
    $databases['default']['default'] = array(
     'driver' => 'sqlite',
      'database' => '/path/to/databasefilename',
    );
Using two functions of PHP var_export($array,true) and array_replace_recursive() I wrote a system that works very well with using the forms api for entry and updating the configuration file.
Some configuration was done by hand during development. Out of habit I wrote the arrays in shorthand. Like this:
'modules' => array(
            "admin",
            "blog",
            "category",
            "contact",
            "content",
            "menu",
            "portfolio",
            //"services",
            "users",

        ),
        'hooks' => array(
            "hook_links",
            "hook_forms",
            "hook_content",
            "hook_blocks",
            "hook_access",
            "hook_permissions",
            "hook_admin_links",
            "hook_module_links",
            "hook_debug",
            "hook_js",
            "hook_css",
        ),

        'module_files' => array(
            "admin" => "This file contains information and menus for the adiministration area of this module" ,
            "form" => "Forms used by this module should be described and variables info inputed  in this class file before rendering.",
            "api"  => "Resources made available to services API in this class file per method",

        ),
        'datasource_default' => array(
            'resource' => "cck.sqlite",
            'hostname' => DOCROOT . DIRECTORY_SEPARATOR . "_db" . DIRECTORY_SEPARATOR,
            'username' => "cck",
            'pwd' => "administrator",
            'type' => "sqlite3"
        ),
        'datasource_0' => array(
            'resource' => "cck_development",
            'hostname' => "data.fhqk.com",
            'username' => "cck",
            'pwd' => "zZ_tofu8",
            'type' => "mysqli"
        ),
        'localhost' => array(
            'resource' => "cck_development",
            'hostname' => "localhost",
            'username' => "root",
            'pwd' => "",
            'type' => "mysqli"
        ),
        'theme_variables' => array(
            'content_title' => "Content Connection Kit",
            'content_footer' =>"Do you want to replace Drupal with something better  faster and easier to understand?"
        ),
        'api_keys' => array(
            'google' => "",
            'facebook' => "",
            'linkedin' => "",
            'twitter' => "",
        )

But is not a problem since any manual work that was done in the this format gets converted to the correct one when the array is regenerated by the system for writing arrays to the file.
$settings['site']['name']['value'] = "Cool Content Kit";
$settings['site']['name']['description'] = "site name";
$settings['site']['description']['value'] = "Web Architecture for web programmers";
$settings['site']['description']['description'] = "site purpose";
$settings['site']['frontpage']['value'] = "index.php?blog/blog_latest";
$settings['site']['frontpage']['description'] = "landing page";
$settings['site']['theme']['value'] = "default";
$settings['site']['theme']['description'] = "templates and visual appearance";
$settings['site']['404_page']['value'] = "cck/not_found";
$settings['site']['404_page']['description'] = "templates and visual appearance";
$settings['site']['message_timeout']['value'] = "5";
$settings['site']['message_timeout']['description'] = "flash messages stop appearing after this time";
$settings['site']['user_timeout']['value'] = "3600";
$settings['site']['user_timeout']['description'] = "user is logged out automatically";
$settings['system']['version']['value'] = "1.x";
$settings['system']['version']['description'] = "";
$settings['system']['base_url']['value'] = "http//cck.fhqk.com";
$settings['system']['base_url']['description'] = "";
$settings['system']['approot']['value'] = "/_configuration";
$settings['system']['approot']['description'] = "";
$settings['system']['core']['value'] = "";
$settings['system']['core']['description'] = "";
$settings['system']['controllers']['value'] = "_controllers";
$settings['system']['controllers']['description'] = "directory containing controller modules";
$settings['system']['models']['value'] = "_models";
$settings['system']['models']['description'] = "directory containing model classes";
$settings['system']['views']['value'] = "_views";
$settings['system']['views']['description'] = "directory containing themes and templates";
$settings['system']['themes']['value'] = "_views/themes";
$settings['system']['themes']['description'] = "sub directory to views";
$settings['system']['helpers']['value'] = "_helpers";
$settings['system']['helpers']['description'] = "directory containing external libraries";
$settings['owner']['name']['value'] = "_helpers";
$settings['owner']['name']['description'] = "directory containing external libraries";
$settings['owner']['password']['value'] = "_helpers";
$settings['owner']['password']['description'] = "directory containing external libraries";
$settings['owner']['email']['value'] = "carlmcdadegmail.com";
$settings['owner']['email']['description'] = "the email address to be used by the site";
$settings['modules'][0] = "admin";
$settings['modules'][1] = "blog";
$settings['modules'][2] = "category";
$settings['modules'][3] = "contact";
$settings['modules'][4] = "content";
$settings['modules'][5] = "menu";
$settings['modules'][6] = "portfolio";
$settings['modules'][7] = "users";
$settings['hooks'][0] = "hook_links";
$settings['hooks'][1] = "hook_forms";
$settings['hooks'][2] = "hook_content";
$settings['hooks'][3] = "hook_blocks";
$settings['hooks'][4] = "hook_access";
$settings['hooks'][5] = "hook_permissions";
$settings['hooks'][6] = "hook_admin_links";
$settings['hooks'][7] = "hook_module_links";
$settings['hooks'][8] = "hook_debug";
$settings['hooks'][9] = "hook_js";
$settings['hooks'][10] = "hook_css";
$settings['module_files']['admin'] = "This file contains information and menus for the adiministration area of this module";
$settings['module_files']['form'] = "Forms used by this module should be described and variables info inputed  in this class file before rendering.";
$settings['module_files']['api'] = "Resources made available to services API in this class file per method";
$settings['datasource_default']['resource'] = "cck.sqlite";
$settings['datasource_default']['hostname'] = "/var/vhosts/fhqk.com/www/informationtechnology/cck/_db/";
$settings['datasource_default']['username'] = "Cosmonautk";
$settings['datasource_default']['pwd'] = "Kradmin99_0";
$settings['datasource_default']['type'] = "sqlite3";
$settings['datasource_0']['resource'] = "cck_development";
$settings['datasource_0']['hostname'] = "cck.fhqk.com";
$settings['datasource_0']['username'] = "cck";
$settings['datasource_0']['pwd'] = "zESZ_tofu8";
$settings['datasource_0']['type'] = "mysqli";
$settings['localhost']['resource'] = "cck_development";
$settings['localhost']['hostname'] = "localhost";
$settings['localhost']['username'] = "root";
$settings['localhost']['pwd'] = "";
$settings['localhost']['type'] = "mysqli";
$settings['theme_variables']['content_title'] = "Content Connection Kit";
$settings['theme_variables']['content_footer'] = "Do you want to replace Drupal with something better  faster and easier to understand?";
$settings['api_keys']['google'] = "";
$settings['api_keys']['facebook'] = "";
$settings['api_keys']['linkedin'] = "";
$settings['api_keys']['twitter'] = "";
Using this you can easily create a form for a particular section of the configuration or leave it to manual settings only. The only soft requirement being to write some extra lines for instruction or comment the code, your choice.

Comments