예제 #1
0
 /**
  * Handles rendering strings. If extra scalar arguments are given after the `$msg`
  * the string will be rendered with `sprintf`. If the second argument is an `array`
  * then each key in the array will be the placeholder name. Placeholders are of the
  * format {:key}.
  *
  * @param string   $msg  The message to render.
  * @param mixed    ...   Either scalar arguments or a single array argument.
  * @return string  The rendered string.
  */
 public static function render($msg)
 {
     $args = func_get_args();
     // No string replacement is needed
     if (count($args) == 1) {
         return Colors::colorize($msg);
     }
     // If the first argument is not an array just pass to sprintf
     if (!is_array($args[1])) {
         // Colorize the message first so sprintf doesn't bitch at us
         $args[0] = Colors::colorize($args[0]);
         return call_user_func_array('sprintf', $args);
     }
     // Here we do named replacement so formatting strings are more understandable
     foreach ($args[1] as $key => $value) {
         $msg = str_replace('{:' . $key . '}', $value, $msg);
     }
     return Colors::colorize($msg);
 }
예제 #2
0
 /**
  * Formats $text according to the formater's options
  * 
  * @param string $text
  */
 public function format($text)
 {
     $lines = explode("\n", $text);
     foreach ($lines as &$line) {
         $line = (string) $this->quote . str_repeat(' ', $this->indent * $this->indentWidth) . $line;
     }
     return Colors::colorize(implode("\n", $lines), $this->fgColor, $this->bgColor);
 }