vendor/symfony/src/Symfony/Component/Console/Output/Output.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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\Output;
       
    13 
       
    14 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
       
    15 use Symfony\Component\Console\Formatter\OutputFormatter;
       
    16 
       
    17 /**
       
    18  * Base class for output classes.
       
    19  *
       
    20  * There is three level of verbosity:
       
    21  *
       
    22  *  * normal: no option passed (normal output - information)
       
    23  *  * verbose: -v (more output - debug)
       
    24  *  * quiet: -q (no output)
       
    25  *
       
    26  * @author Fabien Potencier <fabien@symfony.com>
       
    27  *
       
    28  * @api
       
    29  */
       
    30 abstract class Output implements OutputInterface
       
    31 {
       
    32     private $verbosity;
       
    33     private $formatter;
       
    34 
       
    35     /**
       
    36      * Constructor.
       
    37      *
       
    38      * @param integer                  $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
       
    39      * @param Boolean                  $decorated Whether to decorate messages or not (null for auto-guessing)
       
    40      * @param OutputFormatterInterface $formatter Output formatter instance
       
    41      *
       
    42      * @api
       
    43      */
       
    44     public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
       
    45     {
       
    46         if (null === $formatter) {
       
    47             $formatter = new OutputFormatter();
       
    48         }
       
    49 
       
    50         $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
       
    51         $this->formatter = $formatter;
       
    52         $this->formatter->setDecorated((Boolean) $decorated);
       
    53     }
       
    54 
       
    55     /**
       
    56      * Sets output formatter.
       
    57      *
       
    58      * @param OutputFormatterInterface $formatter
       
    59      *
       
    60      * @api
       
    61      */
       
    62     public function setFormatter(OutputFormatterInterface $formatter)
       
    63     {
       
    64         $this->formatter = $formatter;
       
    65     }
       
    66 
       
    67     /**
       
    68      * Returns current output formatter instance.
       
    69      *
       
    70      * @return  OutputFormatterInterface
       
    71      *
       
    72      * @api
       
    73      */
       
    74     public function getFormatter()
       
    75     {
       
    76         return $this->formatter;
       
    77     }
       
    78 
       
    79     /**
       
    80      * Sets the decorated flag.
       
    81      *
       
    82      * @param Boolean $decorated Whether to decorated the messages or not
       
    83      *
       
    84      * @api
       
    85      */
       
    86     public function setDecorated($decorated)
       
    87     {
       
    88         $this->formatter->setDecorated((Boolean) $decorated);
       
    89     }
       
    90 
       
    91     /**
       
    92      * Gets the decorated flag.
       
    93      *
       
    94      * @return Boolean true if the output will decorate messages, false otherwise
       
    95      *
       
    96      * @api
       
    97      */
       
    98     public function isDecorated()
       
    99     {
       
   100         return $this->formatter->isDecorated();
       
   101     }
       
   102 
       
   103     /**
       
   104      * Sets the verbosity of the output.
       
   105      *
       
   106      * @param integer $level The level of verbosity
       
   107      *
       
   108      * @api
       
   109      */
       
   110     public function setVerbosity($level)
       
   111     {
       
   112         $this->verbosity = (int) $level;
       
   113     }
       
   114 
       
   115     /**
       
   116      * Gets the current verbosity of the output.
       
   117      *
       
   118      * @return integer The current level of verbosity
       
   119      *
       
   120      * @api
       
   121      */
       
   122     public function getVerbosity()
       
   123     {
       
   124         return $this->verbosity;
       
   125     }
       
   126 
       
   127     /**
       
   128      * Writes a message to the output and adds a newline at the end.
       
   129      *
       
   130      * @param string|array $messages The message as an array of lines of a single string
       
   131      * @param integer      $type     The type of output
       
   132      *
       
   133      * @api
       
   134      */
       
   135     public function writeln($messages, $type = 0)
       
   136     {
       
   137         $this->write($messages, true, $type);
       
   138     }
       
   139 
       
   140     /**
       
   141      * Writes a message to the output.
       
   142      *
       
   143      * @param string|array $messages The message as an array of lines of a single string
       
   144      * @param Boolean      $newline  Whether to add a newline or not
       
   145      * @param integer      $type     The type of output
       
   146      *
       
   147      * @throws \InvalidArgumentException When unknown output type is given
       
   148      *
       
   149      * @api
       
   150      */
       
   151     public function write($messages, $newline = false, $type = 0)
       
   152     {
       
   153         if (self::VERBOSITY_QUIET === $this->verbosity) {
       
   154             return;
       
   155         }
       
   156 
       
   157         $messages = (array) $messages;
       
   158 
       
   159         foreach ($messages as $message) {
       
   160             switch ($type) {
       
   161                 case OutputInterface::OUTPUT_NORMAL:
       
   162                     $message = $this->formatter->format($message);
       
   163                     break;
       
   164                 case OutputInterface::OUTPUT_RAW:
       
   165                     break;
       
   166                 case OutputInterface::OUTPUT_PLAIN:
       
   167                     $message = strip_tags($this->formatter->format($message));
       
   168                     break;
       
   169                 default:
       
   170                     throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
       
   171             }
       
   172 
       
   173             $this->doWrite($message, $newline);
       
   174         }
       
   175     }
       
   176 
       
   177     /**
       
   178      * Writes a message to the output.
       
   179      *
       
   180      * @param string  $message A message to write to the output
       
   181      * @param Boolean $newline Whether to add a newline or not
       
   182      */
       
   183     abstract public function doWrite($message, $newline);
       
   184 }