]> granicus.if.org Git - php/commitdiff
* PEAR_Config class to maintain system and per-user configuration for
authorStig Bakken <ssb@php.net>
Fri, 26 Oct 2001 10:07:43 +0000 (10:07 +0000)
committerStig Bakken <ssb@php.net>
Fri, 26 Oct 2001 10:07:43 +0000 (10:07 +0000)
  PEAR installations
* PEAR_Remote class to communicate with the backend server (xmlrpc)
* change pear script's option parsing to "-d foo=bar" style
* added -c/-C (user/system config file) and -s/-S (store user/system config)
  options

pear/Makefile.in
pear/PEAR/Config.php [new file with mode: 0644]
pear/PEAR/Remote.php [new file with mode: 0644]
pear/scripts/pear.in
pear/scripts/pearwin.php

index 5ce5e3aa0a3af39ff6484f185851d0d7f534425b..9d8cf3620890b5100e0524046dd29e06afa4e44f 100644 (file)
@@ -100,6 +100,7 @@ PEAR_FILES = \
        Numbers/Roman.php \
        PEAR.php \
        PEAR/Common.php \
+       PEAR/Config.php \
        PEAR/Installer.php \
        PEAR/Packager.php \
        PEAR/Uploader.php \
diff --git a/pear/PEAR/Config.php b/pear/PEAR/Config.php
new file mode 100644 (file)
index 0000000..6c36e9b
--- /dev/null
@@ -0,0 +1,213 @@
+<?php
+//
+// +----------------------------------------------------------------------+
+// | PHP version 4.0                                                      |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997-2001 The PHP Group                                |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 2.02 of the PHP license,      |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available at through the world-wide-web at                           |
+// | http://www.php.net/license/2_02.txt.                                 |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Authors: Stig Bakken <ssb@fast.no>                                   |
+// |                                                                      |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once 'PEAR.php';
+
+/**
+ * This is a class for storing simple configuration values keeping
+ * track of which are system-defined (defaulted) and which are
+ * user-defined.  By default, only used-defined settings are stored
+ * back to the user's configuration file.
+ *
+ * Configuration member is a simple associative array.  Used keys:
+ *
+ *  master_server      which server to query for mirror lists etc.
+ *  server             which server/mirror we're currently using
+ *  username           PEAR username
+ *  password           PEAR password (stored base64-encoded)
+ *  php_dir            Where to install .php files
+ *  ext_dir            Directory to install compiled libs in
+ *  doc_dir            Directory to install documentation in
+ *
+ */
+class PEAR_Config extends PEAR
+{
+    // {{{ properties
+
+    var $defaults_file = '';
+
+    var $config_file = '';
+
+    var $configuration = array();
+
+    var $defaulted = array();
+
+    // }}}
+
+    // {{{ PEAR_Config([file], [defaults_file])
+
+    function PEAR_Config($file = '', $defaults_file = '')
+    {
+        $this->PEAR();
+        $this->config_file = $file;
+        $this->defaults_file = $defaults_file;
+        if ($file && file_exists($file)) {
+            $this->readConfigFile($file);
+        }
+        if ($defaults_file && file_exists($defaults_file)) {
+            $this->mergeConfigFile($defaults_file, false, true);
+        }
+    }
+
+    // }}}
+
+    // {{{ readConfigFile([file], [defaults])
+
+    function readConfigFile($file = null, $defaults = false)
+    {
+        if ($file === null) {
+            $file = $this->config_file;
+        }
+        $fp = @fopen($file, "r");
+        if (!$fp) {
+            return $this->raiseError($php_errormsg);
+        }
+        $contents = fread($fp, filesize($file));
+        $data = unserialize($contents);
+        if ($data === false) {
+            return $this->raiseError("PEAR_Config::readConfigFile: bad data");
+        }
+        $this->configuration = $data;
+        if ($defaults) {
+            foreach ($data as $key => $value) {
+                $this->defaulted[$key] = true;
+            }
+        }
+    }
+
+    // }}}
+    // {{{ mergeConfigFile(file, [override], [defaults])
+
+    function mergeConfigFile($file, $override = true, $defaults = false)
+    {
+        $fp = @fopen($file, "r");
+        if (!$fp) {
+            return $this->raiseError($php_errormsg);
+        }
+        $contents = fread($fp, filesize($file));
+        $data = unserialize($contents);
+        if ($data === false) {
+            return $this->raiseError("PEAR_Config::mergeConfigFile: bad data");
+        }
+        foreach ($data as $key => $value) {
+            if (isset($this->configuration[$key]) && !$override) {
+                continue;
+            }
+            if ($defaults) {
+                $this->defaulted[$key] = true;
+            }
+            $this->configuration[$key] = $value;
+        }
+    }
+
+    // }}}
+    // {{{ writeConfigFile([file], [what_keys])
+
+    function writeConfigFile($file = null, $what_keys = 'userdefined')
+    {
+        print "storing $what_keys keys\n";
+        if ($what_keys == 'both') {
+            $this->writeConfigFile($file, 'userdefined');
+            $this->writeConfigFile($file, 'default');
+        }
+        if ($file === null) {
+            if ($what_keys == 'default') {
+                $file = $this->defaults_file;
+            } else {
+                $file = $this->config_file;
+            }
+        }
+        if ($what_keys == 'default') {
+            $keys_to_store = array_intersect(array_keys($this->configuration),
+                                             array_keys($this->defaulted));
+        } elseif ($what_keys == 'all') {
+            $keys_to_store = array_keys($this->configuration);
+        } else { // user-defined keys
+            $keys_to_store = array_diff(array_keys($this->configuration),
+                                        array_keys($this->defaulted));
+        }
+        $data = array();
+        foreach ($keys_to_store as $key) {
+            $data[$key] = $this->configuration[$key];
+        }
+        $fp = @fopen($file, "w");
+        if (!$fp) {
+            return $this->raiseError("PEAR_Config::writeConfigFile fopen failed");
+        }
+        if (!@fwrite($fp, serialize($data))) {
+            return $this->raiseError("PEAR_Config::writeConfigFile serialize failed");
+        }
+        return true;
+    }
+
+    // }}}
+    // {{{ get(key)
+
+    function get($key)
+    {
+        return @$this->configuration[$key];
+    }
+
+    // }}}
+    // {{{ set(key, value, [default])
+
+    function set($key, $value, $default = false)
+    {
+        $this->configuration[$key] = $value;
+        if ($default) {
+            $this->defaulted[$key] = true;
+        } elseif (isset($this->defaulted[$key])) {
+            unset($this->defaulted[$key]);
+        }
+    }
+
+    // }}}
+    // {{{ toDefault(key)
+
+    function toDefault($key)
+    {
+        if (file_exists($this->defaults_file)) {
+            // re-reads the defaults file each time, but hey it works
+            unset($this->configuration[$key]);
+            $this->mergeConfigFile($this->defaults_file, false, true);
+        }
+    }
+
+    // }}}
+    // {{{ isDefaulted(key)
+
+    function isDefaulted($key)
+    {
+        return isset($this->defaulted[$key]);
+    }
+
+    // }}}
+    // {{{ isDefined(key)
+
+    function isDefined($key)
+    {
+        return isset($this->configuration[$key]);
+    }
+
+    // }}}
+}
+
+?>
\ No newline at end of file
diff --git a/pear/PEAR/Remote.php b/pear/PEAR/Remote.php
new file mode 100644 (file)
index 0000000..b813215
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+//
+// +----------------------------------------------------------------------+
+// | PHP version 4.0                                                      |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997-2001 The PHP Group                                |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 2.02 of the PHP license,      |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available at through the world-wide-web at                           |
+// | http://www.php.net/license/2_02.txt.                                 |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Authors: Stig Bakken <ssb@fast.no>                                   |
+// |                                                                      |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once 'PEAR.php';
+
+/**
+ * This is a class for doing remote operations against the central
+ * PEAR database.
+ */
+class PEAR_Remote extends PEAR
+{
+    // {{{ properties
+
+    var $config_object = null;
+
+    // }}}
+
+    // {{{ PEAR_Remote(config_object)
+
+    function PEAR_Remote($config_object)
+    {
+        $this->PEAR();
+        $this->config_object = $config_object;
+    }
+
+    // }}}
+
+    // {{{ call(method, [args...])
+
+    function call($method)
+    {
+        if (!extension_loaded("xmlrpc")) {
+            return $this->raiseError("xmlrpc support not loaded");
+        }
+        $params = array_slice(func_get_args(), 1);
+        $request = xmlrpc_encode_request($method, $params);
+        print "<p><b>Request:</b><p>\n";
+        print "<pre>";
+        print htmlspecialchars($request);
+        print "</pre>";
+        $fp = @fsockopen($config->get("master_server"), 80);
+        if (!$fp) {
+            return $this->raiseError("PEAR_Remote::call: connect failed");
+        }
+        $len = strlen($request);
+        fwrite($fp, ("POST /xmlrpc.php HTTP/1.0\r\n".
+                     "Content-type: text/xml\r\n".
+                     "Content-length: $len\r\n".
+                     "\r\n$request"));
+        print "Response:\n";
+        while ($line = fgets($fp, 2048)) {
+            print $line;
+        }
+        // XXX UNFINISHED
+    }
+
+    // }}}
+}
+
+?>
\ No newline at end of file
index 08adc72573db15206bd072702a1eaf87372d00aa..c5f7ec642bf0459bc8067fd443b174c62c90373c 100644 (file)
 // +----------------------------------------------------------------------+
 //
 require_once 'PEAR.php';
+require_once 'PEAR/Config.php';
 require_once 'Console/Getopt.php';
 
 error_reporting(E_ALL ^ E_NOTICE);
 
-$options = Console_Getopt::getopt($argv, "h?v:e:p:d:");
+// {{{ config file and option parsing
+
+$options = Console_Getopt::getopt($argv, "c:C:d:D:h?sS");
 if (PEAR::isError($options)) {
     usage($options);
 }
 
+if (OS_WINDOWS) {
+    $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pearsys.ini';
+    $pear_user_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.ini';
+} else {
+    $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.conf';
+    $pear_user_config = $HTTP_ENV_VARS['HOME'].DIRECTORY_SEPARATOR.'.pearrc';
+}
+
 $opts = $options[0];
+
+foreach ($opts as $opt) {
+    switch ($opt[0]) {
+        case 'c':
+            $pear_user_config = $opt[1];
+            break;
+        case 'C':
+            $pear_default_config = $opt[1];
+            break;
+    }
+}
+
+$config = new PEAR_Config($pear_user_config, $pear_default_config);
+$store_user_config = false;
+$store_default_config = false;
+
 foreach ($opts as $opt) {
     $param = $opt[1];
     switch ($opt[0]) {
-        case 'v':
-            $verbose = $param;
+        case 'd':
+            list($key, $value) = explode('=', $param);
+            $config->set($key, $value);
             break;
-        case 'e':
-            if ($param{0} != DIRECTORY_SEPARATOR) {
-                usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n"));
-            }
-            $ext_dir = $param;
+        case 'D':
+            list($key, $value) = explode('=', $param);
+            $config->set($key, $value, true);
             break;
-        case 'p':
-            if ($param{0} != DIRECTORY_SEPARATOR) {
-                usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n"));
-            }
-            $script_dir = $param;
+        case 's':
+            $store_user_config = true;
             break;
-        case 'd':
-            if ($param{0} != DIRECTORY_SEPARATOR) {
-                usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n"));
-            }
-            $doc_dir = $param;
+        case 'S':
+            $store_default_config = true;
             break;
     }
 }
 
-$verbose    = (isset($verbose)) ? $verbose : 1;
-$script_dir = (isset($script_dir)) ? $script_dir : PEAR_INSTALL_DIR;
-$ext_dir    = (isset($ext_dir)) ? $ext_dir : PEAR_EXTENSION_DIR;
-$doc_dir    = (isset($doc_dir)) ? $doc_dir : '';
+$fallback_config = array(
+    'php_dir' => PEAR_INSTALL_DIR,
+    'ext_dir' => PEAR_EXTENSION_DIR,
+    'doc_dir' => '',
+    'verbose' => true,
+);
+
+foreach ($fallback_config as $key => $value) {
+    if (!$config->isDefined($key)) {
+        $config->set($key, $value);
+    }
+}
+
+$verbose    = $config->get("verbose");
+$script_dir = $config->get("php_dir");
+$ext_dir    = $config->get("ext_dir");
+$doc_dir    = $config->get("doc_dir");
+
+// }}}
+
+PEAR::setErrorHandling(PEAR_ERROR_PRINT, "pear: %s\n");
 
-PEAR::setErrorHandling(PEAR_ERROR_PRINT);
 $command = $options[1][1];
 switch ($command) {
-    case 'install':
+    // {{{ install
+
+    case 'install': {
         include_once 'PEAR/Installer.php';
         $package = $options[1][2];
         $installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
@@ -75,7 +113,12 @@ switch ($command) {
             print "install ok\n";
         }
         break;
-    case 'package':
+    }
+
+    // }}}
+    // {{{ package
+
+    case 'package': {
         include_once 'PEAR/Packager.php';
         $pkginfofile = $options[1][2];
         $packager =& new PEAR_Packager($script_dir, $ext_dir, $doc_dir);
@@ -86,19 +129,37 @@ switch ($command) {
             print "package ok\n";
         }
         break;
-    default:
-        usage();
+    }
+
+    // }}}
+    // {{{ list-packages
+
+    case 'list-packages': {
+        include_once 'PEAR/Remote.php';
+        $remote = new PEAR_Remote($config);
+    }
+
+    // }}}
+    default: {
+        if (!$store_default_config && !$store_user_config) {
+            usage();
+        }
         break;
+    }
 }
 
-function usage($obj = null)
+// {{{ usage()
+
+function usage($error = null)
 {
     $stderr = fopen('php://stderr', 'w');
-    if ($obj !== null) {
-        fputs($stderr, $obj->getMessage());
+    if (PEAR::isError($obj)) {
+        fputs($stderr, $error->getMessage());
+    } elseif ($obj !== null) {
+        fputs($stderr, $error);
     }
     fputs($stderr,
-          "Usage: pear [-v n] [-h] [-p <dir>] [-e <dir>] [-d <dir>] command <parameters>\n".
+          "Usage: pear [options] command <parameters>\n".
           "Options:\n".
           "     -v        set verbosity level to <n> (0-2, default 1)\n".
           "     -p <dir>  set script install dir (absolute path)\n".
@@ -113,4 +174,6 @@ function usage($obj = null)
     exit;
 }
 
+// }}}
+
 ?>
index 2d75b3766aaa2262cd2a17211c631e39938c44fe..2e56d4f77f8a548732d669d98263f412051677db 100644 (file)
@@ -38,19 +38,19 @@ foreach ($opts as $opt) {
             break;
         case 'e':
             if ($param{0} != getenv('DIRECTORY_SEPARATOR')) {
-                usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n"));
+                usage (new PEAR_Error("no absolute path (eg. /usr/lib/php)\n"));
             }
             $ext_dir = $param;
             break;
         case 'p':
             if ($param{0} != getenv('DIRECTORY_SEPARATOR')) {
-                usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n"));
+                usage (new PEAR_Error("no absolute path (eg. /usr/lib/php)\n"));
             }
             $script_dir = $param;
             break;
         case 'd':
             if ($param{0} != getenv('DIRECTORY_SEPARATOR')) {
-                usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n"));
+                usage (new PEAR_Error("no absolute path (eg. /usr/lib/php)\n"));
             }
             $doc_dir = $param;
             break;