Jelajahi Sumber

merged branch laszlokorte/console_formatter (PR #1420)

Commits
-------

6b72cda removed whitespace
282a493 moved the constant from the interface back into the class
7ec7fbe fixed inteface name
9a82924 [Console] Extract the OutputFormatter's regular expression into the inteface.

Discussion
----------

[Console] Expose the OutputFormatter's regular expression

In my ConsoleBundle(https://github.com/CoreSphere/ConsoleBundle) I needed to decorate the OutputFormatter to enable html escaping while allow to apply styles.

For that I need to reuse the regular expression used in the original OutputFormatter. I think it would be cleaner exposing it than duplicating it.
(https://github.com/CoreSphere/ConsoleBundle/blob/master/Formatter/HtmlOutputFormatterDecorator.php#L64)

---------------------------------------------------------------------------

by fabpot at 2011/06/23 12:24:43 -0700

OutputFormatter is just one implementation of the interface, so the constant cannot be defined in the interface.

---------------------------------------------------------------------------

by laszlokorte at 2011/06/23 13:01:46 -0700

I made the false assumption all Formatters would use the same syntax for defining style. I moved it back into the class.
Fabien Potencier 14 tahun lalu
induk
melakukan
87b121510b

+ 6 - 1
src/Symfony/Component/Console/Formatter/OutputFormatter.php

@@ -20,6 +20,11 @@ namespace Symfony\Component\Console\Formatter;
  */
 class OutputFormatter implements OutputFormatterInterface
 {
+    /**
+     * The pattern to phrase the format.
+     */
+    const FORMAT_PATTERN = '#<([a-z][a-z0-9_=;-]+)>(.*?)</\\1?>#is';
+
     private $decorated;
     private $styles = array();
 
@@ -125,7 +130,7 @@ class OutputFormatter implements OutputFormatterInterface
      */
     public function format($message)
     {
-        return preg_replace_callback('#<([a-z][a-z0-9_=;-]+)>(.*?)</\\1?>#is', array($this, 'replaceStyle'), $message);
+        return preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
     }
 
     /**