Exemple #1
0
 /**
  * Run the application.
  * @param string $environment Configuration environment to use, e.g.
  * 'production' or 'development'. Environments are stored in
  * 'app/config/environments'.
  * @param bool $enableCli Whether to enable the command-line interface. If
  * enabled, the environment will be set to 'cli' when the application is run
  * from the command-line.
  */
 public function run($environment = 'production', $enableCli = true)
 {
     if ($this->isCli()) {
         if (!$enableCli) {
             echo tr('The command-line interface is disabled.');
             $this->stop();
         }
         $environment = 'cli';
     }
     $this->environment = $environment;
     // Precompute paths used for error handling
     $logDir = $this->p('log');
     if (Utilities::dirExists($logDir)) {
         $this->errorPaths['log'] = realpath($logDir);
     }
     $errorTemplate = $this->p('app/templates/error/error.php');
     if (file_exists($errorTemplate)) {
         $this->errorPaths['errorTemplate'] = realpath($errorTemplate);
     }
     $exceptionTemplate = $this->p('app/templates/error/exception.php');
     if (file_exists($exceptionTemplate)) {
         $this->errorPaths['exceptionTemplate'] = realpath($exceptionTemplate);
     }
     // Set exception handler
     set_exception_handler(array($this, 'handleError'));
     // Set fatal error handler
     register_shutdown_function(array($this, 'handleFatalError'));
     // Force output buffereing (so that error-pages can clear it)
     ob_start();
     // Load default configuration from application and/or Core
     if (file_exists($this->p('app/config/default.php'))) {
         $this->config->defaults = (include $this->p('app/config/default.php'));
     }
     $this->config->defaults = (include $this->p('Core/config/default.php'));
     // Set timezone (required by file logger)
     if (!isset($this->config['user']['i18n']['timeZone'])) {
         $defaultTimeZone = 'UTC';
         $error = ErrorHandler::detect(function () use($defaultTimeZone) {
             $defaultTimeZone = @date_default_timezone_get();
         });
         $this->config['user']['i18n']['timeZone'] = $defaultTimeZone;
     }
     if (!date_default_timezone_set($this->config['user']['i18n']['timeZone'])) {
         date_default_timezone_set('UTC');
     }
     // Set up the default file loger
     if ($this->logger instanceof Logger) {
         try {
             $this->logger->addHandler(new FileHandler($this->p('log/' . $this->environment . '.log'), $this->config['system']->get('logLevel', LogLevel::WARNING)));
         } catch (LogException $e) {
             $this->logger->warning($e->getMessage(), array('exception' => $e));
             $this->logger->addHandler(new FileHandler($this->p('log/' . $this->environment . '.log'), LogLevel::WARNING));
         }
     }
     // Check PHP version
     if (version_compare(phpversion(), $this->manifest['minPhpVersion']) < 0) {
         throw new AppException(tr('%1 does not support PHP %2. PHP %3 or above is required', $this->name, phpversion(), $this->minPhpVersion));
     }
     $class = $this->n('Init');
     if (!class_exists($class)) {
         $class = 'Jivoo\\Core\\Init';
     }
     $this->m->init = new $class($this);
     $this->triggerEvent('init');
     $this->m->init->init($environment);
     $this->triggerEvent('ready');
     $this->logger->warning(tr('Application not stopped'));
     $this->stop(1);
 }
Exemple #2
0
 public static function dumpException(\Exception $exception, $stream = STDERR)
 {
     if ($exception instanceof \ErrorException) {
         $title = 'Fatal error (' . ErrorHandler::toString($exception->getSeverity()) . ')';
     } else {
         $title = get_class($exception);
     }
     fwrite($stream, $title . ': ' . $exception->getMessage() . ' in ' . $exception->getFile() . ':' . $exception->getLine() . PHP_EOL . PHP_EOL);
     fwrite($stream, 'Stack trace:' . PHP_EOL);
     $trace = $exception->getTrace();
     foreach ($trace as $i => $call) {
         $message = '  ' . sprintf('% 2d', $i) . '. ';
         if (isset($call['file'])) {
             $message .= $call['file'] . ':';
             $message .= $call['line'] . ' ';
         }
         if (isset($call['class'])) {
             $message .= $call['class'] . '::';
         }
         $message .= $call['function'] . '(';
         $arglist = array();
         if (isset($call['args'])) {
             foreach ($call['args'] as $arg) {
                 $arglist[] = is_scalar($arg) ? var_export($arg, true) : gettype($arg);
             }
             $message .= implode(', ', $arglist);
         }
         $message .= ')' . PHP_EOL;
         fwrite($stream, $message);
     }
     $previous = $exception->getPrevious();
     if (isset($previous)) {
         fwrite($stream, 'Caused by:' . PHP_EOL);
         self::dumpException($previous);
     }
     fflush($stream);
 }