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 (!jServer::isCLI()) {
  23.             throw new Exception("Error: you're not allowed to execute this script outside a command line shell.");
  24.         }
  25.  
  26.         jApp::setEnv('cli');
  27.         parent::__construct($configFile, $enableErrorHandler);
  28.     }
  29.  
  30.     /**
  31.     * main method : launch the execution of the action.
  32.     *
  33.     * This method should be called in a Command line entry point.
  34.     * @param  jRequestCmdline  $request the command line request object
  35.     */
  36.     public function process($request){
  37.         parent::process($request);
  38.         exit($this->response->getExitCode());
  39.     }
  40.  
  41.     public $allErrorMessages = array();
  42.  
  43.     /**
  44.      * Handle an error event. Called by error handler and exception handler.
  45.      * @param string  $type    error type : 'error', 'warning', 'notice'
  46.      * @param integer $code    error code
  47.      * @param string  $message error message
  48.      * @param string  $file    the file name where the error appear
  49.      * @param integer $line    the line number where the error appear
  50.      * @param array   $trace   the stack trace
  51.      * @since 1.1
  52.      */
  53.     public function handleError($type, $code, $message, $file, $line, $trace){
  54.         global $gJConfig;
  55.  
  56.         $errorLog = new jLogErrorMessage($type, $code, $message, $file, $line, $trace);
  57.  
  58.         if ($this->request) {
  59.             // we have config, so we can process "normally"
  60.             $errorLog->setFormat($gJConfig->error_handling['messageLogFormat']);
  61.             jLog::log($errorLog, $type);
  62.             $this->allErrorMessages[] = $errorLog;
  63.  
  64.             // if non fatal error, it is finished
  65.             if ($type != 'error')
  66.                 return;
  67.  
  68.             $this->errorMessage = $errorLog;
  69.  
  70.             while (ob_get_level() && @ob_end_clean());
  71.  
  72.             if($this->response) {
  73.                 $resp = $this->response;
  74.             }
  75.             else {
  76.                 require_once(JELIX_LIB_CORE_PATH.'response/jResponseCmdline.class.php');
  77.                 $resp = $this->response = new jResponseCmdline();
  78.             }
  79.             $resp->outputErrors();
  80.             jSession::end();
  81.         }
  82.         // for non fatal error appeared during init, let's just store it for loggers later
  83.         elseif ($type != 'error') {
  84.             $this->allErrorMessages[] = $errorLog;
  85.             $this->initErrorMessages[] = $errorLog;
  86.             return;
  87.         }
  88.         else {
  89.             // fatal error appeared during init, let's display a single message
  90.             while (ob_get_level() && @ob_end_clean());
  91.             // log into file
  92.             @error_log($errorLog->getFormatedMessage()."\n",3, jApp::logPath('errors.log'));
  93.             // output text response
  94.             echo 'Error during initialization: '.$message.' ('.$file.' '.$line.")\n";
  95.         }
  96.         exit(1);
  97.     }
  98. }

Documentation generated on Wed, 24 Sep 2014 21:56:42 +0200 by phpDocumentor 1.4.3