From: Stig Bakken Date: Fri, 1 Mar 2002 09:43:18 +0000 (+0000) Subject: * code for install/uninstall/upgrade complete, not yet tested X-Git-Tag: php-4.2.0RC1~226 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8e0e7cd4ba4ff9cca4b88ccb4e7dc2c3edc827df;p=php * code for install/uninstall/upgrade complete, not yet tested # uses a new, un-committed version of PEAR_Config --- diff --git a/pear/PEAR/Command.php b/pear/PEAR/Command.php index 260eb91b17..49807d0339 100644 --- a/pear/PEAR/Command.php +++ b/pear/PEAR/Command.php @@ -18,6 +18,15 @@ // // $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. @@ -61,34 +70,59 @@ * 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']; } } diff --git a/pear/PEAR/Command/Common.php b/pear/PEAR/Command/Common.php index d5c57be9ec..e54c1694f1 100644 --- a/pear/PEAR/Command/Common.php +++ b/pear/PEAR/Command/Common.php @@ -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 diff --git a/pear/PEAR/Command/Install.php b/pear/PEAR/Command/Install.php index a40486d07c..0d99f85b1a 100644 --- a/pear/PEAR/Command/Install.php +++ b/pear/PEAR/Command/Install.php @@ -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