Source for file jCmdlineCoordinator.class.php

Documentation is available at jCmdlineCoordinator.class.php

  1. <?php
  2. /**
  3. @package      jelix
  4. @subpackage   core
  5. @author       Christophe Thiriot
  6. @contributor  Laurent Jouanneau
  7. @copyright    2008 Christophe Thiriot, 2011 Laurent Jouanneau
  8. @link         http://www.jelix.org
  9. @licence      GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. */
  11.  
  12. /**
  13.  * The command line version of jCoordinator
  14.  *
  15.  * This allows us to handle exit code of commands properly
  16.  * @package  jelix
  17.  * @subpackage core
  18.  */
  19. class jCmdlineCoordinator extends jCoordinator {
  20.  
  21.     function __construct ($configFile$enableErrorHandler=true{
  22.         if (PHP_SAPI != 'cli' && strpos(PHP_SAPI'cgi'=== false{
  23.             throw new Exception("Error: you're not allowed to execute this script outside a command line shell.");
  24.         }
  25.  
  26.         if (PHP_SAPI != 'cli'{
  27.             // only php-cgi used from the command line can be used, not the one called by apache
  28.             if (isset($_SERVER['HTTP_HOST']|| isset($_SERVER['REDIRECT_URL'])  || isset($_SERVER['SERVER_PORT'])) {
  29.                 throw new Exception("Error: you're not allowed to execute this script with php-cgi outside a command line shell.");
  30.             }
  31.             header('Content-type: text/plain');
  32.             if (!isset($_SERVER['argv'])) {
  33.                 $_SERVER['argv'array_keys($_GET);
  34.                 $_SERVER['argc'count($_GET);
  35.             }
  36.             if (!isset($_SERVER['SCRIPT_NAME'])) {
  37.                 $_SERVER['SCRIPT_NAME'$_SERVER['argv'][0];
  38.             }
  39.             if (!isset($_SERVER['DOCUMENT_ROOT'])) {
  40.                 $_SERVER['DOCUMENT_ROOT''';
  41.             }
  42.         }
  43.  
  44.         jApp::setEnv('cli');
  45.         parent::__construct($configFile$enableErrorHandler);
  46.     }
  47.  
  48.     /**
  49.     * main method : launch the execution of the action.
  50.     *
  51.     * This method should be called in a Command line entry point.
  52.     * @param  jRequestCmdline  $request the command line request object
  53.     */
  54.     public function process($request){
  55.         parent::process($request);
  56.         exit($this->response->getExitCode());
  57.     }
  58.  
  59.     public $allErrorMessages = array();
  60.  
  61.     /**
  62.      * Handle an error event. Called by error handler and exception handler.
  63.      * @param string  $type    error type : 'error', 'warning', 'notice'
  64.      * @param integer $code    error code
  65.      * @param string  $message error message
  66.      * @param string  $file    the file name where the error appear
  67.      * @param integer $line    the line number where the error appear
  68.      * @param array   $trace   the stack trace
  69.      * @since 1.1
  70.      */
  71.     public function handleError($type$code$message$file$line$trace){
  72.         global $gJConfig;
  73.  
  74.         $errorLog new jLogErrorMessage($type$code$message$file$line$trace);
  75.  
  76.         if ($this->request{
  77.             // we have config, so we can process "normally"
  78.             $errorLog->setFormat($gJConfig->error_handling['messageLogFormat']);
  79.             jLog::log($errorLog$type);
  80.             $this->allErrorMessages[$errorLog;
  81.  
  82.             // if non fatal error, it is finished
  83.             if ($type != 'error')
  84.                 return;
  85.  
  86.             $this->errorMessage = $errorLog;
  87.  
  88.             while (ob_get_level()) {
  89.                 ob_end_clean();
  90.             }
  91.  
  92.             if($this->response{
  93.                 $resp $this->response;
  94.             }
  95.             else {
  96.                 $resp $this->response = new jResponseCmdline();
  97.             }
  98.             $resp->outputErrors();
  99.             jSession::end();
  100.         }
  101.         // for non fatal error appeared during init, let's just store it for loggers later
  102.         elseif ($type != 'error'{
  103.             $this->allErrorMessages[$errorLog;
  104.             $this->initErrorMessages[$errorLog;
  105.             return;
  106.         }
  107.         else {
  108.             // fatal error appeared during init, let's display a single message
  109.             while (ob_get_level()) {
  110.                 ob_end_clean();
  111.             }
  112.             // log into file
  113.             @error_log($errorLog->getFormatedMessage(),3jApp::logPath('errors.log'));
  114.             // output text response
  115.             echo 'Error during initialization: '.$message.' ('.$file.' '.$line.")\n";
  116.         }
  117.         exit(1);
  118.     }
  119. }

Documentation generated on Mon, 19 Sep 2011 14:11:59 +0200 by phpDocumentor 1.4.3