Changeset 323

Show
Ignore:
Timestamp:
12/20/07 15:13:19 (7 months ago)
Author:
hans
Message:

Refs #201 - Added improved support for arg parsing in Phing::execute() as well as better error handling between phing.php CLI script and Phing class.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/2.3/bin/phing.php

    r322 r323  
    1313/* set classpath */ 
    1414if (getenv('PHP_CLASSPATH')) { 
    15     define('PHP_CLASSPATH',  getenv('PHP_CLASSPATH') . PATH_SEPARATOR . get_include_path()); 
     15    if (!defined('PHP_CLASSPATH')) { define('PHP_CLASSPATH',  getenv('PHP_CLASSPATH') . PATH_SEPARATOR . get_include_path()); } 
    1616    ini_set('include_path', PHP_CLASSPATH); 
    1717} else { 
    18     define('PHP_CLASSPATH',  get_include_path()); 
     18    if (!defined('PHP_CLASSPATH')) { define('PHP_CLASSPATH',  get_include_path()); } 
    1919} 
    2020 
     
    3131 
    3232        // Grab and clean up the CLI arguments 
    33         $args = isset($argv) ? $argv : $_SERVER['argv']; // $_SERVER['argv'] seems not to work when argv is registered (PHP5b4) 
     33        $args = isset($argv) ? $argv : $_SERVER['argv']; // $_SERVER['argv'] seems to not work (sometimes?) when argv is registered 
    3434        array_shift($args); // 1st arg is script name, so drop it 
    3535         
     
    4040        Phing::shutdown(); 
    4141         
     42} catch (ConfigurationException $x) { 
     43         
     44        Phing::printMessage($x); 
     45        exit(-1); // This was convention previously for configuration errors. 
     46         
    4247} catch (Exception $x) { 
    43         // There was a problem; it should have already been reported as part of the build 
    44         // results, so we just need to return the result code. 
    45         if ($x->getCode() != 0) { 
    46                 exit($x->getCode()); 
    47         } else { 
    48                 exit(1); 
    49         } 
     48         
     49        // Assume the message was already printed as part of the build and 
     50        // exit with non-0 error code. 
     51         
     52        exit(1); 
     53         
    5054} 
    5155 
  • branches/2.3/classes/phing/Phing.php

    r322 r323  
    128128         */ 
    129129        private static $isLogFileUsed = false; 
     130         
     131        /** 
     132         * Array to hold original ini settings that Phing changes (and needs 
     133         * to restore in restoreIni() method). 
     134         * 
     135         * @var array Struct of array(setting-name => setting-value) 
     136         * @see restoreIni() 
     137         */ 
     138        private static $origIniSettings = array(); 
    130139 
    131140        /** 
     
    134143         * This method encapsulates the complete build lifecycle. 
    135144         * 
    136          * @param array &$args The commandline args passed to phing shell script. 
     145         * @param array $args The commandline args passed to phing shell script. 
    137146         * @param array $additionalUserProperties   Any additional properties to be passed to Phing (alternative front-end might implement this). 
    138147         *                                          These additional properties will be available using the getDefinedProperty() method and will 
     
    142151         * @throws Exception - if there is an error during build 
    143152         */ 
    144         public static function start(&$args, $additionalUserProperties = null) { 
    145  
     153        public static function start($args, $additionalUserProperties = null) { 
     154                 
    146155                try { 
    147156                        $m = new Phing(); 
    148157                        $m->execute($args); 
    149158                } catch (Exception $exc) { 
    150                         self::handleLogfile(); // clean up log file before attempting to print message 
    151                         $m->printMessage($exc); 
     159                        self::handleLogfile(); 
    152160                        throw $exc; 
    153161                } 
     
    165173                        $m->runBuild(); 
    166174                } catch(Exception $exc) { 
    167                         if ($exc instanceof ConfigurationException) { 
    168                                 if (self::$msgOutputLevel >= Project::MSG_VERBOSE) { 
    169                                         self::$out->write($exc->__toString() . PHP_EOL); 
    170                                 } else { 
    171                                         self::$out->write($exc->getMessage() . PHP_EOL); 
    172                                 } 
    173                         } 
    174175                        self::handleLogfile(); 
    175176                        throw $exc; 
     
    185186         */ 
    186187        public static function printMessage(Exception $t) { 
    187                 if (self::getMsgOutputLevel() <= Project::MSG_DEBUG) { 
    188                         self::$err->write($t->__toString()); 
     188                if (self::$err === null) { // Make sure our error output is initialized 
     189                        self::initializeOutputStreams(); 
     190                } 
     191                if (self::getMsgOutputLevel() >= Project::MSG_VERBOSE) { 
     192                        self::$err->write($t->__toString() . PHP_EOL); 
    189193                } else { 
    190                         self::$err->write($t->getMessage()); 
     194                        self::$err->write($t->getMessage() . PHP_EOL); 
    191195                } 
    192196        } 
     
    279283                self::$definedProps = new Properties(); 
    280284                $this->searchForThis = null; 
    281  
    282                 // cycle through given args 
    283                 for ($i = 0, $argcount = count($args); $i < $argcount; ++$i) { 
    284                         // ++$i intentional here, as first param is script name 
    285                         $arg = $args[$i]; 
    286  
    287                         if ($arg == "-help" || $arg == "-h") { 
    288                                 $this->printUsage(); 
    289                                 return; 
    290                         } elseif ($arg == "-version" || $arg == "-v") { 
    291                                 $this->printVersion(); 
    292                                 return; 
    293                         } elseif ($arg == "-quiet" || $arg == "-q") { 
    294                                 self::$msgOutputLevel = Project::MSG_WARN; 
    295                         } elseif ($arg == "-verbose") { 
    296                                 $this->printVersion(); 
    297                                 self::$msgOutputLevel = Project::MSG_VERBOSE; 
    298                         } elseif ($arg == "-debug") { 
    299                                 $this->printVersion(); 
    300                                 self::$msgOutputLevel = Project::MSG_DEBUG; 
    301                         } elseif ($arg == "-logfile") { 
     285                 
     286                // 1) First handle any options which should always 
     287                // Note: The order in which these are executed is important (if multiple of these options are specified) 
     288                 
     289                if (in_array('-help', $args) || in_array('-h', $args)) { 
     290                        $this->printUsage(); 
     291                        return; 
     292                } 
     293                 
     294                if (in_array('-version', $args) || in_array('-v', $args)) { 
     295                        $this->printVersion(); 
     296                        return; 
     297                } 
     298                 
     299                // 2) Next pull out stand-alone args. 
     300                // Note: The order in which these are executed is important (if multiple of these options are specified) 
     301                 
     302                if (false !== ($key = array_search('-quiet', $args, true))) { 
     303                        self::$msgOutputLevel = Project::MSG_WARN; 
     304                        unset($args[$key]); 
     305                } 
     306                 
     307                if (false !== ($key = array_search('-verbose', $args, true))) { 
     308                        self::$msgOutputLevel = Project::MSG_VERBOSE; 
     309                        unset($args[$key]); 
     310                } 
     311                 
     312                if (false !== ($key = array_search('-debug', $args, true))) { 
     313                        self::$msgOutputLevel = Project::MSG_DEBUG; 
     314                        unset($args[$key]); 
     315                } 
     316                 
     317                // 3) Finally, cycle through to parse remaining args 
     318                foreach ($args as $i => $arg) { 
     319                         
     320                        if ($arg == "-logfile") { 
    302321                                try { 
    303322                                        // see: http://phing.info/trac/ticket/65 
     
    11851204 
    11861205        /** 
    1187          * Sets the include path based on PHP_CLASSPATH constant (set in phing.php). 
     1206         * Sets the include path to PHP_CLASSPATH constant (if this has been defined). 
    11881207         * @return void 
    1189          * @throws BuildException - if the include_path could not be set (for some bizarre reason) 
     1208         * @throws ConfigurationException - if the include_path could not be set (for some bizarre reason) 
    11901209         */ 
    11911210        private static function setIncludePaths() { 
    1192                 $success = false; 
    1193  
    11941211                if (defined('PHP_CLASSPATH')) { 
    1195                         $success = set_include_path(PHP_CLASSPATH); 
    1196                 } else { 
    1197                         // don't do anything, just assume that include_path has been properly set. 
    1198                         $success = true; 
    1199                 } 
    1200  
    1201                 if ($success === false) { 
    1202                         throw new BuildException("Could not set PHP include path"); 
     1212                        $result = set_include_path(PHP_CLASSPATH); 
     1213                        if ($result === false) { 
     1214                                throw new ConfigurationException("Could not set PHP include_path."); 
     1215                        } 
     1216                        self::$origIniSettings['include_path'] = $result; // save original value for setting back later 
    12031217                } 
    12041218        } 
     
    12091223         */ 
    12101224        private static function setIni() { 
    1211                 error_reporting(E_ALL); 
     1225                 
     1226                self::$origIniSettings['error_reporting'] = error_reporting(E_ALL); 
     1227                 
     1228                // We won't bother storing original max_execution_time, since 1) the value in  
     1229                // php.ini may be wrong (and there's no way to get the current value) and  
     1230                // 2) it would mean something very strange to set it to a value less than time script 
     1231                // has already been running, which would be the likely change. 
     1232                 
    12121233                set_time_limit(0); 
    1213                 ini_set('magic_quotes_gpc', 'off'); 
    1214                 ini_set('short_open_tag', 'off'); 
    1215                 ini_set('default_charset', 'iso-8859-1'); 
    1216                 ini_set('register_globals', 'off'); 
    1217                 ini_set('allow_call_time_pass_reference', 'on'); 
    1218                 ini_set('track_errors', 1); 
     1234                 
     1235                self::$origIniSettings['magic_quotes_gpc'] = ini_set('magic_quotes_gpc', 'off'); 
     1236                self::$origIniSettings['short_open_tag'] = ini_set('short_open_tag', 'off'); 
     1237                self::$origIniSettings['default_charset'] = ini_set('default_charset', 'iso-8859-1'); 
     1238                self::$origIniSettings['register_globals'] = ini_set('register_globals', 'off'); 
     1239                self::$origIniSettings['allow_call_time_pass_reference'] = ini_set('allow_call_time_pass_reference', 'on'); 
     1240                self::$origIniSettings['track_errors'] = ini_set('track_errors', 1); 
    12191241                 
    12201242                // should return memory limit in MB 
    12211243                $mem_limit = (int) ini_get('memory_limit'); 
    12221244                if ($mem_limit < 32) { 
     1245                        // We do *not* need to save the original value here, since we don't plan to restore 
     1246                        // this after shutdown (we don't trust the effectiveness of PHP's garbage collection). 
    12231247                        ini_set('memory_limit', '32M'); // nore: this may need to be higher for many projects 
     1248                } 
     1249        } 
     1250         
     1251        /** 
     1252         * Restores [most] PHP INI values to their pre-Phing state. 
     1253         *  
     1254         * Currently the following settings are not restored: 
     1255         *      - max_execution_time (because getting current time limit is not possible) 
     1256         *  - memory_limit (which may have been increased by Phing) 
     1257         *  
     1258         * @return void 
     1259         */ 
     1260        private static function restoreIni() 
     1261        { 
     1262                foreach(self::$origIniSettings as $settingName => $settingValue) { 
     1263                        switch($settingName) { 
     1264                                case 'error_reporting': 
     1265                                        error_reporting($settingValue); 
     1266                                        break; 
     1267                                default: 
     1268                                        ini_set($settingName, $settingValue); 
     1269                        } 
    12241270                } 
    12251271        } 
     
    12391285        /** 
    12401286         * Start up Phing. 
    1241          * Sets up the Phing environment -- does NOT initiate the build process. 
     1287         * Sets up the Phing environment but does not initiate the build process. 
    12421288         * @return void 
     1289         * @throws Exception - If the Phing environment cannot be initialized.  
    12431290         */ 
    12441291        public static function startup() { 
    1245                          
    1246                 register_shutdown_function(array('Phing', 'shutdown')); 
    12471292 
    12481293                // setup STDOUT and STDERR defaults 
     
    12591304        /** 
    12601305         * Halts the system. 
     1306         * @deprecated This method is deprecated and is no longer called by Phing internally.  Any 
     1307         *                              normal shutdown routines are handled by the shutdown() method. 
    12611308         * @see shutdown() 
    12621309         */ 
    1263         public static function halt($code=0) { 
    1264                 self::shutdown($code); 
    1265         } 
    1266  
    1267         /** 
    1268          * Stops timers & exits. 
     1310        public static function halt() { 
     1311                self::shutdown(); 
     1312        } 
     1313 
     1314        /** 
     1315         * Performs any shutdown routines, such as stopping timers. 
    12691316         * @return void 
    12701317         */ 
    1271         public static function shutdown($exitcode = 0) { 
     1318        public static function shutdown() { 
     1319                self::restoreIni(); 
    12721320                self::getTimer()->stop(); 
    1273                 //exit($exitcode); // final point where everything stops 
    12741321        } 
    12751322