CLI.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | FastFrame Application Framework                                      |
00007 // +----------------------------------------------------------------------+
00008 // | Copyright 2003-2005 Chuck Hagenbuch <chuck@horde.org>                |
00009 // | Copyright 2003-2005 Jan Schneider <jan@horde.org>                    |
00010 // | Copyright (c) 2004 The Codejanitor Group                             |
00011 // +----------------------------------------------------------------------+
00012 // | This source file is subject to the GNU Lesser Public License (LGPL), |
00013 // | that is bundled with this package in the file LICENSE, and is        |
00014 // | available at through the world-wide-web at                           |
00015 // | http://www.fsf.org/copyleft/lesser.html                              |
00016 // | If you did not receive a copy of the LGPL and are unable to          |
00017 // | obtain it through the world-wide-web, you can get it by writing the  |
00018 // | Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
00019 // | MA 02111-1307, USA.                                                  |
00020 // +----------------------------------------------------------------------+
00021 // | Authors: Chuck Hagenbuch <chuck@horde.org                            |
00022 // | Authors: Jan Schneider <jan@horde.org                                |
00023 // | Authors: Jason Rust <jrust@codejanitor.com>                          |
00024 // +----------------------------------------------------------------------+
00025 
00026 // }}}
00027 // {{{ requires
00028 
00029 require_once dirname(__FILE__) . '/../FastFrame.php';
00030 require_once dirname(__FILE__) . '/ErrorHandler.php';
00031 require_once dirname(__FILE__) . '/Registry.php';
00032 require_once dirname(__FILE__) . '/Request.php';
00033 require_once dirname(__FILE__) . '/ActionHandler.php';
00034 
00035 // }}}
00036 // {{{ class FF_CLI
00037 
00047 // }}}
00048 class FF_CLI {
00049     // {{{ properties
00050 
00055     var $_console;
00056 
00061     var $_newline;
00062 
00067     var $_indent;
00068 
00073     var $_bold_start = '';
00074 
00079     var $_bold_end = '';
00080 
00084     var $_red_start    = '';
00085     var $_green_start  = '';
00086     var $_yellow_start = '';
00087     var $_blue_start   = '';
00088 
00092     var $_red_end      = '';
00093     var $_green_end    = '';
00094     var $_yellow_end   = '';
00095     var $_blue_end     = '';
00096 
00097     // }}}
00098     // {{{ constructor
00099 
00110     function FF_CLI($in_app = 'fastframe')
00111     {
00112         $this->_console = $this->runningFromCLI();
00113 
00114         if ($this->_console) {
00115             @define('IS_AJAX', false);
00116             $this->init();
00117             $this->_newline = "\n";
00118             $this->_indent  = '    ';
00119 
00120             $term = getenv('TERM');
00121             if ($term) {
00122                 if (preg_match('/^(xterm|vt220|linux)/', $term)) {
00123                     $this->_bold_start   = "\x1b[1m";
00124                     $this->_red_start    = "\x1b[01;31m";
00125                     $this->_green_start  = "\x1b[01;32m";
00126                     $this->_yellow_start = "\x1b[01;33m";
00127                     $this->_blue_start   = "\x1b[01;34m";
00128                     $this->_bold_end = $this->_red_end = $this->_green_end = $this->_yellow_end = $this->_blue_end = "\x1b[0m";
00129                 } elseif (preg_match('/^vt100/', $term)) {
00130                     $this->_bold_start = "\x1b[1m";
00131                     $this->_bold_end   = "\x1b[0m";
00132                 }
00133             }
00134         } else {
00135             $this->_newline = '<br />';
00136             $this->_indent  = str_repeat('&nbsp;', 4);
00137 
00138             $this->_bold_start  = '<strong>';
00139             $this->_bold_end    = '</strong>';
00140             $this->_red_start   = '<span style="color:red">';
00141             $this->_green_start = '<span style="color:green">';
00142             $this->_yellow_start = '<span style="color:yellow">';
00143             $this->_blue_start   = '<span style="color:blue">';
00144             $this->_red_end = $this->_green_end = $this->_yellow_end = $this->_blue_end = '</span>';
00145         }
00146 
00147         $o_registry =& FF_Registry::singleton();
00148         $o_registry->pushApp($in_app);
00149         if (!isset($GLOBALS['o_error'])) {
00150             PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_NOTICE); 
00151             if ($this->_console) {
00152                 $a_reporter = array('stdout' => array('level' => E_VERY_ALL));
00153             }
00154             else {
00155                 $a_reporter = array('console' => array('level' => E_VERY_ALL));
00156             }
00157 
00158             $o_error = new FF_ErrorHandler($a_reporter);
00159             $GLOBALS['o_error'] =& $o_error;
00160             $o_error->setDateFormat('[Y-m-d H:i:s]');
00161             $o_error->setExcludeObjects(false);
00162         }
00163     }
00164 
00165     // }}}
00166     // {{{ singleton()
00167 
00171     function &singleton()
00172     {
00173         static $instance;
00174         if (!isset($instance)) {
00175             $instance = &new FF_CLI();
00176         }
00177 
00178         return $instance;
00179     }
00180 
00181     // }}}
00182     // {{{ writeln()
00183 
00191     function writeln($text = '', $pre = false)
00192     {
00193         if ($pre) {
00194             echo $this->_newline . $text;
00195         } else {
00196             echo $text . $this->_newline;
00197         }
00198     }
00199 
00200     // }}}
00201     // {{{ indent()
00202 
00208     function indent($text)
00209     {
00210         return $this->_indent . $text;
00211     }
00212 
00213     // }}}
00214     // {{{ bold()
00215 
00221     function bold($text)
00222     {
00223         return $this->_bold_start . $text . $this->_bold_end;
00224     }
00225 
00226     // }}}
00227     // {{{ red()
00228 
00234     function red($text)
00235     {
00236         return $this->_red_start . $text . $this->_red_end;
00237     }
00238 
00239     // }}}
00240     // {{{ green()
00241 
00247     function green($text)
00248     {
00249         return $this->_green_start . $text . $this->_green_end;
00250     }
00251 
00252     // }}}
00253     // {{{ blue()
00254 
00260     function blue($text)
00261     {
00262         return $this->_blue_start . $text . $this->_blue_end;
00263     }
00264 
00265     // }}}
00266     // {{{ yellow()
00267 
00273     function yellow($text)
00274     {
00275         return $this->_yellow_start . $text . $this->_yellow_end;
00276     }
00277 
00278     // }}}
00279     // {{{ message()
00280 
00289     function message($message, $type = 'cli.message')
00290     {
00291         switch ($type) {
00292         case 'cli.error':
00293             $type_message = '[ ' . $this->red('ERROR!') . ' ] ';
00294             break;
00295         case 'cli.warning':
00296             $type_message = '[  ' . $this->yellow('WARN') . '  ] ';
00297             break;
00298         case 'cli.success':
00299             $type_message = '[   ' . $this->green('OK') . '   ] ';
00300             break;
00301         case 'cli.message':
00302             $type_message = '[  ' . $this->blue('INFO') . '  ] ';
00303             break;
00304         }
00305 
00306         $this->writeln($type_message . $message);
00307     }
00308 
00309     // }}}
00310     // {{{ fatal()
00311 
00317     function fatal($error)
00318     {
00319         $this->writeln($this->red('===================='));
00320         $this->writeln();
00321         $this->writeln($this->red(_("Fatal Error!")));
00322         $this->writeln($this->red($error));
00323         $this->writeln();
00324         $this->writeln($this->red('===================='));
00325         exit;
00326     }
00327 
00328     // }}}
00329     // {{{ prompt()
00330 
00341     function prompt($prompt, $choices = null)
00342     {
00343         $stdin = fopen('php://stdin', 'r');
00344 
00345         // Main event loop to capture top level command.
00346         while (true) {
00347             // Print out the prompt message.
00348             $this->writeln($prompt . ' ', !is_array($choices));
00349             if (is_array($choices) && !empty($choices)) {
00350                 foreach ($choices as $key => $choice) {
00351                     $key = $this->bold($key);
00352                     $this->writeln($this->indent('(' . $key . ') ' . $choice));
00353                 }
00354                 $this->writeln(_("Type your choice: "), true);
00355 
00356                 // Get the user choice.
00357                 $response = trim(fgets($stdin, 256));
00358 
00359                 if (isset($choices[$response])) {
00360                     // Close standard in.
00361                     fclose($stdin);
00362                     return $response;
00363                 } else {
00364                     $this->writeln(sprintf(_("'%s' is not a valid choice."), $response));
00365                 }
00366             } else {
00367                 $response = trim(fgets($stdin, 256));
00368                 fclose($stdin);
00369                 return $response;
00370             }
00371         }
00372 
00373         return true;
00374     }
00375 
00376     // }}}
00377     // {{{ init()
00378 
00386     function init()
00387     {
00388         @set_time_limit(0);
00389         ob_implicit_flush(true);
00390         ini_set('html_errors', false);
00391         $_SERVER['HTTP_HOST'] = '127.0.0.1';
00392         $_SERVER['SERVER_NAME'] = '127.0.0.1';
00393         $_SERVER['SERVER_PORT'] = '';
00394         $_SERVER['REMOTE_ADDR'] = '';
00395         $_SERVER['PHP_SELF'] = isset($argv) ? $argv[0] : '';
00396     }
00397 
00398     // }}}
00399     // {{{ runningFromCLI()
00400 
00409     function runningFromCLI()
00410     {
00411         // STDIN isn't a CLI constant before 4.3.0
00412         $sapi = php_sapi_name();
00413         if (version_compare(PHP_VERSION, '4.3.0') >= 0 && $sapi != 'cgi') {
00414             return @is_resource(STDIN);
00415         } else {
00416             return in_array($sapi, array('cli', 'cgi')) && empty($_SERVER['REMOTE_ADDR']);
00417         }
00418     }
00419 
00420     // }}}
00421 }
00422 ?>

Generated on Fri Jun 23 11:38:15 2006 for FastFrame by  doxygen 1.4.4