1

I'm trying to build a small CMS using CodeIgniter, and I need to be able to dynamically update some variables within the application/config.php

So far I did:

private function update_file ($file, $var, $var_name) {
    $start_tag = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n";
    if (file_exists($file)) {
        require_once ($file);
        $updated_array = array_merge($$var_name, $var);         
        $data = $start_tag."\$".$var_name." = ".var_export($updated_array, true).";";
        file_put_contents($file, $data);
    } else {
        return false;
    }
}

Everything works just fine! The result in the config.php file will be:

<?php ...;
$config = array (
'base_url' => '',
...
...
);

But what if I would like to maintain the original config.php file format with comments, spaces and
separated declared $config['key'] = 'value' ... ?

Is that possible ?


EDIT:

Thank you for your answers, very precious. I found a slightly different solution for my needs, performing a preg_replace on the return of file_get_contents() and then write back on the file the new resulting string. File maintains the exact original clean format.

private function update_file ($file, $var, $var_name) {
    if (file_exists($file)) {
        require_once ($file);

        $contents = file_get_contents($file);
        $updated_array = array_merge($$var_name, $var);
        $search = array();
        $replace = array();

        foreach($$var_name as $key => $val) {
            $pattern = '/\$'.$var_name.'\[\\\''.$key.'\\\'\]\s+=\s+[^\;]+/';
            $replace_string = "\$".$var_name."['".$key."'] = ".var_export($updated_array[$key], true);      
            array_push($search, $pattern);
            array_push($replace, $replace_string);
        }

        $new_contents = preg_replace($search, $replace, $contents);
        write_file($file, $new_contents);
}

Maybe it requires some slight performance improvements. But this is my baseline idea.

4

2 に答える 2

1

create the keys with empty values

$config['base_url'] = '';

then set them inside any of your controllers. This works best if you store the values in the db, and initialize them in MY_Controller.

$this->config->set_item('base_url', 'value');
于 2012-05-01T21:53:55.743 に答える
0

可能です。コードが見つかりませんが、そのようなものを書いたら。全体的なアイデアは、テンプレートファイルをトークン化し、配列内の値を置き換え、キーの順序、行番号、およびテンプレートからのコメントを保持することに基づいていました。

[+]それを見つけました。目的は、次のように見えるテンプレートから値を入力することでした(もちろんはるかに大きかった):

<?php

$_CFG = array(

    // DB section
    'db_host' => 'localhost',
    'db_user' => 'root',
    'db_pass' => '',
    'db_name' => 'test',

    // Site specific
    'lang' => array('pl','en'),
    'admin' => 'admin@example.com',
);

そして、すべての魔法を実行していたコード:

$tokens = token_get_all(file_get_contents('tpl/config.php'));

$level = -1;
$buffer = '';
$last_key = 0;
$iteration = 0;

foreach($tokens as $t){
    if($t === ')'){
        $iteration = 0;
        $last_key = 0;
        $level--;
    }

    if(is_array($t)){
        if($t[0] == T_ARRAY && strtolower($t[1]) === 'array')
            $level++;

        if($t[0] == T_CONSTANT_ENCAPSED_STRING){
            if($last_key){
                if($level){
                    if(isset($new_config[$last_key][$iteration])){
                        $buffer .= var_export($new_config[$last_key][$iteration], TRUE);
                    }
                    else
                        $buffer .= 'null';

                    $iteration++;
                }
                else{
                    if(isset($new_config[$last_key]))
                        $buffer .= var_export($new_config[$last_key], TRUE);
                    else
                        $buffer .= 'null';

                    $last_key = 0;
                }
            }
            else{
                $buffer .= $t[1];
                $last_key = trim($t[1],"'");
            }
        }
        else
            $buffer .= $t[1];
    }
    else
        $buffer .= $t;
}

file_put_contents('config.php',$buffer);
于 2012-05-01T21:31:59.417 に答える