PEAR_Config::set PHP Method

set() public method

Enforces the types defined in the configuration_info array. An integer config variable will be cast to int, and a set config variable will be validated against its legal values.
public set ( $key, $value, $layer = 'user', $channel = false ) : boolean
return boolean TRUE on success, FALSE on failure
    function set($key, $value, $layer = 'user', $channel = false)
    {
        if ($key == '__channels') {
            return false;
        }
        if (!isset($this->configuration[$layer])) {
            return false;
        }
        if ($key == 'default_channel') {
            // can only set this value globally
            $channel = 'pear.php.net';
            if ($value != 'pear.php.net') {
                $this->_lazyChannelSetup($layer);
            }
        }
        if ($key == 'preferred_mirror') {
            if ($channel == '__uri') {
                return false;
                // can't set the __uri pseudo-channel's mirror
            }
            $reg =& $this->getRegistry($layer);
            if (is_object($reg)) {
                $chan =& $reg->getChannel($channel ? $channel : 'pear.php.net');
                if (PEAR::isError($chan)) {
                    return false;
                }
                if (!$chan->getMirror($value) && $chan->getName() != $value) {
                    return false;
                    // mirror does not exist
                }
            }
        }
        if (!isset($this->configuration_info[$key])) {
            return false;
        }
        extract($this->configuration_info[$key]);
        switch ($type) {
            case 'integer':
                $value = (int) $value;
                break;
            case 'set':
                // If a valid_set is specified, require the value to
                // be in the set.  If there is no valid_set, accept
                // any value.
                if ($valid_set) {
                    reset($valid_set);
                    if (key($valid_set) === 0 && !in_array($value, $valid_set) || key($valid_set) !== 0 && empty($valid_set[$value])) {
                        return false;
                    }
                }
                break;
        }
        if (!$channel) {
            $channel = $this->get('default_channel', null, 'pear.php.net');
        }
        if (!in_array($channel, $this->_channels)) {
            $this->_lazyChannelSetup($layer);
            $reg =& $this->getRegistry($layer);
            if ($reg) {
                $channel = $reg->channelName($channel);
            }
            if (!in_array($channel, $this->_channels)) {
                return false;
            }
        }
        if ($channel != 'pear.php.net') {
            if (in_array($key, $this->_channelConfigInfo)) {
                $this->configuration[$layer]['__channels'][$channel][$key] = $value;
                return true;
            }
            return false;
        }
        if ($key == 'default_channel') {
            if (!isset($reg)) {
                $reg =& $this->getRegistry($layer);
                if (!$reg) {
                    $reg =& $this->getRegistry();
                }
            }
            if ($reg) {
                $value = $reg->channelName($value);
            }
            if (!$value) {
                return false;
            }
        }
        $this->configuration[$layer][$key] = $value;
        if ($key == 'php_dir' && !$this->_noRegistry) {
            if (!isset($this->_registry[$layer]) || $value != $this->_registry[$layer]->install_dir) {
                $this->_registry[$layer] =& new PEAR_Registry($value);
                $this->_regInitialized[$layer] = false;
                $this->_registry[$layer]->setConfig($this, false);
            }
        }
        return true;
    }

Usage Example

コード例 #1
0
ファイル: Role.php プロジェクト: jubinpatel/horde
 /**
  * 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::set