]> granicus.if.org Git - php/commitdiff
* code for install/uninstall/upgrade complete, not yet tested
authorStig Bakken <ssb@php.net>
Fri, 1 Mar 2002 09:43:18 +0000 (09:43 +0000)
committerStig Bakken <ssb@php.net>
Fri, 1 Mar 2002 09:43:18 +0000 (09:43 +0000)
# uses a new, un-committed version of PEAR_Config

pear/PEAR/Command.php
pear/PEAR/Command/Common.php
pear/PEAR/Command/Install.php

index 260eb91b171718b4caeb758f302b54e710af5364..49807d0339bfc0ed0e95236407dd45febbc9c05b 100644 (file)
 //
 // $Id$
 
+
+require_once "PEAR.php";
+
+/**
+ * List of commands and what classes they are implemented in.
+ * @var array command => implementing class
+ */
+$GLOBALS['_PEAR_Command_commandlist'] = array();
+
 /**
  * PEAR command class, a simple factory class for administrative
  * commands.
  *   classes do PEAR::raiseError(), from other classes do
  *   $this->raiseError().
  */
-
-require_once "PEAR.php";
-
-$GLOBALS['_PEAR_Command_commandlist'] = array();
-
 class PEAR_Command
 {
-    function factory($command)
+    /**
+     * Get the right object for executing a command.
+     *
+     * @param object Instance of PEAR_Config object
+     * @param string The name of the command
+     *
+     * @return object the command object or a PEAR error
+     *
+     * @access public
+     */
+    function factory(&$config, $command)
     {
-        static $command_classes = array(
-            'install'   => 'PEAR_Command_Install':
-            'uninstall' => 'PEAR_Command_Install':
-            'upgrade'   => 'PEAR_Command_Install':
-            );
-        if (isset($command_classes[$command])) {
-            $class = $command_classes[$command];
-            $obj =& new $class();
+        if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
+            PEAR_Command::registerCommands();
+        }
+        if (isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
+            $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
+            $obj =& new $class($config);
             return $obj;
         }
         return PEAR::raiseError("unknown command: $command");
     }
 
-    function registerAllCommands()
+    /**
+     * Scan through the Command directory looking for classes
+     * and see what commands they implement.
+     *
+     * @param bool   (optional) if FALSE (default), the new list of
+     *               commands should replace the current one.  If TRUE,
+     *               new entries will be merged with old.
+     *
+     * @param string (optional) where (what directory) to look for
+     *               classes, defaults to the Command subdirectory of
+     *               the directory from where this file (__FILE__) is
+     *               included.
+     *
+     * @return bool TRUE on success, a PEAR error on failure
+     *
+     * @access public
+     */
+    function registerCommands($merge = false, $dir = null)
     {
-        $dir = dirname(__FILE__) . '/Command';
+        if ($dir === null) {
+            $dir = dirname(__FILE__) . '/Command';
+        }
         $dp = @opendir($dir);
         if (empty($dp)) {
-            return PEAR::raiseError("PEAR_Command::registerAllCommands: opendir($dir) failed");
+            return PEAR::raiseError("PEAR_Command::registerCommands: opendir($dir) failed");
+        }
+        if (!$merge) {
+            $GLOBALS['_PEAR_Command_commandlist'] = array();
         }
         while ($entry = readdir($dp)) {
             if ($entry{0} == '.' || substr($entry, -4) != '.php' || $entry == 'Common.php') {
@@ -98,7 +132,24 @@ class PEAR_Command
             $file = "$dir/$entry";
             include_once $file;
             $implements = call_user_func(array($class, "getCommands"));
+            foreach ($implements as $command) {
+                $GLOBALS['_PEAR_Command_commandlist'][$command] = $class;
+            }
         }
+        return true;
+    }
+
+    /**
+     * Get the list of currently supported commands, and what
+     * classes implement them.
+     *
+     * @return array command => implementing class
+     *
+     * @access public
+     */
+    function getCommands()
+    {
+        return $GLOBALS['_PEAR_Command_commandlist'];
     }
 }
 
index d5c57be9ecc771a34e6aa4f70fb785c6c12dd89d..e54c1694f13cc21f01720b8ca9e38a1b0da3bb17 100644 (file)
@@ -23,15 +23,46 @@ require_once "PEAR/CommandResponse.php";
 
 class PEAR_Command_Common extends PEAR
 {
+    /**
+     * PEAR_Config object used to pass user system and configuration
+     * on when executing commands
+     *
+     * @var object
+     */
+    var $config;
+
+    /**
+     * PEAR_Command_Common constructor.
+     *
+     * @access public
+     */
     function PEAR_Command_Common()
     {
         parent::PEAR();
+        $this->config = PEAR_Config::singleton();
     }
 
-    function &makeResponse($status, $message)
+    /**
+     * Return a PEAR_CommandResponse object with parameters
+     * filled in.
+     *
+     * @param int     status code
+     * @param string  message text
+     * @param string  (optional) message character encoding
+     *
+     * @return object a PEAR_CommandResponse object
+     *
+     * @access public
+     *
+     * @see PEAR_CommandResponse
+     */
+    function &makeResponse($status, $message, $encoding = null)
     {
-        
+        $obj =& new PEAR_CommandResponse($status, $message, $encoding);
+        return $obj;
     }
+
+    
 }
 
 ?>
\ No newline at end of file
index a40486d07ca46910fd49381769489323b0143ea2..0d99f85b1afbf64cabbdc32cf20f5d642026a1c1 100644 (file)
@@ -39,7 +39,8 @@ class PEAR_Command_Install extends PEAR_Command_Common
     var $command; // XXX UNUSED
 
     /**
-     * PEAR_Command_Install constructor.  Nothing to see here.
+     * PEAR_Command_Install constructor.
+     *
      * @access public
      */
     function PEAR_Command_Install()
@@ -59,44 +60,40 @@ class PEAR_Command_Install extends PEAR_Command_Common
 
     function run($command, $options, $params)
     {
+        $installer =& new PEAR_Installer($options['php_dir'],
+                                         $options['ext_dir'],
+                                         $options['doc_dir']);
+        $installer->debug = @$options['verbose'];
+        $status = PEAR_COMMAND_SUCCESS;
+        ob_start();
         switch ($command) {
-        }
-    }
-
-    function doInstall($command, $options, $params)
-    {
-            $pkgfile = $cmdargs[0];
-            $installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
-            $installer->setErrorHandling(PEAR_ERROR_DIE,
-                                         basename($pkgfile) . ": %s\n");
-            $installer->debug = $verbose;
-            $install_options = array();
-            if ($command == 'upgrade') {
-                $install_options['upgrade'] = true;
-            }
-            foreach ($cmdopts as $opt) {
-                switch ($opt[0]) {
-                    case 'r':
-                        // This option is for use by rpm and other package
-                        // tools that can install files etc. by itself, but
-                        // still needs to register the package as installed in
-                        // PEAR's local registry.
-                        $install_options['register_only'] = true;
-                    break;
-                    case 'f':
-                        $install_options['force'] = true;
-                    break;
+            case 'install':
+            case 'upgrade': {
+                if ($command == 'upgrade') {
+                    $options['upgrade'] = true;
                 }
+                if ($installer->install($params[0], $options, $this->config)) {
+                    print "install ok\n";
+                } else {
+                    print "install failed\n";
+                    $status = PEAR_COMMAND_FAILURE;
+                }
+                break;
             }
-            if ($installer->install($pkgfile, $install_options, $config)) {
-                print "install ok\n";
-            } else {
-                print "install failed\n";
-            }
+            case 'uninstall': {
+                if ($installer->uninstall($params[0], $uninstall_options)) {
+                    print "uninstall ok\n";
+                } else {
+                    print "uninstall failed\n";
+                    $status = PEAR_COMMAND_FAILURE;
+                }
+                break;
+            }                
         }
+        $output = ob_get_contents();
+        ob_end_clean();
+        return $this->makeResponse($status, $output);
     }
-
-    function getStatus(
 }
 
 ?>
\ No newline at end of file