diff --git a/lib/Doctrine/ORM/Tools/Cli/AbstractPrinter.php b/lib/Doctrine/ORM/Tools/Cli/AbstractPrinter.php new file mode 100644 index 000000000..bbe8d3877 --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/AbstractPrinter.php @@ -0,0 +1,104 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli; + +use Doctrine\ORM\Tools\Cli\Style; + +abstract class AbstractPrinter +{ + protected $_stream; + + protected $_maxColumnSize; + + protected $_styles; + + public function __construct($stream = STDOUT) + { + $this->_stream = $stream; + $this->_maxColumnSize = 80; + + $this->_initStyles(); + } + + protected function _initStyles() + { + // Defines base styles + $this->addStyles(array( + 'ERROR' => new Style(), + 'INFO' => new Style(), + 'COMMENT' => new Style(), + 'HEADER' => new Style(), + 'NONE' => new Style(), + )); + } + + public function addStyles($styles) + { + foreach ($styles as $name => $style) { + $this->addStyle($name, $style); + } + } + + public function addStyle($name, Style $style) + { + $this->_styles[strtoupper($name)] = $style; + } + + public function getStyle($name) + { + $name = strtoupper($name); + + return isset($this->_styles[$name]) ? $this->_styles[$name] : null; + } + + /** + * 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 output stream the message, 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 write($message, $style = 'ERROR') + { + $style = is_string($style) ? $this->getStyle($style) : $style; + + fwrite($this->_stream, $this->format($message, $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 $style); +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/AbstractTask.php b/lib/Doctrine/ORM/Tools/Cli/AbstractTask.php new file mode 100644 index 000000000..942bbebaa --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/AbstractTask.php @@ -0,0 +1,58 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli; + +use Doctrine\ORM\Tools\Cli\AbstractPrinter; + +abstract class AbstractTask +{ + protected $_printer; + protected $_arguments; + + public function setPrinter(AbstractPrinter $printer) + { + $this->_printer = $printer; + } + + public function getPrinter() + { + return $this->_printer; + } + + public function setArguments($arguments) + { + $this->_arguments = $arguments; + } + + public function getArguments() + { + return $this->_arguments; + } + + abstract public function extendedHelp(); + + abstract public function basicHelp(); + + abstract public function validate(); + + abstract public function run(); +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/Printer/AnsiColor.php b/lib/Doctrine/ORM/Tools/Cli/Printer/AnsiColor.php new file mode 100644 index 000000000..25fc9c2a9 --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/Printer/AnsiColor.php @@ -0,0 +1,167 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli\Printer; + +use Doctrine\ORM\Tools\Cli\AbstractPrinter, + Doctrine\ORM\Tools\Cli\Style; + +class AnsiColor extends AbstractPrinter +{ + protected function _initStyles() + { + $this->addStyles(array( + 'ERROR' => new Style('RED', 'WHITE', array('BOLD' => true)), + 'INFO' => new Style('GREEN', 'DEFAULT', array('BOLD' => true)), + 'COMMENT' => new Style('YELLOW', 'DEFAULT'), + 'HEADER' => new Style('DEFAULT', 'DEFAULT', array('BOLD' => true)), + 'NONE' => new Style(), + )); + } + + public function format($message, Style $style) + { + if ( ! $this->_supportsColor()) { + return $message; + } + + $str = $this->getForegroundString($style->getForeground()) + . $this->getBackgroundString($style->getBackground()) + . $this->getOptionsString($style->getOptions()); + $styleSet = ($str != ''); + + return $str . $message . ($styleSet ? chr(27) . '[0m' : ''); + } + + public function getForegroundString($foreground) + { + if (empty($foreground)) { + return ''; + } + + $esc = chr(27); + + switch ($foreground) { + case 'BLACK': + return $esc . '[40m'; + case 'RED': + return $esc . '[41m'; + case 'GREEN': + return $esc . '[42m'; + case 'YELLOW': + return $esc . '[43m'; + case 'BLUE': + return $esc . '[44m'; + case 'MAGENTA': + return $esc . '[45m'; + case 'CYAN': + return $esc . '[46m'; + case 'WHITE': + return $esc . '[47m'; + case 'DEFAULT': + default: + return $esc . '[48m'; + } + } + + public function getBackgroundString($background) + { + if (empty($background)) { + return ''; + } + + $esc = chr(27); + + switch ($background) { + case 'BLACK': + return $esc . '[30m'; + case 'RED': + return $esc . '[31m'; + case 'GREEN': + return $esc . '[32m'; + case 'YELLOW': + return $esc . '[33m'; + case 'BLUE': + return $esc . '[34m'; + case 'MAGENTA': + return $esc . '[35m'; + case 'CYAN': + return $esc . '[36m'; + case 'WHITE': + return $esc . '[37m'; + case 'DEFAULT_FGU': + return $esc . '[38m'; + case 'DEFAULT': + default: + return $esc . '[39m'; + } + } + + public function getOptionsString($options) + { + if (empty($options)) { + return ''; + } + + $esc = chr(27); + $str = ''; + + foreach ($options as $name => $value) { + if ($value) { + $name = strtoupper($name); + + switch ($name) { + case 'BOLD': + $str .= $esc . '[1m'; + break; + case 'HALF': + $str .= $esc . '[2m'; + break; + case 'UNDERLINE': + $str .= $esc . '[4m'; + break; + case 'BLINK': + $str .= $esc . '[5m'; + break; + case 'REVERSE': + $str .= $esc . '[7m'; + break; + case 'CONCEAL': + $str .= $esc . '[8m'; + break; + default: + // Ignore unknown option + break; + } + } + } + + return $str; + } + + + private function _supportsColor() + { + return DIRECTORY_SEPARATOR != '\\' && + function_exists('posix_isatty') && + @posix_isatty($this->_stream); + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/Printer/Normal.php b/lib/Doctrine/ORM/Tools/Cli/Printer/Normal.php new file mode 100644 index 000000000..b387e914d --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/Printer/Normal.php @@ -0,0 +1,33 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli\Printer; + +use Doctrine\ORM\Tools\Cli\AbstractPrinter, + Doctrine\ORM\Tools\Cli\Style; + +class Normal extends AbstractPrinter +{ + public function format($message, Style $style) + { + return $message; + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/Style.php b/lib/Doctrine/ORM/Tools/Cli/Style.php new file mode 100644 index 000000000..67f225c4a --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/Style.php @@ -0,0 +1,53 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli; + +class Style +{ + private $_background; + + private $_foreground; + + private $_options = array(); + + public function __construct($foreground = null, $background = null, $options = array()) + { + $this->_foreground = strtoupper($foreground); + $this->_background = strtoupper($background); + $this->_options = $options; + } + + public function getForeground() + { + return $this->_foreground; + } + + public function getBackground() + { + return $this->_background; + } + + public function getOptions() + { + return $this->_options; + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/Task/Help.php b/lib/Doctrine/ORM/Tools/Cli/Task/Help.php new file mode 100644 index 000000000..130a07753 --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/Task/Help.php @@ -0,0 +1,59 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli\Task; + +use Doctrine\ORM\Tools\Cli\AbstractTask; + +class Help extends AbstractTask +{ + public function extendedHelp() + { + $this->getPrinter()->write('help extended help' . PHP_EOL, 'HEADER'); + $this->getPrinter()->write('help extended help' . PHP_EOL, 'ERROR'); + $this->getPrinter()->write('help extended help' . PHP_EOL, 'INFO'); + $this->getPrinter()->write('help extended help' . PHP_EOL, 'COMMENT'); + $this->getPrinter()->write('help extended help' . PHP_EOL, 'NONE'); + } + + public function basicHelp() + { + $this->getPrinter()->write('help basic help' . PHP_EOL, 'HEADER'); + $this->getPrinter()->write('help basic help' . PHP_EOL, 'ERROR'); + $this->getPrinter()->write('help basic help' . PHP_EOL, 'INFO'); + $this->getPrinter()->write('help basic help' . PHP_EOL, 'COMMENT'); + $this->getPrinter()->write('help basic help' . PHP_EOL, 'NONE'); + } + + public function validate() + { + return true; + } + + public function run() + { + $this->getPrinter()->write('help run' . PHP_EOL, 'HEADER'); + $this->getPrinter()->write('help run' . PHP_EOL, 'ERROR'); + $this->getPrinter()->write('help run' . PHP_EOL, 'INFO'); + $this->getPrinter()->write('help run' . PHP_EOL, 'COMMENT'); + $this->getPrinter()->write('help run' . PHP_EOL, 'NONE'); + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/Task/Version.php b/lib/Doctrine/ORM/Tools/Cli/Task/Version.php new file mode 100644 index 000000000..01580013d --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/Task/Version.php @@ -0,0 +1,59 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli\Task; + +use Doctrine\ORM\Tools\Cli\AbstractTask; + +class Version extends AbstractTask +{ + public function extendedHelp() + { + $this->getPrinter()->write('version extended help' . PHP_EOL, 'HEADER'); + $this->getPrinter()->write('version extended help' . PHP_EOL, 'ERROR'); + $this->getPrinter()->write('version extended help' . PHP_EOL, 'INFO'); + $this->getPrinter()->write('version extended help' . PHP_EOL, 'COMMENT'); + $this->getPrinter()->write('version extended help' . PHP_EOL, 'NONE'); + } + + public function basicHelp() + { + $this->getPrinter()->write('version basic help' . PHP_EOL, 'HEADER'); + $this->getPrinter()->write('version basic help' . PHP_EOL, 'ERROR'); + $this->getPrinter()->write('version basic help' . PHP_EOL, 'INFO'); + $this->getPrinter()->write('version basic help' . PHP_EOL, 'COMMENT'); + $this->getPrinter()->write('version basic help' . PHP_EOL, 'NONE'); + } + + public function validate() + { + return true; + } + + public function run() + { + $this->getPrinter()->write('version run' . PHP_EOL, 'HEADER'); + $this->getPrinter()->write('version run' . PHP_EOL, 'ERROR'); + $this->getPrinter()->write('version run' . PHP_EOL, 'INFO'); + $this->getPrinter()->write('version run' . PHP_EOL, 'COMMENT'); + $this->getPrinter()->write('version run' . PHP_EOL, 'NONE'); + } +} \ No newline at end of file