Ejemplo n.º 1
0
 /**
  * Push one log onto the end of stack.
  */
 public static function prepare($class, $method, $params = [])
 {
     // convert argument value to string.
     $depth = 0;
     $parent = 0;
     foreach ($params as $pos => $val) {
         $str = Util\StringUtils::export($val);
         if (self::MAX_PARAMS_LENGTH < strlen($str)) {
             $str = substr($str, 0, self::MAX_PARAMS_LENGTH) . '...';
         }
         $params[$pos] = $str;
     }
     // detect parent process and depth.
     foreach (array_reverse(static::$stock, 1) as $frame => $log) {
         if (!$log[self::VAR_PROCESSED_TIME]) {
             $depth = $log[self::VAR_DEPTH] + 1;
             $parent = $frame;
             break;
         }
     }
     // push infomation to call stock.
     if (static::$activated) {
         static::$stock[] = [self::VAR_DEPTH => $depth, self::VAR_PARENT => $parent, self::VAR_CLASS => $class, self::VAR_METHOD => $method, self::VAR_PARAMS => $params, self::VAR_RESULT => null, self::VAR_PROCESSED_TIME => null, self::VAR_ELAPSED_TIME => null];
     }
     return count(static::$stock) - 1;
 }
Ejemplo n.º 2
0
 /**
  * Returns the string representation of the exception.
  *
  * @return  string  the string representation of the exception.
  */
 public function __toString()
 {
     if (PHP_SAPI === 'cli') {
         $context = Util\StringUtils::export($this->getContext());
         $parts = [sprintf('[%d:%s] %s', $this->getCode(), $this->getName(), $this->getMessage()), '  #file    ' . $this->getFile(), '  #line    ' . $this->getLine(), '  #context ' . $context];
         $parts[] = Util\DebugUtils::getBacktraceAsString($this);
     } else {
         $req = Http\Request::getInstance();
         $data = ['context' => $this->getContext(), 'header' => $req->getHeader(), 'get' => $req->getFromGET(), 'post' => $req->getFromPOST(), 'cookie' => $req->getFromCookie()];
         foreach ($data as $key => $val) {
             $data[$key] = Util\StringUtils::export($val);
         }
         $parts = [sprintf('[%d:%s] %s', $this->getCode(), $this->getName(), $this->getMessage()), '  #file    ' . $this->getFile(), '  #line    ' . $this->getLine(), '  #context ' . $data['context'], '  #domain  ' . @$_SERVER['SERVER_NAME'], '  #path    ' . $req->getPathInfo(), '  #auth    ' . $req->getAuthType(), '  #method  ' . $req->getMethod(), '  #get     ' . $data['get'], '  #post    ' . $data['post'], '  #cookie  ' . $data['cookie']];
         $parts[] = Util\DebugUtils::getBacktraceAsString($this);
     }
     return implode(PHP_EOL, $parts);
 }
Ejemplo n.º 3
0
 /**
  * Constructs and returns a validation failure message
  * with the given message key and value.
  * If a translator is available, the translation will be used.
  *
  * @param   int     The key of message.
  * @param   mixed   The value to validate.
  * @return  string  The generated message for validation failure.
  */
 protected function createMessage($key, $value = null)
 {
     // Gets the failure message by key.
     $message = isset(static::$MESSAGE_TEMPLATE[$key]) ? static::$MESSAGE_TEMPLATE[$key] : 'Unexpected validation failure messages is occurred';
     // 1) Translates the temaplte message
     // 2) Replaces the magical constant value
     // 3) Replaces the placeholder to bound variables.
     // 4) Auto-adjust the length of final generated message.
     $message = static::translateMessage($message);
     $message = Util\StringUtils::replace(self::MAGIC_BIND_NAME_VALUE, $value, $message, static::$obscuredValue);
     $message = Util\StringUtils::bindValues($message, $this->option, $strict = false);
     if (static::$autoAdjustMessage) {
         $message = Util\StringUtils::adjust($message, static::$maxMessageLength);
     }
     return $message;
 }
Ejemplo n.º 4
0
 /**
  * Allows a class to decide how it will react when it is treated like a string.
  *
  * @return  string  The class conditions as string.
  */
 public function __toString()
 {
     $cond = Util\StringUtils::export($this->data);
     return "[{$cond}]";
 }
Ejemplo n.º 5
0
 /**
  * Returns TRUE to accept the log event, FALSE to block it.
  *
  * @param   array    The event to write log.
  * @return  boolean  accepted?
  */
 public function filter($event)
 {
     $message = Util\StringUtils::export($event['message']);
     $extra = Util\StringUtils::export($event['extra']);
     return 0 < preg_match($this->pattern, $message) || 0 < preg_match($this->pattern, $extra);
 }
Ejemplo n.º 6
0
 /**
  * Performs type conversions whenever possible,
  * so that you always receive the data in the format you asked for.
  *
  * @param   array   The input data.
  * @param   string  The type spec format.
  * @param   bool    Throws Exception, if warning conversions is occurred.
  * @return  array   The result data.
  * @throws  Exception\IllegalArgumentException
  *          Exception\ZendTypeCastException
  */
 public static function parse($args, $typeSpec, $strict = false)
 {
     static $symbolToZType = ['l' => IS_LONG, 'L' => IS_LONG, 'd' => IS_DOUBLE, 'b' => IS_BOOL, 'a' => IS_ARRAY, 'A' => IS_ARRAY, 's' => IS_STRING, 'P' => IS_STRING, 'o' => IS_OBJECT, 'O' => IS_OBJECT, 'r' => IS_RESOURCE, 'f' => IS_CALLBACK];
     $typeArgs = [];
     $minNumArgs = -1;
     $maxNumArgs = 0;
     $numArgs = count($args);
     foreach (str_split($typeSpec) as $c) {
         switch ($c) {
             case 'l':
             case 'L':
             case 'd':
             case 'b':
             case 'a':
             case 'A':
             case 's':
             case 'p':
             case 'o':
             case 'O':
             case 'r':
             case 'f':
                 $typeArgs[] = $symbolToZType[$c];
                 $maxNumArgs++;
                 break;
             case '|':
                 $minNumArgs = $maxNumArgs;
                 break;
             default:
                 $message = 'Bad type specifier while parsing parameters';
                 $context = ['typeSpec' => $typeSpec];
                 throw new Exception\IllegalArgumentException($message, -1, $context);
         }
     }
     if ($minNumArgs < 0) {
         $minNumArgs = $maxNumArgs;
     }
     if ($numArgs < $minNumArgs) {
         $message = sprintf('Expect at least parameter %d given', $minNumArgs);
         $context = ['minNumArgs' => $minNumArgs, 'numArgs' => $numArgs];
         throw new Exception\IllegalArgumentException($message, -1, $context);
     }
     // ---------------------------------------------------------------------
     $result = [];
     $loopEnd = min($numArgs, $maxNumArgs);
     for ($index = 0; $index < $loopEnd; $index++) {
         $value = $args[$index];
         if (is_null($value)) {
             $result[$index] = $value;
             continue;
         }
         $ztype = $typeArgs[$index];
         switch ($ztype) {
             case IS_RESOURCE:
             case IS_CALLBACK:
                 if ($ztype !== ZendValUtils::getType($value)) {
                     if ($strict) {
                         $message = 'Given parameters is not matched specified type';
                         $context = ['ztype' => $ztype, 'value' => StringUtils::export($value)];
                         throw new Exception\IllegalArgumentException($message, -1, $context);
                     }
                     $value = null;
                 }
                 $result[$index] = $value;
                 break;
             default:
                 ZendValUtils::convertToExplicitType($value, $ztype, $strict);
                 $result[$index] = $value;
                 break;
         }
     }
     return array_pad($result, $maxNumArgs, null);
 }