About Phalcon\Config\Adapter\Ini

I've been searching in the docs, but I don't find a way to save changes to an INI file (offsetSet doesn't work), so I supposse I'll just have to use PHP ini functions, right?



3.0k
edited Feb '15

Once i was looking for same thing and not found it =( at my current project i'm using extended Config class with 2 additional functions:

... > public function toString(){ > $config = $this->toArray(); > $string = '; config [2.0]'."\n"; > foreach($config as $key => $option){ > if(isarray($option)){ > $string .= PHPEOL.'['.$key.']'.PHP%5FEOL; > $stringify = httpbuildquery($option); > $stringify = strreplace(array('&', '%5B', '%5D', '=', '+'), array('"'.PHP%5FEOL, '.', '', ' = "', ' '), $stringify).'"'; > $stringify = rawurldecode($stringify).PHPEOL; > $string .= $stringify; > } > else{ > $string .= $key.' = "'.$option.'"'.PHP%5FEOL; > } > } > > return trim($string, PHP_EOL); > } > > public function save($file){ > $file = new IO\Adapter\File($file); > return $file->putContents($this->toString()); > }

...

Not perfect, but i'm satisfied with it. Hope it can help you.



20.0k

I thank your effort, but I'm doing this just one time (for now). It's more quick using PHP functions directly.



47.5k
edited Feb '15

I've found that this works pretty well for my yml files:

        // Merge the custom settings over the defaults
        $configMerged = array_replace_recursive($config1, $config2, $config3);

        // Write the merged settings to the dist directory
        yaml_emit_file("$distDir/etc/config.yaml", $configMerged);

The array_replace_recursive file is able to work through the config and return an actual array.

Also I've been using this ini writer function for a while. I found it online somewhere a long time ago.

function write_ini_file($assoc_arr, $path, $has_sections = FALSE)
{
    $content = ""; 
    if ($has_sections)
    {
        foreach ($assoc_arr as $key=>$elem)
        {
            $content .= "[".$key."]\n"; 
            foreach ($elem as $key2=>$elem2)
            {
                if (is_array($elem2)) 
                { 
                    for ($i = 0; $i<count($elem2); $i++) 
                    {
                        $content .= $key2."[] = \"".$elem2[$i]."\"\n"; 
                    }
                } 
                else if ($elem2 == "")
                    $content .= $key2." = \n";
                else
                    $content .= $key2." = \"".$elem2."\"\n";
            }
        } 
    }
    else
    {
        foreach ($assoc_arr as $key=>$elem)
        {
            if (is_array($elem))
            { 
                for( $i = 0; $i<count($elem); $i++)
                {
                    $content .= $key2."[] = \"".$elem[$i]."\"\n"; 
                } 
            } 
            else if ($elem == "")
                $content .= $key2." = \n"; 
            else
                $content .= $key2." = \"".$elem."\"\n"; 
        } 
    }

    if (!$handle = fopen($path, 'w'))
        return false; 
    if (!fwrite($handle, $content))
        return false;

    fclose($handle);
    return true; 
}