Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • OutputFormatter
  • OutputFormatterStyle
  • OutputFormatterStyleStack

Interfaces

  • OutputFormatterInterface
  • OutputFormatterStyleInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: /*
  4:  * This file is part of the Symfony package.
  5:  *
  6:  * (c) Fabien Potencier <fabien@symfony.com>
  7:  *
  8:  * For the full copyright and license information, please view the LICENSE
  9:  * file that was distributed with this source code.
 10:  */
 11: 
 12: namespace Symfony\Component\Console\Formatter;
 13: 
 14: /**
 15:  * Formatter class for console output.
 16:  *
 17:  * @author Konstantin Kudryashov <ever.zet@gmail.com>
 18:  *
 19:  * @api
 20:  */
 21: class OutputFormatter implements OutputFormatterInterface
 22: {
 23:     /**
 24:      * The pattern to phrase the format.
 25:      */
 26:     const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>((?: [^<\\\\]+ | (?!<(?:/?[a-z]|/>)). | .(?<=\\\\<) )*)#isx';
 27: 
 28:     private $decorated;
 29:     private $styles = array();
 30:     private $styleStack;
 31: 
 32:     /**
 33:      * Escapes "<" special char in given text.
 34:      *
 35:      * @param string $text Text to escape
 36:      *
 37:      * @return string Escaped text
 38:      */
 39:     public static function escape($text)
 40:     {
 41:         return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
 42:     }
 43: 
 44:     /**
 45:      * Initializes console output formatter.
 46:      *
 47:      * @param Boolean          $decorated Whether this formatter should actually decorate strings
 48:      * @param FormatterStyle[] $styles    Array of "name => FormatterStyle" instances
 49:      *
 50:      * @api
 51:      */
 52:     public function __construct($decorated = null, array $styles = array())
 53:     {
 54:         $this->decorated = (Boolean) $decorated;
 55: 
 56:         $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
 57:         $this->setStyle('info', new OutputFormatterStyle('green'));
 58:         $this->setStyle('comment', new OutputFormatterStyle('yellow'));
 59:         $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
 60: 
 61:         foreach ($styles as $name => $style) {
 62:             $this->setStyle($name, $style);
 63:         }
 64: 
 65:         $this->styleStack = new OutputFormatterStyleStack();
 66:     }
 67: 
 68:     /**
 69:      * Sets the decorated flag.
 70:      *
 71:      * @param Boolean $decorated Whether to decorate the messages or not
 72:      *
 73:      * @api
 74:      */
 75:     public function setDecorated($decorated)
 76:     {
 77:         $this->decorated = (Boolean) $decorated;
 78:     }
 79: 
 80:     /**
 81:      * Gets the decorated flag.
 82:      *
 83:      * @return Boolean true if the output will decorate messages, false otherwise
 84:      *
 85:      * @api
 86:      */
 87:     public function isDecorated()
 88:     {
 89:         return $this->decorated;
 90:     }
 91: 
 92:     /**
 93:      * Sets a new style.
 94:      *
 95:      * @param string                        $name  The style name
 96:      * @param OutputFormatterStyleInterface $style The style instance
 97:      *
 98:      * @api
 99:      */
100:     public function setStyle($name, OutputFormatterStyleInterface $style)
101:     {
102:         $this->styles[strtolower($name)] = $style;
103:     }
104: 
105:     /**
106:      * Checks if output formatter has style with specified name.
107:      *
108:      * @param string $name
109:      *
110:      * @return Boolean
111:      *
112:      * @api
113:      */
114:     public function hasStyle($name)
115:     {
116:         return isset($this->styles[strtolower($name)]);
117:     }
118: 
119:     /**
120:      * Gets style options from style with specified name.
121:      *
122:      * @param string $name
123:      *
124:      * @return OutputFormatterStyleInterface
125:      *
126:      * @throws \InvalidArgumentException When style isn't defined
127:      *
128:      * @api
129:      */
130:     public function getStyle($name)
131:     {
132:         if (!$this->hasStyle($name)) {
133:             throw new \InvalidArgumentException('Undefined style: '.$name);
134:         }
135: 
136:         return $this->styles[strtolower($name)];
137:     }
138: 
139:     /**
140:      * Formats a message according to the given styles.
141:      *
142:      * @param string $message The message to style
143:      *
144:      * @return string The styled message
145:      *
146:      * @api
147:      */
148:     public function format($message)
149:     {
150:         $message = preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
151: 
152:         return str_replace('\\<', '<', $message);
153:     }
154: 
155:     /**
156:      * @return OutputFormatterStyleStack
157:      */
158:     public function getStyleStack()
159:     {
160:         return $this->styleStack;
161:     }
162: 
163:     /**
164:      * Replaces style of the output.
165:      *
166:      * @param array $match
167:      *
168:      * @return string The replaced style
169:      */
170:     private function replaceStyle($match)
171:     {
172:         // we got "\<" escaped char
173:         if ('\\' === $match[1]) {
174:             return $this->applyCurrentStyle($match[0]);
175:         }
176: 
177:         if ('' === $match[3]) {
178:             if ('/' === $match[2]) {
179:                 // we got "</>" tag
180:                 $this->styleStack->pop();
181: 
182:                 return $this->applyCurrentStyle($match[4]);
183:             }
184: 
185:             // we got "<>" tag
186:             return '<>'.$this->applyCurrentStyle($match[4]);
187:         }
188: 
189:         if (isset($this->styles[strtolower($match[3])])) {
190:             $style = $this->styles[strtolower($match[3])];
191:         } else {
192:             $style = $this->createStyleFromString($match[3]);
193: 
194:             if (false === $style) {
195:                 return $this->applyCurrentStyle($match[0]);
196:             }
197:         }
198: 
199:         if ('/' === $match[2]) {
200:             $this->styleStack->pop($style);
201:         } else {
202:             $this->styleStack->push($style);
203:         }
204: 
205:         return $this->applyCurrentStyle($match[4]);
206:     }
207: 
208:     /**
209:      * Tries to create new style instance from string.
210:      *
211:      * @param string $string
212:      *
213:      * @return OutputFormatterStyle|Boolean false if string is not format string
214:      */
215:     private function createStyleFromString($string)
216:     {
217:         if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
218:             return false;
219:         }
220: 
221:         $style = new OutputFormatterStyle();
222:         foreach ($matches as $match) {
223:             array_shift($match);
224: 
225:             if ('fg' == $match[0]) {
226:                 $style->setForeground($match[1]);
227:             } elseif ('bg' == $match[0]) {
228:                 $style->setBackground($match[1]);
229:             } else {
230:                 $style->setOption($match[1]);
231:             }
232:         }
233: 
234:         return $style;
235:     }
236: 
237:     /**
238:      * Applies current style from stack to text, if must be applied.
239:      *
240:      * @param string $text Input text
241:      *
242:      * @return string string Styled text
243:      */
244:     private function applyCurrentStyle($text)
245:     {
246:         return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
247:     }
248: }
249: 
php-coveralls API documentation generated by ApiGen 2.8.0