. */ namespace Doctrine\Common\Cli\Printers; use Doctrine\Common\Cli\Style; /** * CLI Output Printer. * Abstract class responsable to provide basic methods to support output * styling and excerpt limited by output margin. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.0 * @version $Revision$ * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel */ abstract class AbstractPrinter { /** * @var resource Output Stream */ protected $_stream; /** * @var integer Maximum column size */ protected $_maxColumnSize; /** * @var array Array of Styles */ protected $_styles; /** * Creates an instance of Printer * * @param resource $stream Output Stream */ public function __construct($stream = STDOUT) { $this->_stream = $stream; $this->_maxColumnSize = 80; $this->_initStyles(); } /** * Initializes Printer Styles * */ protected function _initStyles() { // Defines base styles $this->addStyles(array( 'ERROR' => new Style(), 'INFO' => new Style(), 'COMMENT' => new Style(), 'HEADER' => new Style(), 'NONE' => new Style(), )); } /** * Add a collection of styles to the Printer. * To include them, just call the method with the following structure: * * [php] * $printer->addStyles(array( * 'ERROR' => new Style('BLACK', 'DEFAULT', array('BOLD' => true)), * ... * )); * * @param array $tasks CLI Tasks to be included */ public function addStyles($styles) { foreach ($styles as $name => $style) { $this->addStyle($name, $style); } } /** * Add a single Style to Printer. * Example of inclusion to support a new Style: * * [php] * $printer->addStyle('ERROR', new Style('BLACK', 'DEFAULT', array('BOLD' => true))); * * @param string $name Style name * @param Style $style Style instance */ public function addStyle($name, Style $style) { $this->_styles[strtoupper($name)] = $style; } /** * Retrieves a defined Style. * * @return Style */ public function getStyle($name) { if (is_string($name)) { $name = strtoupper($name); return isset($this->_styles[$name]) ? $this->_styles[$name] : null; } else { return $name; } } /** * Sets the maximum column size (defines the CLI margin). * * @param integer $maxColumnSize The maximum column size for a message */ public function setMaxColumnSize($maxColumnSize) { $this->_maxColumnSize = $maxColumnSize; } /** * Writes to the output stream. * * @param string $message Message to be outputted */ public function output($message) { fwrite($this->_stream, $message); return $this; } /** * Formats message applying the defined style and writes to the output stream. * * @param string $message Message to be outputted * @param mixed $style Optional style to be applied in message */ public function write($message, $style = 'NONE') { $this->output($this->format($message, $style)); return $this; } /** * Writes a line to the output stream, formatting it by applying the defined style. * * @param string $message Message to be outputted * @param mixed $style Optional style to be applied in message */ public function writeln($message, $style = 'NONE') { return $this->write($message . PHP_EOL, $style); } /** * Formats the given message with the defined style. * * @param string $message Message to be formatted * @param mixed $style Style to be applied in message * @return string Formatted message */ abstract public function format($message, $style); }