コード例 #1
0
ファイル: Configuration.php プロジェクト: Victopia/prefw
 /**
  * Query contents with current configuration key from the database,
  * and update the local stored value.
  */
 function update()
 {
     // Root objects will get value from database upon creation
     if ($this->parentObject === null) {
         $confObj = array();
         // Database support
         if (Database::isConnected()) {
             $res = (array) @Node::getOne(array(Node::FIELD_COLLECTION => FRAMEWORK_COLLECTION_CONFIGURATION, '@key' => $this->key));
             unset($res['@key'], $res[Node::FIELD_COLLECTION]);
             $confObj += $res;
             unset($res);
         }
         // basenames to search
         $basenames = array('', gethostname());
         array_walk($basenames, function ($basename) use(&$confObj) {
             if ($basename) {
                 $basename = ".{$basename}";
             }
             $basename = self::FALLBACK_DIRECTORY . "/{$this->key}{$basename}";
             // JSON Support
             $res = "{$basename}.json";
             if (is_readable($res)) {
                 $res = (array) @ContentDecoder::json(file_get_contents($res), 1);
                 if ($res) {
                     $confObj = $res + $confObj;
                 } else {
                     throw new exceptions\FrameworkException('JSON file exists but decode failed.');
                 }
             }
             unset($res);
             // YAML support (symfony/yaml)
             if (class_exists('Yaml')) {
                 $res = "{$basename}.yaml";
                 if (is_readable($res)) {
                     $res = Yaml::parse($res);
                     // Sorry mate, at least an array.
                     if (is_array($res)) {
                         $confObj = $res + $confObj;
                     } else {
                         throw new exceptions\FrameworkException('YAML file exists but decode failed.');
                     }
                 }
                 unset($res);
             }
         });
     } else {
         $confObj =& $this->parentObject->__valueOf();
         $confObj =& $confObj[$this->key];
     }
     $this->contents =& $confObj;
 }
コード例 #2
0
ファイル: ExceptionsHandler.php プロジェクト: Victopia/prefw
 public static function handleException($e)
 {
     if (error_reporting() == 0) {
         return;
     }
     while (ob_get_level() > 0) {
         ob_end_clean();
     }
     $eS = $e->getMessage();
     $eN = $e->getCode();
     $eC = $e->getTrace();
     // Put current context into stack trace
     array_unshift($eC, array('file' => $e->getFile(), 'line' => $e->getLine()));
     if ($e instanceof ErrorException) {
         switch ($e->getSeverity()) {
             case E_ERROR:
             case E_PARSE:
             case E_CORE_ERROR:
             case E_USER_ERROR:
             default:
                 $logType = LogLevel::CRITICAL;
                 break;
             case E_WARNING:
             case E_CORE_WARNING:
             case E_USER_WARNING:
                 $logType = LogLevel::WARNING;
                 break;
             case E_DEPRECATED:
             case E_NOTICE:
             case E_USER_DEPRECATED:
             case E_USER_NOTICE:
                 $logType = LogLevel::NOTICE;
                 break;
             case E_STRICT:
                 $logType = LogLevel::INFO;
                 break;
         }
         $exceptionType = 'error';
     } else {
         $exceptionType = get_class($e);
         if (strpos($exceptionType, '\\') !== false) {
             $exceptionType = substr(strrchr($exceptionType, '\\'), 1);
         }
         $logType = LogLevel::ERROR;
     }
     $logString = sprintf('[Gateway] Uncaught %s#%d with message: "%s".', $exceptionType, $eN, $eS);
     unset($exceptionType);
     // Current request context
     $resolver = Resolver::getActiveInstance();
     if ($resolver) {
         if ($resolver->request()) {
             $client = $resolver->request()->client();
         }
         $response = $resolver->response();
     }
     unset($resolver);
     // Prevent recursive errors on logging when database fails to connect.
     if (Database::isConnected()) {
         // Release table locks of current session.
         @Database::unlockTables(false);
         if (Database::inTransaction()) {
             @Database::rollback();
         }
     }
     $logContext = array_filter(array('errorContext' => $eC, 'client' => @$client));
     // Log the error
     try {
         @Log::log($logType, $logString, $logContext);
     } catch (\Exception $e) {
     }
     unset($logContext);
     // Send the error to output
     $output = array('error' => $eS, 'code' => $eN);
     if (System::environment(false) != System::ENV_PRODUCTION) {
         $output['trace'] = $eC;
     }
     // Display error message
     if (isset($response) && @$client['type'] != 'cli') {
         // Do i18n when repsonse context is available
         if ($e instanceof GeneralException) {
             $errorMessage = $response->__($eS, $logType);
             if ($errorMessage) {
                 $output['error'] = $errorMessage;
             }
         }
         if ($e instanceof ErrorException) {
             $statusCode = 500;
         } else {
             $statusCode = 400;
         }
         if ($e instanceof ValidationException) {
             $output['errors'] = $e->getErrors();
         }
         $response->clearHeaders();
         $response->header('Content-Type', 'application/json; charset=utf-8');
         $response->send($output, $statusCode);
     } else {
         header('Content-Type: text/plain', true, 500);
         echo "{$logString}\n";
         // Debug stack trace
         if (System::environment(false) != System::ENV_PRODUCTION) {
             echo "Trace:\n";
             array_walk($eC, function ($stack, $index) {
                 $trace = $index + 1 . '.';
                 $function = implode('->', array_filter(array(@$stack['class'], @$stack['function'])));
                 if ($function) {
                     $trace .= " {$function}()";
                 }
                 unset($function);
                 if (@$stack['file']) {
                     $trace .= " {$stack['file']}";
                     if (@$stack['line']) {
                         $trace .= ":{$stack['line']}";
                     }
                 }
                 echo "{$trace}\n";
             });
         }
     }
     // CLI exit code on Exceptions and Errors
     if (in_array($logType, array(LogLevel::ERROR, LogLevel::CRITICAL, LogLevel::ALERT, LogLevel::EMERGENCY))) {
         $exitCode = $e->getCode();
         if ($exitCode <= 0) {
             $exitCode = 1;
         }
         die($exitCode);
     }
 }