From: Stig Bakken Date: Sat, 23 Feb 2002 15:32:36 +0000 (+0000) Subject: * started implementing new "cross-environment" command API X-Git-Tag: php-4.2.0RC1~289 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3ea5f3343b76b3e58bc4865c6d6d00f7cc54554a;p=php * started implementing new "cross-environment" command API # work in progress, not tested at all yet! --- diff --git a/pear/PEAR/Command.php b/pear/PEAR/Command.php new file mode 100644 index 0000000000..b3cede482d --- /dev/null +++ b/pear/PEAR/Command.php @@ -0,0 +1,106 @@ + | +// | | +// +----------------------------------------------------------------------+ +// +// $Id$ + +/** + * PEAR command class, a simple factory class for administrative + * commands. + * + * How to implement command classes: + * + * - The class must be called PEAR_Command_Nnn, installed in the + * "PEAR/Common" subdir, with a method called getCommands() that + * returns an array of the commands implemented by the class (see + * PEAR/Command/Install.php for an example). + * + * - The class must implement a run() function that is called with three + * params: + * + * (string) command name + * (array) assoc array with options, freely defined by each + * command, for example: + * array('force' => true) + * (array) list of the other parameters + * + * The run() function returns a PEAR_CommandResponse object. Use + * these methods to get information: + * + * int getStatus() Returns PEAR_COMMAND_(SUCCESS|FAILURE|PARTIAL) + * *_PARTIAL means that you need to issue at least + * one more command to complete the operation + * (used for example for validation steps). + * + * string getMessage() Returns a message for the user. Remember, + * no HTML or other interface-specific markup. + * + * If something unexpected happens, run() returns a PEAR error. + * + * - DON'T OUTPUT ANYTHING! Return text for output instead. + * + * - DON'T USE HTML! The text you return will be used from both Gtk, + * web and command-line interfaces, so for now keep everything to + * plain text. + * + * - DON'T USE EXIT OR DIE! Always use pear errors. From static + * 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) + { + 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(); + return $obj; + } + return PEAR::raiseError("unknown command: $command"); + } + + function registerAllCommands() + { + $dir = dirname(__FILE__) . '/Command'; + $dp = @opendir($dir); + if (empty($dp)) { + return PEAR::raiseError("PEAR_Command::registerAllCommands: opendir($dir) failed"); + } + while ($entry = readdir($dp)) { + if ($entry{0} == '.' || substr($entry, -4) != '.php' || $entry == 'Common.php') { + continue; + } + $class = "PEAR_Command_".substr($entry, 0, -4); + $file = "$dir/$entry"; + include_once $file; + $implements = call_user_func(array($class, "getCommands")); + } + } +} + +?> \ No newline at end of file diff --git a/pear/PEAR/Command/Common.php b/pear/PEAR/Command/Common.php new file mode 100644 index 0000000000..d4ed5109c1 --- /dev/null +++ b/pear/PEAR/Command/Common.php @@ -0,0 +1,38 @@ + | +// | | +// +----------------------------------------------------------------------+ +// +// $Id$ + +require_once "PEAR.php"; +require_once "PEAR/CommandResponse.php"; + +class PEAR_Command_Common extends PEAR +{ + function PEAR_Command_Common() + { + parent::PEAR(); + } + + function &makeResponse($status, $message) + { + + } +} + +?> \ No newline at end of file diff --git a/pear/PEAR/Command/Install.php b/pear/PEAR/Command/Install.php new file mode 100644 index 0000000000..c97a0c2566 --- /dev/null +++ b/pear/PEAR/Command/Install.php @@ -0,0 +1,103 @@ + | +// | | +// +----------------------------------------------------------------------+ +// +// $Id$ + +require_once "PEAR/Command/Common.php"; +require_once "PEAR/Installer.php"; + +/** + * PEAR commands for installation or deinstallation/upgrading of + * packages. + * + */ +class PEAR_Command_Install extends PEAR_Command_Common +{ + /** Stack of executing commands, to make run() re-entrant + * @var array + */ + var $command_stack; // XXX UNUSED to make run() re-entrant + + /** Currently executing command. + * @var string + */ + var $command; // XXX UNUSED + + /** + * PEAR_Command_Install constructor. Nothing to see here. + * @access public + */ + function PEAR_Command_Install() + { + parent::PEAR_Command_Common(); + } + + /** + * Return a list of all the commands defined by this class. + * @return array list of commands + * @access public + */ + function getCommands() + { + return array('install', 'uninstall', 'upgrade'); + } + + function run($command, $options, $params) + { + 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; + } + } + if ($installer->install($pkgfile, $install_options, $config)) { + print "install ok\n"; + } else { + print "install failed\n"; + } + } + } + + function getStatus( +} + +?> \ No newline at end of file diff --git a/pear/PEAR/CommandResponse.php b/pear/PEAR/CommandResponse.php new file mode 100644 index 0000000000..6e5b4c9c89 --- /dev/null +++ b/pear/PEAR/CommandResponse.php @@ -0,0 +1,124 @@ + | +// | | +// +----------------------------------------------------------------------+ +// +// $Id$ + +require_once "PEAR.php"; + +define('PEAR_COMMAND_FAILURE', 0); +define('PEAR_COMMAND_SUCCESS', 1); +define('PEAR_COMMAND_PARTIAL', 2); + +/** + * PEAR_CommandResponse is for returning an "environment-neutral" + * response to the application when a PEAR command is done. This + * means that there should be no HTML markup etc. in the message. The + * application should try rendering the message "as-is" with + * linebreaks and preferably a fixed-width font. + */ +class PEAR_CommandResponse extends PEAR +{ + /** Status code (one of the PEAR_COMMAND_* constants + * @var int + */ + var $status = null; + + /** Message for user, in plain text. + * @var string + */ + var $message = ''; + + /** Character set/encoding of $message. + * @var string + */ + var $encoding = 'ISO-8859-1'; + + /** + * Constructor. Not very exciting. + * + * @param int Command status, one of: + * PEAR_COMMAND_SUCCESS : the command was successful + * PEAR_COMMAND_FAILURE : the command failed + * PEAR_COMMAND_PARTIAL : the command was successful, + * but requires some other command to complete the + * operation (for example if the user must confirm + * something). + * @param string Message for the user. + * @param string (optional) What character encoding the message + * is in, defaults to ISO-8859-1. + * @access public + */ + function PEAR_CommandRepsonse($status, $message, $encoding = null) + { + if ($encoding !== null) { + $this->setEncoding($encoding); + } + $this->status = $status; + $this->message = $message; + } + + /** + * Get the response status code. + * @return int response status code + * @access public + */ + function getStatus() + { + return $this->status; + } + + /** + * Get the response message. + * @return string response message + * @access public + */ + function getMessage() + { + return $this->message; + } + + /** + * Get the response message charset encoding. + * @return string response message charset encoding + * @access public + */ + function getEncoding() + { + return $this->encoding; + } + + /** + * Set the response message charset encoding. + * @param string Which encoding to use, valid charsets are currently + * ISO-8859-{1-15} and UTF-8. + * @return bool true if the encoding was valid, false if not + * @access public + */ + function setEncoding($encoding) + { + if (preg_match('/^ISO-8859-([1-9]|1[1-5])$/i', $encoding) || + strcasecmp('UTF-8', $encoding) == 0) { + $this->encoding = $encoding; + return true; + } + return false; + } +} + +?> \ No newline at end of file diff --git a/pear/PEAR/Packager.php b/pear/PEAR/Packager.php index b8db4686b8..29b9ea732b 100644 --- a/pear/PEAR/Packager.php +++ b/pear/PEAR/Packager.php @@ -120,7 +120,7 @@ class PEAR_Packager extends PEAR_Common } $pwd = getcwd(); $pkgfile = basename($pkgfile); - if (isset($pkginfo['release_state']) && $pkginfo['release_state'] == 'snapshot') { + if (isset($pkginfo['release_state']) && $pkginfo['release_state'] == 'snapshot' && empty($pkginfo['version'])) { $pkginfo['version'] = date('Ymd'); } // don't want strange characters