生成config文件到内存中

<?php

$site_code='property';
        $common = [ 'API_HOST_V3'=>'http://10.1.118.12:83/apiv3.json' ,'SITE_CODE'=>'CP'];
        $api_v3 = 'http://10.1.118.12:83/apiv3.json';
        $data = [['api'=>"config/lists", 'data'=>['site_code'=>' '.$site_code.' ']]];
        //$data = [['api'=>"appMemberAuth/lists", 'data'=>['SITE_CODE'=>'CP']]];
        $data_string = json_encode($data);
        $header = [
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)
        ];
        $ch = curl_init($api_v3);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $result = curl_exec($ch);
        $result = json_decode($result,true);
        $result_data = $result['data'][0]['data'];
        $result_lists = json_decode($result_data);
        $cfg_file_path = RUNTIME_PATH.'/config.php';
        if(!file_exists($cfg_file_path))
        {
                        
            file_put_contents($cfg_file_path, "<?php".PHP_EOL, FILE_APPEND);
            file_put_contents($cfg_file_path, "    return array(".PHP_EOL, FILE_APPEND);
            
            foreach ($result_lists->data->lists as $key=>$item)
            {
                $config_str = "";
                if($item->value==='true')
                {
                    $config_str = $config_str . "'". $item->key ."'";
                    $config_str = $config_str . "=>  true ,".PHP_EOL;
                }else if($item->value==='false')
                {
                    $config_str = $config_str . "'". $item->key ."'";
                    $config_str = $config_str . "=>  false ,".PHP_EOL;
                }else if($item->data_type==='array')
                {
                    if(is_array(json_decode($item->value,true)))
                    {    
                        $config_str = $config_str . "'". $item->key ."'";
                        $config_str = $config_str . "=> [".PHP_EOL;
                       foreach(json_decode($item->value,true) as $k=>$v)
                       { 
                                $config_str = $config_str . "'". $k ."'";
                                 if(is_array($v))
                                {
                                    $config_str = $config_str . "=>[".PHP_EOL;
                                    foreach($v as $kk=>$vv)
                                    {
                                        $config_str = $config_str . "'". $kk ."'";
                                            
                                        $config_str = $config_str . "=>'" .$vv ."',".PHP_EOL;
                                    }
                                    $config_str = $config_str . "],".PHP_EOL;
                                }else{
                                    $config_str = $config_str . "=>'" .$v ." ',".PHP_EOL;
                                }    
                       }
                       $config_str = $config_str . "],".PHP_EOL;
                    }
                    $data_arr[]=$item->value;

                }else if(is_numeric($item->value))
                {
                    $config_str = $config_str . "'". $item->key ."'";
                    $config_str = $config_str . "=>  " .$item->value .",".PHP_EOL;
                }else{
                    $config_str = $config_str . "'". $item->key ."'";
                    $config_str = $config_str . "=>  '" .$item->value ."',".PHP_EOL;
                }
                
                file_put_contents($cfg_file_path, $config_str, FILE_APPEND);
            }
            
            file_put_contents($cfg_file_path, ");", FILE_APPEND);
        }
         return  array_merge(file_exists($cfg_file_path) ? require_once $cfg_file_path : [], $common);
原文地址:https://www.cnblogs.com/stj123/p/12354275.html