home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / PEAR / Command.php < prev    next >
Encoding:
PHP Script  |  2005-12-02  |  12.9 KB  |  408 lines

  1. <?php
  2. /**
  3.  * PEAR_Command, command pattern class
  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-2005 The PHP Group
  18.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  19.  * @version    CVS: $Id: Command.php,v 1.35 2005/11/01 05:39:23 cellog Exp $
  20.  * @link       http://pear.php.net/package/PEAR
  21.  * @since      File available since Release 0.1
  22.  */
  23.  
  24. /**
  25.  * Needed for error handling
  26.  */
  27. require_once 'PEAR.php';
  28. require_once 'PEAR/Frontend.php';
  29. require_once 'PEAR/XMLParser.php';
  30.  
  31. /**
  32.  * List of commands and what classes they are implemented in.
  33.  * @var array command => implementing class
  34.  */
  35. $GLOBALS['_PEAR_Command_commandlist'] = array();
  36.  
  37. /**
  38.  * List of shortcuts to common commands.
  39.  * @var array shortcut => command
  40.  */
  41. $GLOBALS['_PEAR_Command_shortcuts'] = array();
  42.  
  43. /**
  44.  * Array of command objects
  45.  * @var array class => object
  46.  */
  47. $GLOBALS['_PEAR_Command_objects'] = array();
  48.  
  49. /**
  50.  * PEAR command class, a simple factory class for administrative
  51.  * commands.
  52.  *
  53.  * How to implement command classes:
  54.  *
  55.  * - The class must be called PEAR_Command_Nnn, installed in the
  56.  *   "PEAR/Common" subdir, with a method called getCommands() that
  57.  *   returns an array of the commands implemented by the class (see
  58.  *   PEAR/Command/Install.php for an example).
  59.  *
  60.  * - The class must implement a run() function that is called with three
  61.  *   params:
  62.  *
  63.  *    (string) command name
  64.  *    (array)  assoc array with options, freely defined by each
  65.  *             command, for example:
  66.  *             array('force' => true)
  67.  *    (array)  list of the other parameters
  68.  *
  69.  *   The run() function returns a PEAR_CommandResponse object.  Use
  70.  *   these methods to get information:
  71.  *
  72.  *    int getStatus()   Returns PEAR_COMMAND_(SUCCESS|FAILURE|PARTIAL)
  73.  *                      *_PARTIAL means that you need to issue at least
  74.  *                      one more command to complete the operation
  75.  *                      (used for example for validation steps).
  76.  *
  77.  *    string getMessage()  Returns a message for the user.  Remember,
  78.  *                         no HTML or other interface-specific markup.
  79.  *
  80.  *   If something unexpected happens, run() returns a PEAR error.
  81.  *
  82.  * - DON'T OUTPUT ANYTHING! Return text for output instead.
  83.  *
  84.  * - DON'T USE HTML! The text you return will be used from both Gtk,
  85.  *   web and command-line interfaces, so for now, keep everything to
  86.  *   plain text.
  87.  *
  88.  * - DON'T USE EXIT OR DIE! Always use pear errors.  From static
  89.  *   classes do PEAR::raiseError(), from other classes do
  90.  *   $this->raiseError().
  91.  * @category   pear
  92.  * @package    PEAR
  93.  * @author     Stig Bakken <ssb@php.net>
  94.  * @author     Greg Beaver <cellog@php.net>
  95.  * @copyright  1997-2005 The PHP Group
  96.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  97.  * @version    Release: 1.4.5
  98.  * @link       http://pear.php.net/package/PEAR
  99.  * @since      Class available since Release 0.1
  100.  */
  101. class PEAR_Command
  102. {
  103.     // {{{ factory()
  104.  
  105.     /**
  106.      * Get the right object for executing a command.
  107.      *
  108.      * @param string $command The name of the command
  109.      * @param object $config  Instance of PEAR_Config object
  110.      *
  111.      * @return object the command object or a PEAR error
  112.      *
  113.      * @access public
  114.      * @static
  115.      */
  116.     function &factory($command, &$config)
  117.     {
  118.         if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
  119.             PEAR_Command::registerCommands();
  120.         }
  121.         if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
  122.             $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  123.         }
  124.         if (!isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
  125.             $a = PEAR::raiseError("unknown command `$command'");
  126.             return $a;
  127.         }
  128.         $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
  129.         if (!class_exists($class)) {
  130.             require_once $GLOBALS['_PEAR_Command_objects'][$class];
  131.         }
  132.         if (!class_exists($class)) {
  133.             $a = PEAR::raiseError("unknown command `$command'");
  134.             return $a;
  135.         }
  136.         $ui =& PEAR_Command::getFrontendObject();
  137.         $obj = &new $class($ui, $config);
  138.         return $obj;
  139.     }
  140.  
  141.     // }}}
  142.     // {{{ & getObject()
  143.     function &getObject($command)
  144.     {
  145.         $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
  146.         if (!class_exists($class)) {
  147.             require_once $GLOBALS['_PEAR_Command_objects'][$class];
  148.         }
  149.         if (!class_exists($class)) {
  150.             return PEAR::raiseError("unknown command `$command'");
  151.         }
  152.         $ui =& PEAR_Command::getFrontendObject();
  153.         $config = &PEAR_Config::singleton();
  154.         $obj = &new $class($ui, $config);
  155.         return $obj;
  156.     }
  157.  
  158.     // }}}
  159.     // {{{ & getFrontendObject()
  160.  
  161.     /**
  162.      * Get instance of frontend object.
  163.      *
  164.      * @return object|PEAR_Error
  165.      * @static
  166.      */
  167.     function &getFrontendObject()
  168.     {
  169.         $a = &PEAR_Frontend::singleton();
  170.         return $a;
  171.     }
  172.  
  173.     // }}}
  174.     // {{{ & setFrontendClass()
  175.  
  176.     /**
  177.      * Load current frontend class.
  178.      *
  179.      * @param string $uiclass Name of class implementing the frontend
  180.      *
  181.      * @return object the frontend object, or a PEAR error
  182.      * @static
  183.      */
  184.     function &setFrontendClass($uiclass)
  185.     {
  186.         $a = &PEAR_Frontend::setFrontendClass($uiclass);
  187.         return $a;
  188.     }
  189.  
  190.     // }}}
  191.     // {{{ setFrontendType()
  192.  
  193.     /**
  194.      * Set current frontend.
  195.      *
  196.      * @param string $uitype Name of the frontend type (for example "CLI")
  197.      *
  198.      * @return object the frontend object, or a PEAR error
  199.      * @static
  200.      */
  201.     function setFrontendType($uitype)
  202.     {
  203.         $uiclass = 'PEAR_Frontend_' . $uitype;
  204.         return PEAR_Command::setFrontendClass($uiclass);
  205.     }
  206.  
  207.     // }}}
  208.     // {{{ registerCommands()
  209.  
  210.     /**
  211.      * Scan through the Command directory looking for classes
  212.      * and see what commands they implement.
  213.      *
  214.      * @param bool   (optional) if FALSE (default), the new list of
  215.      *               commands should replace the current one.  If TRUE,
  216.      *               new entries will be merged with old.
  217.      *
  218.      * @param string (optional) where (what directory) to look for
  219.      *               classes, defaults to the Command subdirectory of
  220.      *               the directory from where this file (__FILE__) is
  221.      *               included.
  222.      *
  223.      * @return bool TRUE on success, a PEAR error on failure
  224.      *
  225.      * @access public
  226.      * @static
  227.      */
  228.     function registerCommands($merge = false, $dir = null)
  229.     {
  230.         $parser = new PEAR_XMLParser;
  231.         if ($dir === null) {
  232.             $dir = dirname(__FILE__) . '/Command';
  233.         }
  234.         $dp = @opendir($dir);
  235.         if (empty($dp)) {
  236.             return PEAR::raiseError("registerCommands: opendir($dir) failed");
  237.         }
  238.         if (!$merge) {
  239.             $GLOBALS['_PEAR_Command_commandlist'] = array();
  240.         }
  241.         while ($entry = readdir($dp)) {
  242.             if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
  243.                 continue;
  244.             }
  245.             $class = "PEAR_Command_".substr($entry, 0, -4);
  246.             $file = "$dir/$entry";
  247.             $parser->parse(file_get_contents($file));
  248.             $implements = $parser->getData();
  249.             // List of commands
  250.             if (empty($GLOBALS['_PEAR_Command_objects'][$class])) {
  251.                 $GLOBALS['_PEAR_Command_objects'][$class] = "$dir/" . substr($entry, 0, -4) .
  252.                     '.php';
  253.             }
  254.             foreach ($implements as $command => $desc) {
  255.                 if ($command == 'attribs') {
  256.                     continue;
  257.                 }
  258.                 if (isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
  259.                     return PEAR::raiseError('Command "' . $command . '" already registered in ' .
  260.                         'class "' . $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
  261.                 }
  262.                 $GLOBALS['_PEAR_Command_commandlist'][$command] = $class;
  263.                 $GLOBALS['_PEAR_Command_commanddesc'][$command] = $desc['summary'];
  264.                 if (isset($desc['shortcut'])) {
  265.                     $shortcut = $desc['shortcut'];
  266.                     if (isset($GLOBALS['_PEAR_Command_shortcuts'][$shortcut])) {
  267.                         return PEAR::raiseError('Command shortcut "' . $shortcut . '" already ' .
  268.                             'registered to command "' . $command . '" in class "' .
  269.                             $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
  270.                     }
  271.                     $GLOBALS['_PEAR_Command_shortcuts'][$shortcut] = $command;
  272.                 }
  273.                 if (isset($desc['options']) && $desc['options']) {
  274.                     foreach ($desc['options'] as $oname => $option) {
  275.                         if (isset($option['shortopt']) && strlen($option['shortopt']) > 1) {
  276.                             return PEAR::raiseError('Option "' . $oname . '" short option "' .
  277.                                 $option['shortopt'] . '" must be ' .
  278.                                 'only 1 character in Command "' . $command . '" in class "' .
  279.                                 $class . '"');
  280.                         }
  281.                     }
  282.                 }
  283.             }
  284.         }
  285.         ksort($GLOBALS['_PEAR_Command_shortcuts']);
  286.         ksort($GLOBALS['_PEAR_Command_commandlist']);
  287.         @closedir($dp);
  288.         return true;
  289.     }
  290.  
  291.     // }}}
  292.     // {{{ getCommands()
  293.  
  294.     /**
  295.      * Get the list of currently supported commands, and what
  296.      * classes implement them.
  297.      *
  298.      * @return array command => implementing class
  299.      *
  300.      * @access public
  301.      * @static
  302.      */
  303.     function getCommands()
  304.     {
  305.         if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
  306.             PEAR_Command::registerCommands();
  307.         }
  308.         return $GLOBALS['_PEAR_Command_commandlist'];
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ getShortcuts()
  313.  
  314.     /**
  315.      * Get the list of command shortcuts.
  316.      *
  317.      * @return array shortcut => command
  318.      *
  319.      * @access public
  320.      * @static
  321.      */
  322.     function getShortcuts()
  323.     {
  324.         if (empty($GLOBALS['_PEAR_Command_shortcuts'])) {
  325.             PEAR_Command::registerCommands();
  326.         }
  327.         return $GLOBALS['_PEAR_Command_shortcuts'];
  328.     }
  329.  
  330.     // }}}
  331.     // {{{ getGetoptArgs()
  332.  
  333.     /**
  334.      * Compiles arguments for getopt.
  335.      *
  336.      * @param string $command     command to get optstring for
  337.      * @param string $short_args  (reference) short getopt format
  338.      * @param array  $long_args   (reference) long getopt format
  339.      *
  340.      * @return void
  341.      *
  342.      * @access public
  343.      * @static
  344.      */
  345.     function getGetoptArgs($command, &$short_args, &$long_args)
  346.     {
  347.         if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
  348.             PEAR_Command::registerCommands();
  349.         }
  350.         if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
  351.             $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  352.         }
  353.         if (!isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
  354.             return null;
  355.         }
  356.         $obj = &PEAR_Command::getObject($command);
  357.         return $obj->getGetoptArgs($command, $short_args, $long_args);
  358.     }
  359.  
  360.     // }}}
  361.     // {{{ getDescription()
  362.  
  363.     /**
  364.      * Get description for a command.
  365.      *
  366.      * @param  string $command Name of the command
  367.      *
  368.      * @return string command description
  369.      *
  370.      * @access public
  371.      * @static
  372.      */
  373.     function getDescription($command)
  374.     {
  375.         if (!isset($GLOBALS['_PEAR_Command_commanddesc'][$command])) {
  376.             return null;
  377.         }
  378.         return $GLOBALS['_PEAR_Command_commanddesc'][$command];
  379.     }
  380.  
  381.     // }}}
  382.     // {{{ getHelp()
  383.  
  384.     /**
  385.      * Get help for command.
  386.      *
  387.      * @param string $command Name of the command to return help for
  388.      *
  389.      * @access public
  390.      * @static
  391.      */
  392.     function getHelp($command)
  393.     {
  394.         $cmds = PEAR_Command::getCommands();
  395.         if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
  396.             $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  397.         }
  398.         if (isset($cmds[$command])) {
  399.             $obj = &PEAR_Command::getObject($command);
  400.             return $obj->getHelp($command);
  401.         }
  402.         return false;
  403.     }
  404.     // }}}
  405. }
  406.  
  407. ?>
  408.