[ Index ]
 

Code source de PHP PEAR 1.4.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/PEAR/Command/ -> Config.php (source)

   1  <?php
   2  /**
   3   * PEAR_Command_Config (config-show, config-get, config-set, config-help, config-create commands)
   4   *
   5   * PHP versions 4 and 5
   6   *
   7   * LICENSE: This source file is subject to version 3.0 of the PHP license
   8   * that is available through the world-wide-web at the following URI:
   9   * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10   * the PHP License and are unable to obtain it through the web, please
  11   * send a note to license@php.net so we can mail you a copy immediately.
  12   *
  13   * @category   pear
  14   * @package    PEAR
  15   * @author     Stig Bakken <ssb@php.net>
  16   * @author     Greg Beaver <cellog@php.net>
  17   * @copyright  1997-2006 The PHP Group
  18   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  19   * @version    CVS: $Id: Config.php,v 1.52 2006/03/05 21:32:47 cellog Exp $
  20   * @link       http://pear.php.net/package/PEAR
  21   * @since      File available since Release 0.1
  22   */
  23  
  24  /**
  25   * base class
  26   */
  27  require_once  'PEAR/Command/Common.php';
  28  
  29  /**
  30   * PEAR commands for managing configuration data.
  31   *
  32   * @category   pear
  33   * @package    PEAR
  34   * @author     Stig Bakken <ssb@php.net>
  35   * @author     Greg Beaver <cellog@php.net>
  36   * @copyright  1997-2006 The PHP Group
  37   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  38   * @version    Release: 1.5.0
  39   * @link       http://pear.php.net/package/PEAR
  40   * @since      Class available since Release 0.1
  41   */
  42  class PEAR_Command_Config extends PEAR_Command_Common
  43  {
  44      // {{{ properties
  45  
  46      var $commands = array(
  47          'config-show' => array(
  48              'summary' => 'Show All Settings',
  49              'function' => 'doConfigShow',
  50              'shortcut' => 'csh',
  51              'options' => array(
  52                  'channel' => array(
  53                      'shortopt' => 'c',
  54                      'doc' => 'show configuration variables for another channel',
  55                      'arg' => 'CHAN',
  56                      ),
  57  ),
  58              'doc' => '[layer]
  59  Displays all configuration values.  An optional argument
  60  may be used to tell which configuration layer to display.  Valid
  61  configuration layers are "user", "system" and "default". To display
  62  configurations for different channels, set the default_channel
  63  configuration variable and run config-show again.
  64  ',
  65              ),
  66          'config-get' => array(
  67              'summary' => 'Show One Setting',
  68              'function' => 'doConfigGet',
  69              'shortcut' => 'cg',
  70              'options' => array(
  71                  'channel' => array(
  72                      'shortopt' => 'c',
  73                      'doc' => 'show configuration variables for another channel',
  74                      'arg' => 'CHAN',
  75                      ),
  76  ),
  77              'doc' => '<parameter> [layer]
  78  Displays the value of one configuration parameter.  The
  79  first argument is the name of the parameter, an optional second argument
  80  may be used to tell which configuration layer to look in.  Valid configuration
  81  layers are "user", "system" and "default".  If no layer is specified, a value
  82  will be picked from the first layer that defines the parameter, in the order
  83  just specified.  The configuration value will be retrieved for the channel
  84  specified by the default_channel configuration variable.
  85  ',
  86              ),
  87          'config-set' => array(
  88              'summary' => 'Change Setting',
  89              'function' => 'doConfigSet',
  90              'shortcut' => 'cs',
  91              'options' => array(
  92                  'channel' => array(
  93                      'shortopt' => 'c',
  94                      'doc' => 'show configuration variables for another channel',
  95                      'arg' => 'CHAN',
  96                      ),
  97  ),
  98              'doc' => '<parameter> <value> [layer]
  99  Sets the value of one configuration parameter.  The first argument is
 100  the name of the parameter, the second argument is the new value.  Some
 101  parameters are subject to validation, and the command will fail with
 102  an error message if the new value does not make sense.  An optional
 103  third argument may be used to specify in which layer to set the
 104  configuration parameter.  The default layer is "user".  The
 105  configuration value will be set for the current channel, which
 106  is controlled by the default_channel configuration variable.
 107  ',
 108              ),
 109          'config-help' => array(
 110              'summary' => 'Show Information About Setting',
 111              'function' => 'doConfigHelp',
 112              'shortcut' => 'ch',
 113              'options' => array(),
 114              'doc' => '[parameter]
 115  Displays help for a configuration parameter.  Without arguments it
 116  displays help for all configuration parameters.
 117  ',
 118             ),
 119          'config-create' => array(
 120              'summary' => 'Create a Default configuration file',
 121              'function' => 'doConfigCreate',
 122              'shortcut' => 'coc',
 123              'options' => array(
 124                  'windows' => array(
 125                      'shortopt' => 'w',
 126                      'doc' => 'create a config file for a windows install',
 127                      ),
 128              ),
 129              'doc' => '<root path> <filename>
 130  Create a default configuration file with all directory configuration
 131  variables set to subdirectories of <root path>, and save it as <filename>.
 132  This is useful especially for creating a configuration file for a remote
 133  PEAR installation (using the --remoteconfig option of install, upgrade,
 134  and uninstall).
 135  ',
 136              ),
 137          );
 138  
 139      // }}}
 140      // {{{ constructor
 141  
 142      /**
 143       * PEAR_Command_Config constructor.
 144       *
 145       * @access public
 146       */
 147      function PEAR_Command_Config(&$ui, &$config)
 148      {
 149          parent::PEAR_Command_Common($ui, $config);
 150      }
 151  
 152      // }}}
 153  
 154      // {{{ doConfigShow()
 155  
 156      function doConfigShow($command, $options, $params)
 157      {
 158          if (is_array($params)) {
 159              $layer = isset($params[0]) ? $params[0] : NULL;
 160          } else {
 161              $layer = NULL;
 162          }
 163  
 164          // $params[0] -> the layer
 165          if ($error = $this->_checkLayer($layer)) {
 166              return $this->raiseError("config-show:$error");
 167          }
 168          $keys = $this->config->getKeys();
 169          sort($keys);
 170          $channel = isset($options['channel']) ? $options['channel'] :
 171              $this->config->get('default_channel');
 172          $reg = &$this->config->getRegistry();
 173          if (!$reg->channelExists($channel)) {
 174              return $this->raiseError('Channel "' . $channel . '" does not exist');
 175          }
 176          $data = array('caption' => 'Configuration (channel ' . $channel . '):');
 177          foreach ($keys as $key) {
 178              $type = $this->config->getType($key);
 179              $value = $this->config->get($key, $layer, $channel);
 180              if ($type == 'password' && $value) {
 181                  $value = '********';
 182              }
 183              if ($value === false) {
 184                  $value = 'false';
 185              } elseif ($value === true) {
 186                  $value = 'true';
 187              }
 188              $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value);
 189          }
 190          foreach ($this->config->getLayers() as $layer) {
 191              $data['data']['Config Files'][] = array(ucfirst($layer) . ' Configuration File', 'Filename' , $this->config->getConfFile($layer));
 192          }
 193  
 194          $this->ui->outputData($data, $command);
 195          return true;
 196      }
 197  
 198      // }}}
 199      // {{{ doConfigGet()
 200  
 201      function doConfigGet($command, $options, $params)
 202      {
 203          if (!is_array($params)) {
 204              $args_cnt = 0;
 205          } else {
 206              $args_cnt  = count($params);
 207          }
 208  
 209          switch ($args_cnt) {
 210              case 1:
 211                  $config_key = $params[0];
 212                  $layer = NULL;
 213                  break;
 214              case 2:
 215                  $config_key = $params[0];
 216                  $layer = $params[1];
 217                  if ($error = $this->_checkLayer($layer)) {
 218                      return $this->raiseError("config-get:$error");
 219                  }
 220                  break;
 221              case 0:
 222              default:
 223                  return $this->raiseError("config-get expects 1 or 2 parameters");
 224          }
 225  
 226          $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
 227          $reg = &$this->config->getRegistry();
 228  
 229          if (!$reg->channelExists($channel)) {
 230              return $this->raiseError('Channel "' . $channel . '" does not exist');
 231          }
 232  
 233          $this->ui->outputData($this->config->get($config_key, $layer, $channel), $command);
 234  
 235          return true;
 236      }
 237  
 238      // }}}
 239      // {{{ doConfigSet()
 240  
 241      function doConfigSet($command, $options, $params)
 242      {
 243          // $param[0] -> a parameter to set
 244          // $param[1] -> the value for the parameter
 245          // $param[2] -> the layer
 246          $failmsg = '';
 247          if (sizeof($params) < 2 || sizeof($params) > 3) {
 248              $failmsg .= "config-set expects 2 or 3 parameters";
 249              return PEAR::raiseError($failmsg);
 250          }
 251          if (isset($params[2]) && ($error = $this->_checkLayer($params[2]))) {
 252              $failmsg .= $error;
 253              return PEAR::raiseError("config-set:$failmsg");
 254          }
 255          $channel = isset($options['channel']) ? $options['channel'] :
 256              $this->config->get('default_channel');
 257          $reg = &$this->config->getRegistry();
 258          if (!$reg->channelExists($channel)) {
 259              return $this->raiseError('Channel "' . $channel . '" does not exist');
 260          }
 261          if ($params[0] == 'default_channel') {
 262              if (!$reg->channelExists($params[1])) {
 263                  return $this->raiseError('Channel "' . $params[1] . '" does not exist');
 264              }
 265          }
 266          if (count($params) == 2) {
 267              array_push($params, 'user');
 268              $layer = 'user';
 269          } else {
 270              $layer = $params[2];
 271          }
 272          array_push($params, $channel);
 273          if (!call_user_func_array(array(&$this->config, 'set'), $params))
 274          {
 275              array_pop($params);
 276              $failmsg = "config-set (" . implode(", ", $params) . ") failed, channel $channel";
 277          } else {
 278              $this->config->store($layer);
 279          }
 280          if ($failmsg) {
 281              return $this->raiseError($failmsg);
 282          }
 283          $this->ui->outputData('config-set succeeded', $command);
 284          return true;
 285      }
 286  
 287      // }}}
 288      // {{{ doConfigHelp()
 289  
 290      function doConfigHelp($command, $options, $params)
 291      {
 292          if (empty($params)) {
 293              $params = $this->config->getKeys();
 294          }
 295          $data['caption']  = "Config help" . ((count($params) == 1) ? " for $params[0]" : '');
 296          $data['headline'] = array('Name', 'Type', 'Description');
 297          $data['border']   = true;
 298          foreach ($params as $name) {
 299              $type = $this->config->getType($name);
 300              $docs = $this->config->getDocs($name);
 301              if ($type == 'set') {
 302                  $docs = rtrim($docs) . "\nValid set: " .
 303                      implode(' ', $this->config->getSetValues($name));
 304              }
 305              $data['data'][] = array($name, $type, $docs);
 306          }
 307          $this->ui->outputData($data, $command);
 308      }
 309  
 310      // }}}
 311      // {{{ doConfigCreate()
 312  
 313      function doConfigCreate($command, $options, $params)
 314      {
 315          if (count($params) != 2) {
 316              return PEAR::raiseError('config-create: must have 2 parameters, root path and ' .
 317                  'filename to save as');
 318          }
 319          $root = $params[0];
 320          // Clean up the DIRECTORY_SEPARATOR mess
 321          $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
 322          $root = preg_replace(array('!\\\\+!', '!/+!', "!$ds2+!"),
 323                               array('/', '/', '/'),
 324                              $root);
 325          if ($root{0} != '/') {
 326              if (isset($options['windows'])) {
 327                  if (!preg_match('/^[A-Za-z]:/', $root)) {
 328                      return PEAR::raiseError('Root directory must be an absolute path beginning ' .
 329                          'with "\\" or "C:\\", was: "' . $root . '"');
 330                  }
 331              } else {
 332                  return PEAR::raiseError('Root directory must be an absolute path beginning ' .
 333                      'with "/", was: "' . $root . '"');
 334              }
 335          }
 336          $windows = isset($options['windows']);
 337          if ($windows) {
 338              $root = str_replace('/', '\\', $root);
 339          }
 340          if (!file_exists($params[1])) {
 341              if (!@touch($params[1])) {
 342                  return PEAR::raiseError('Could not create "' . $params[1] . '"');
 343              }
 344          }
 345          $params[1] = realpath($params[1]);
 346          $config = &new PEAR_Config($params[1], '#no#system#config#', false, false);
 347          if ($root{strlen($root) - 1} == '/') {
 348              $root = substr($root, 0, strlen($root) - 1);
 349          }
 350          $config->noRegistry();
 351          $config->set('php_dir', $windows ? "$root\\pear\\php" : "$root/pear/php", 'user');
 352          $config->set('data_dir', $windows ? "$root\\pear\\data" : "$root/pear/data");
 353          $config->set('ext_dir', $windows ? "$root\\pear\\ext" : "$root/pear/ext");
 354          $config->set('doc_dir', $windows ? "$root\\pear\\docs" : "$root/pear/docs");
 355          $config->set('test_dir', $windows ? "$root\\pear\\tests" : "$root/pear/tests");
 356          $config->set('cache_dir', $windows ? "$root\\pear\\cache" : "$root/pear/cache");
 357          $config->set('bin_dir', $windows ? "$root\\pear" : "$root/pear");
 358          $config->writeConfigFile();
 359          $this->_showConfig($config);
 360          $this->ui->outputData('Successfully created default configuration file "' . $params[1] . '"',
 361              $command);
 362      }
 363  
 364      // }}}
 365  
 366      function _showConfig(&$config)
 367      {
 368          $params = array('user');
 369          $keys = $config->getKeys();
 370          sort($keys);
 371          $channel = 'pear.php.net';
 372          $data = array('caption' => 'Configuration (channel ' . $channel . '):');
 373          foreach ($keys as $key) {
 374              $type = $config->getType($key);
 375              $value = $config->get($key, 'user', $channel);
 376              if ($type == 'password' && $value) {
 377                  $value = '********';
 378              }
 379              if ($value === false) {
 380                  $value = 'false';
 381              } elseif ($value === true) {
 382                  $value = 'true';
 383              }
 384              $data['data'][$config->getGroup($key)][] =
 385                  array($config->getPrompt($key) , $key, $value);
 386          }
 387          foreach ($config->getLayers() as $layer) {
 388              $data['data']['Config Files'][] =
 389                  array(ucfirst($layer) . ' Configuration File', 'Filename' ,
 390                      $config->getConfFile($layer));
 391          }
 392  
 393          $this->ui->outputData($data, 'config-show');
 394          return true;
 395      }
 396      // {{{ _checkLayer()
 397  
 398      /**
 399       * Checks if a layer is defined or not
 400       *
 401       * @param string $layer The layer to search for
 402       * @return mixed False on no error or the error message
 403       */
 404      function _checkLayer($layer = null)
 405      {
 406          if (!empty($layer) && $layer != 'default') {
 407              $layers = $this->config->getLayers();
 408              if (!in_array($layer, $layers)) {
 409                  return " only the layers: \"" . implode('" or "', $layers) . "\" are supported";
 410              }
 411          }
 412          return false;
 413      }
 414  
 415      // }}}
 416  }
 417  
 418  ?>


Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7