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 / CodeGen / Command.php next >
Encoding:
PHP Script  |  2006-04-07  |  7.2 KB  |  246 lines

  1. <?php
  2. /**
  3.  * Command wrapper class 
  4.  *
  5.  * PHP versions 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   Tools and Utilities
  14.  * @package    CodeGen
  15.  * @author     Hartmut Holzgraefe <hartmut@php.net>
  16.  * @copyright  2005 Hartmut Holzgraefe
  17.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  18.  * @version    CVS: $Id: Command.php,v 1.6 2006/02/15 15:25:09 hholzgra Exp $
  19.  * @link       http://pear.php.net/package/CodeGen
  20.  */
  21.  
  22. /**
  23.  * includes
  24.  */
  25. require_once 'PEAR.php';
  26.  
  27. require_once "CodeGen/Tools/Getopt.php";
  28. require_once "CodeGen/Extension.php";
  29.  
  30. /**
  31.  * Command wrapper class
  32.  *
  33.  * This class wraps up the functionality needed for the 
  34.  * command line script. 
  35.  *
  36.  * @category   Tools and Utilities
  37.  * @package    CodeGen
  38.  * @author     Hartmut Holzgraefe <hartmut@php.net>
  39.  * @copyright  2005 Hartmut Holzgraefe
  40.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  41.  * @version    Release: @package_version@
  42.  * @link       http://pear.php.net/package/CodeGen
  43.  */
  44. class CodeGen_Command
  45. {
  46.     /**
  47.      * The extension the command class is going to work on
  48.      *
  49.      * @var object
  50.      */
  51.     protected $extension;
  52.  
  53.     /**
  54.      * Command constructor
  55.      *
  56.      * @param object  Extension to work on
  57.      */
  58.     function __construct(CodeGen_Extension $extension)
  59.     {
  60.         $this->extension = $extension;
  61.  
  62.         // no compromise
  63.         error_reporting(E_ALL);
  64.  
  65.         // but make sure errors are written to stderr so they can't be collected by ob_* functions
  66.         set_error_handler(array($this, "errorHandler"));
  67.  
  68.         list($shortOptions, $longOptions) = $this->commandOptions();
  69.  
  70.         $this->options = new CodeGen_Tools_Getopt($shortOptions, 
  71.                                                    $longOptions,
  72.                                                    array($this, "showUsage"));
  73.  
  74.         if ($this->options->have("help", "h")) {
  75.             $this->showVersion();
  76.             $this->showUsage();
  77.             exit(0);
  78.         }
  79.         
  80.         if ($this->options->have("version")) {
  81.             $this->showVersion();
  82.             exit(0);
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Define the available command line options
  88.      *
  89.      * @return array  the available short and long options
  90.      */
  91.     function commandOptions()
  92.     {
  93.         $shortOptions = "fd=hlqx";
  94.  
  95.         $longOptions = array( "help",
  96.                               "dir=",
  97.                               "experimental",
  98.                               "force",
  99.                               "lint",
  100.                               "quiet", 
  101.                               "version", 
  102.                               );
  103.  
  104.         return array($shortOptions, $longOptions);
  105.     }
  106.  
  107.     /**
  108.      * Show copyright and version info taken from extension class
  109.      *
  110.      */
  111.     function showVersion() 
  112.     {
  113.         $fp = fopen("php://stderr", "w");
  114.         fputs($fp, basename($_SERVER["argv"][0]) . " ". $this->extension->version() . "," . $this->extension->copyright() . "\n");
  115.         fclose($fp);
  116.     }
  117.  
  118.     
  119.     /**
  120.      * Show usage/help message
  121.      *
  122.      * @param string  optional error message to display
  123.      */
  124.     function showUsage($message = false)
  125.     {
  126.         $fp = fopen("php://stderr", "w");
  127.         
  128.         if ($message) fputs($fp, "$message\n\n");
  129.         
  130.         fputs($fp, "Usage:
  131.  
  132. ". $_SERVER["argv"][0] ." [-hxf] [-d dir] [--version] specfile.xml
  133.  
  134.   -h|--help          this message
  135.   -x|--experimental  enable experimental features
  136.   -d|--dir           output directory
  137.   -f|--force         overwrite existing files/directories
  138.   -l|--lint          check syntax only, don't create output
  139.   --version          show version info
  140. ");
  141.  
  142.         fclose($fp);
  143.     }
  144.  
  145.     /**
  146.      * Show error message and bailout
  147.      *
  148.      * @param string  error message
  149.      */
  150.     function terminate($msg)
  151.     {
  152.         $stderr = fopen("php://stderr", "w");
  153.         if ($stderr) {
  154.             fprintf($stderr, "%s\n", $msg);
  155.             fclose($stderr);
  156.         } else {
  157.             echo "$msg\n";
  158.         }
  159.         exit(3);
  160.     }
  161.  
  162.     /**
  163.      * Error handler callback
  164.      *
  165.      * @param int     error level number
  166.      * @param string  error message
  167.      * @param string  source file
  168.      * @param int     source line
  169.      */
  170.     function errorHandler($errno, $errstr, $errfile, $errline) 
  171.     {
  172.         if ($errno & error_reporting()) {
  173.             $fp = fopen("php://stderr", "w");
  174.             
  175.             switch ($errno) {
  176.             case E_ERROR           : fputs($fp, "Error"); break;
  177.             case E_WARNING         : fputs($fp, "Warning"); break;
  178.             case E_PARSE           : fputs($fp, "Parsing Error"); break;
  179.             case E_NOTICE          : fputs($fp, "Notice"); break;
  180.             case E_CORE_ERROR      : fputs($fp, "Core Error"); break;
  181.             case E_CORE_WARNING    : fputs($fp, "Core Warning"); break;
  182.             case E_COMPILE_ERROR   : fputs($fp, "Compile Error"); break;
  183.             case E_COMPILE_WARNING : fputs($fp, "Compile Warning"); break;
  184.             case E_USER_ERROR      : fputs($fp, "User Error"); break;
  185.             case E_USER_WARNING    : fputs($fp, "User Warning"); break;
  186.             case E_USER_NOTICE     : fputs($fp, "User Notice"); break;
  187.             case E_STRICT          : fputs($fp, "Runtime Notice"); break;
  188.             default                : fputs($fp, "Unknown Error"); break;
  189.             }
  190.             
  191.             fputs($fp, ": $errstr in $errfile on line $errline\n");
  192.             fclose($fp);
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Create extension using the given parser 
  198.      *
  199.      * @param object  Extension parser
  200.      */
  201.     function execute($parser)
  202.     {
  203.         // normal operation: read XML file and go with that
  204.         $arguments = $this->options->arguments();
  205.         if (count($arguments) != 1) {
  206.             $this->showUsage();
  207.             exit(3);
  208.         }
  209.         
  210.         $xmlfile = $arguments[0];
  211.         
  212.         if (!file_exists($xmlfile) || !is_readable($xmlfile)) {
  213.             $this->terminate("Cannot open spec file '$xmlfile'");
  214.         }
  215.         
  216.         // create parser for extension specs
  217.         $err = $parser->setInputFile($xmlfile);
  218.         if (PEAR::isError($err)) {
  219.             $this->terminate($err->message);
  220.         }
  221.         
  222.         // do the actual parsing
  223.         $err = $parser->parse();
  224.         if (PEAR::isError($err)) {
  225.             $this->terminate($err->getMessage()." ".$err->getUserInfo());
  226.         } else if(is_string($err)) {
  227.           $this->terminate($err);
  228.         }
  229.       
  230.         if ($this->options->have("l", "lint")) {
  231.             return;
  232.         }  
  233.         
  234.         // and now create the actual extension from the collected specs
  235.         $err = $this->extension->createExtension($this->options->value("d", "dir"), $this->options->have("f", "force"));
  236.         if (PEAR::isError($err)) {
  237.             $this->terminate($err->getMessage()." ".$err->getUserInfo());
  238.         }
  239.         
  240.         if (!$this->options->have("q", "quiet")) {
  241.             echo $this->extension->successMsg($this->extension->getName());
  242.         }
  243.         
  244.     }
  245. }
  246.