RobertP Posted November 19, 2012 Share Posted November 19, 2012 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, ); ?> Link to comment https://forums.phpfreaks.com/topic/270893-dynamic-configuration-file-updater/ Share on other sites More sharing options...
Christian F. Posted November 20, 2012 Share Posted November 20, 2012 Why not just write (and read) a serialized array to (from) the file? Link to comment https://forums.phpfreaks.com/topic/270893-dynamic-configuration-file-updater/#findComment-1393699 Share on other sites More sharing options...
RobertP Posted November 20, 2012 Author Share Posted November 20, 2012 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?) Link to comment https://forums.phpfreaks.com/topic/270893-dynamic-configuration-file-updater/#findComment-1393824 Share on other sites More sharing options...
Christian F. Posted November 20, 2012 Share Posted November 20, 2012 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 https://forums.phpfreaks.com/topic/270893-dynamic-configuration-file-updater/#findComment-1393906 Share on other sites More sharing options...
Psycho Posted November 20, 2012 Share Posted November 20, 2012 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. Link to comment https://forums.phpfreaks.com/topic/270893-dynamic-configuration-file-updater/#findComment-1393910 Share on other sites More sharing options...
RobertP Posted November 22, 2012 Author Share Posted November 22, 2012 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 https://forums.phpfreaks.com/topic/270893-dynamic-configuration-file-updater/#findComment-1394401 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.