PEAR_Config::writeConfigFile PHP Method

writeConfigFile() public method

Writes data into a config layer from a file.
public writeConfigFile ( $file = null, $layer = 'user', $data = null ) : boolean
return boolean TRUE on success or a PEAR error on failure
    function writeConfigFile($file = null, $layer = 'user', $data = null)
    {
        $this->_lazyChannelSetup($layer);
        if ($layer == 'both' || $layer == 'all') {
            foreach ($this->files as $type => $file) {
                $err = $this->writeConfigFile($file, $type, $data);
                if (PEAR::isError($err)) {
                    return $err;
                }
            }
            return true;
        }
        if (empty($this->files[$layer])) {
            return $this->raiseError("unknown config file type `{$layer}'");
        }
        if ($file === null) {
            $file = $this->files[$layer];
        }
        $data = $data === null ? $this->configuration[$layer] : $data;
        $this->_encodeOutput($data);
        $opt = array('-p', dirname($file));
        if (!@System::mkDir($opt)) {
            return $this->raiseError("could not create directory: " . dirname($file));
        }
        if (file_exists($file) && is_file($file) && !is_writeable($file)) {
            return $this->raiseError("no write access to {$file}!");
        }
        $fp = @fopen($file, "w");
        if (!$fp) {
            return $this->raiseError("PEAR_Config::writeConfigFile fopen('{$file}','w') failed ({$php_errormsg})");
        }
        $contents = "#PEAR_Config 0.9\n" . serialize($data);
        if (!@fwrite($fp, $contents)) {
            return $this->raiseError("PEAR_Config::writeConfigFile: fwrite failed ({$php_errormsg})");
        }
        return true;
    }

Usage Example

Beispiel #1
0
 /**
  * Run task after prompt.
  *
  * @param array $info   Parameter array.
  * @param string $name  Postinstall phase.
  */
 public function run($info, $phase)
 {
     if ($phase !== 'first') {
         return;
     }
     if (!$this->_config->set('horde_dir', $info['horde_dir'], 'user', 'pear.horde.org')) {
         print "Could not save horde_dir configuration value to PEAR config.\n";
         return;
     }
     $res = $this->_config->writeConfigFile();
     if ($res instanceof PEAR_Error) {
         print 'ERROR: ' . $res->getMessage() . "\n";
         exit;
     }
     print "Configuration successfully saved to PEAR config.\n";
 }
All Usage Examples Of PEAR_Config::writeConfigFile