Jump to content

Dynamic Configuration File Updater


RobertP

Recommended Posts

i have coded a simple yet very diverse method to update an array inside configuration.php, while retaining comments / user notes.

 

this method works no problem on a windows platform, but since my project needs to be cross-platform i need to figure out what the issue is on my ubuntu. i know that the issue is in the $replacements array (containing a key => value for a simple preg_replace)

 

$keys argument is a simple string array eg: ['debug','siteUrl','defaultTimezone']

$config argument is a preloaded config array eg: ['debug'=>false,'siteUrl'=>'hxxp://127.0.0.1','defaultTimezone'=>'America/Toronto']

 

 public static function saveConfigByKey($keys, $config) {
if (!is_array($keys)) {
trigger_error('Admin::saveConfigByKey: Invalid keys.', E_USER_NOTICE);
return false;
}
$replacements = array();
foreach ($keys as $key) {
if (!array_key_exists('f_' . $key, $_POST))
return false;
$newVal = addslashes($_POST['f_' . $key]);
$config[$key] = $newVal;
if ($newVal != 'true' && $newVal != 'false' && !is_numeric($newVal))
$newVal = '\'' . $newVal . '\'';
elseif ($newVal == '\'\'') {
$newVal = 'null';
$config[$key] = null;
} elseif ($newVal == 'true' || $newVal == 'false')
$config[$key] = $newVal == 'true';
$replacements['#\'' . $key . '\'=>(.*),#'] = '\'' . $key . '\'=>' . $newVal . ',';
}
$rawConfig = file_get_contents(_SOURCES . 'Configuration.php');
$rawConfig = preg_replace(array_keys($replacements), array_values($replacements), $rawConfig);
echo($rawConfig);exit;
copy(_SOURCES . 'Configuration.php', _SOURCES . 'files' . _DS . 'Configuration.backup.php');
//exit($rawConfig);//test
if (!file_put_contents(_SOURCES . 'Configuration.php', $rawConfig)) {
trigger_error('Admin::saveConfigByKey: Internal error.', E_USER_NOTICE);
copy(_SOURCES . 'files' . _DS . 'Configuration.backup.php', _SOURCES . 'Configuration.php');
return false;
}
return true;
}

 

 

Configuration.php


<?php

if (!defined('_ROOT'))
exit(header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'));

$this->config = array(
//===> general
'debug' => false,
'siteUrl' => 'http://127.0.0.1', //no trailing slash.
'defaultTimezone' => 'America/Toronto',
'displayPageLoadInfo' => true,
'defaultLanguage' => 'english',
'enableSEFLinks' => true,
'defaultPage' => 'index',
'disable404' => false,
);
?>

Edited by RobertP
Link to comment
Share on other sites

Why not just write (and read) a serialized array to (from) the file?

 

Well, what is faster / performance-wise?

 

1. Include a file with an array already defined? (current implementation)

2. Include a file, then unserialize a variable and then populate the $config array?

 

The configuration file does not change, unless an administrator changes a setting inside the admin panel, so if it takes 0.3 seconds longer each time the file is saved, verse 0.3 (over exaggerating a little) each time my site is accessed to parse the config array?

 

My problem is not with my design (imho), it is with my reg-x pattern (is there a difference on what platform i execute preg_replace?)

Edited by RobertP
Link to comment
Share on other sites

The runtime differences are measured in nanoseconds, if that. So that's of no concern what so ever. This is premature micro-optimization, and a well known anti-pattern.

Do what's easiest to maintain, it'll save you a whole lot more time that would otherwise go to developing and debugging. Chances are very high that you'll save a lot more time by doing what's easiest for you, than doing what is programatically the fastest method.

Link to comment
Share on other sites

For what it's worth, you could consider creating your configuration file in the same structure as a PHP ini file and then use the PHP function parse_ini_file() to read the settings into memory as an associative array. There is also a user contributed write_php_ini in the user comments for writing the ini file. Personally, I would write the file in a way that is easily readable by a human.

Edited by Psycho
Link to comment
Share on other sites

For what it's worth, you could consider creating your configuration file in the same structure as a PHP ini file and then use the PHP function parse_ini_file() to read the settings into memory as an associative array. There is also a user contributed write_php_ini in the user comments for writing the ini file. Personally, I would write the file in a way that is easily readable by a human.

 

Thank you for the hint towards parse_ini_file, this might just be my solution..

 

is there a difference on the line-endings??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.