From 038e6cadfbc44f71c2155245861661db3e81e3b0 Mon Sep 17 00:00:00 2001 From: guilhermeblanco Date: Sat, 17 Oct 2009 01:57:50 +0000 Subject: [PATCH] [2.0] Fix CLI documentation of schema-tool task --- lib/Doctrine/Common/Annotations/Parser.php | 10 +- .../Tools/Cli/Printers/AbstractPrinter.php | 153 +++++++++++++++++- .../ORM/Tools/Cli/Tasks/SchemaToolTask.php | 11 +- 3 files changed, 157 insertions(+), 17 deletions(-) diff --git a/lib/Doctrine/Common/Annotations/Parser.php b/lib/Doctrine/Common/Annotations/Parser.php index 854e8018e..5babb1dcc 100644 --- a/lib/Doctrine/Common/Annotations/Parser.php +++ b/lib/Doctrine/Common/Annotations/Parser.php @@ -201,7 +201,7 @@ class Parser } /** - * Annotation ::= "@" AnnotationName [ "(" [Values] ")" ] + * Annotation ::= "@" AnnotationName ["(" [Values] ")"] * AnnotationName ::= QualifiedName | SimpleName * QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName * NameSpacePart ::= identifier @@ -262,7 +262,7 @@ class Parser } /** - * Values ::= Value {"," Value}* + * Values ::= Array | Value {"," Value}* * * @return array */ @@ -320,7 +320,7 @@ class Parser } /** - * PlainValue ::= integer | string | float | Array | Annotation + * PlainValue ::= integer | string | float | boolean | Array | Annotation * * @return mixed */ @@ -410,7 +410,7 @@ class Parser /** * ArrayEntry ::= Value | KeyValuePair - * KeyValuePair ::= Key "=" Value + * KeyValuePair ::= Key "=" PlainValue * Key ::= string | integer * * @return array @@ -429,7 +429,7 @@ class Parser $key = $this->_lexer->token['value']; $this->match('='); - return array($key => $this->Value()); + return array($key => $this->PlainValue()); } return array($this->Value()); diff --git a/lib/Doctrine/ORM/Tools/Cli/Printers/AbstractPrinter.php b/lib/Doctrine/ORM/Tools/Cli/Printers/AbstractPrinter.php index dacd3c415..c557cecf4 100644 --- a/lib/Doctrine/ORM/Tools/Cli/Printers/AbstractPrinter.php +++ b/lib/Doctrine/ORM/Tools/Cli/Printers/AbstractPrinter.php @@ -1,4 +1,4 @@ -_stream = $stream; - $this->_maxColumnSize = 80; + $this->setMaxColumnSize(0); $this->_initStyles(); } @@ -74,10 +79,14 @@ abstract class AbstractPrinter { // Defines base styles $this->addStyles(array( + 'HEADER' => new Style(), 'ERROR' => new Style(), + 'WARNING' => new Style(), + 'KEYWORD' => new Style(), + 'REQ_ARG' => new Style(), + 'OPT_ARG' => new Style(), 'INFO' => new Style(), 'COMMENT' => new Style(), - 'HEADER' => new Style(), 'NONE' => new Style(), )); } @@ -134,11 +143,13 @@ abstract class AbstractPrinter /** * Sets the maximum column size (defines the CLI margin). * - * @param integer $maxColumnSize The maximum column size for a message + * @param integer $maxColumnSize The maximum column size for a message. + * Must be higher than 25 and assigns default + * value (if invalid) to 80 columns. */ public function setMaxColumnSize($maxColumnSize) { - $this->_maxColumnSize = $maxColumnSize; + $this->_maxColumnSize = ($maxColumnSize > 25) ? $maxColumnSize : 80; } /** @@ -165,6 +176,136 @@ abstract class AbstractPrinter return $this->write($message . PHP_EOL, $style); } + public function writeTaskDocumentation($taskName, $arguments = array(), $description, $options = array()) + { + // Writting task name + $this->write('Task: ', 'HEADER')->writeln($taskName, 'KEYWORD'); + + // Synopsis + $this->writeln('Synopsis:', 'HEADER'); + $this->writeSynopsis($taskName, $arguments); + + // We need to split the description according to maximum column size + $this->writeln('Description:', 'HEADER'); + $this->writeDescription($description); + + // Find largest length option name (it is mandatory for tab spacing) + $lengths = array_map(create_function('$v', 'return strlen($v["name"]);'), $options); + sort($lengths, SORT_NUMERIC); + + $highestLength = end($lengths); + $maxTabs = ceil($highestLength / self::TAB_LENGTH); + + // Options (required + optional arguments) + $this->writeln('Options:', 'HEADER'); + + for ($i = 0, $len = count($options); $i < $len; $i++) { + $this->writeOption($options[$i], $maxTabs, $highestLength); + + if ($i != $len - 1) { + $this->write(PHP_EOL); + } + } + } + + public function writeSynopsis($taskName, $arguments = array()) + { + // Required arguments + $requiredArguments = ''; + + if (isset($arguments['required'])) { + $requiredArguments = ' ' . ((is_array($arguments['required'])) + ? implode(' ', $arguments['required']) : $arguments['required']); + } + + // Optional arguments + $optionalArguments = ''; + + if (isset($arguments['optional'])) { + $optionalArguments = ' ' . ((is_array($arguments['optional'])) + ? implode(' ', $arguments['optional']) : $arguments['optional']); + } + + $this->write($taskName, 'KEYWORD'); + + if (($l = strlen($taskName . $requiredArguments)) > $this->_maxColumnSize) { + $this->write(PHP_EOL); + } + + $this->write(' ' . $requiredArguments, 'REQ_ARG'); + + if (($l + strlen($optionalArguments)) > $this->_maxColumnSize) { + $this->write(PHP_EOL); + } + + $this->write(' ' . $optionalArguments, 'OPT_ARG'); + + $this->write(PHP_EOL); + } + + protected function writeDescription($description) + { + $descriptionLength = strlen($description); + $startPos = 0; + $maxSize = $endPos = $this->_maxColumnSize; + + // Description + while ($startPos < $descriptionLength) { + $descriptionPart = trim(substr($description, $startPos, $endPos + 1)); + $endPos = (($l = strlen($descriptionPart)) > $maxSize) + ? strrpos($descriptionPart, ' ') : $l; + $endPos = ($endPos === false) ? strlen($description) : $endPos + 1; + + // Write description line + $this->writeln(trim(substr($description, $startPos, $endPos))); + + $startPos += $endPos; + $endPos = $maxSize; + } + } + + protected function writeOption($option, $maxTabs, $highestLength) + { + // Option name + $this->write( + $option['name'], + (isset($option['required']) && $option['required']) ? 'REQ_ARG' : 'OPT_ARG' + ); + + // Tab spacing + $optionLength = strlen($option['name']); + $tabs = floor($optionLength / self::TAB_LENGTH); + $decrementer = 0; + + //echo '[' .$tabs. ']'; + + if (($optionLength % self::TAB_LENGTH != 0)) { + $decrementer = 1; + //$tabs--; + } + + $this->write(str_repeat(" ", ($maxTabs - $tabs) * self::TAB_LENGTH)); + + // Description + $descriptionLength = strlen($option['description']); + + $startPos = 0; + $maxSize = $endPos = $this->_maxColumnSize - ($maxTabs * self::TAB_LENGTH); + + while ($startPos < $descriptionLength) { + $descriptionPart = trim(substr($option['description'], $startPos, $endPos + 1)); + $endPos = (($l = strlen($descriptionPart)) >= $maxSize) + ? strrpos($descriptionPart, ' ') : $l; + $endPos = ($endPos === false) ? strlen($option['description']) : $endPos + 1; + $descriptionLine = (($startPos != 0) ? str_repeat(" ", $maxTabs * self::TAB_LENGTH) : '') + . trim(substr($option['description'], $startPos, $endPos)); + $this->writeln($descriptionLine); + + $startPos += $endPos; + $endPos = $maxSize; + } + } + /** * Formats the given message with the defined style. * diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/SchemaToolTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/SchemaToolTask.php index 985018d23..7ca1d7040 100644 --- a/lib/Doctrine/ORM/Tools/Cli/Tasks/SchemaToolTask.php +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/SchemaToolTask.php @@ -68,14 +68,14 @@ class SchemaToolTask extends AbstractTask ->writeln("\t\tUpdates the schema in EntityManager (update tables on Database)") ->writeln("\t\t\tIf defined, --create and --drop can not be requested on same task") ->write(PHP_EOL) + ->write('--re-create', 'REQ_ARG') + ->writeln("\t\tRuns --drop then --create to re-create the database.") + ->write(PHP_EOL) ->write('--dump-sql', 'OPT_ARG') ->writeln("\t\tInstead of try to apply generated SQLs into EntityManager, output them.") ->write(PHP_EOL) ->write('--class-dir=', 'OPT_ARG') ->writeln("\tOptional class directory to fetch for Entities.") - ->write(PHP_EOL) - ->write('--re-create', 'OPT_ARG') - ->writeln("\t\tRuns --drop then --create to re-create the database.") ->write(PHP_EOL); } @@ -90,9 +90,8 @@ class SchemaToolTask extends AbstractTask private function _writeSynopsis($printer) { $printer->write('schema-tool', 'KEYWORD') - ->write(' (--create | --drop | --update)', 'REQ_ARG') - ->write(' [--dump-sql] [--class-dir=]', 'OPT_ARG') - ->writeln(' [--re-create]', 'OPT_ARG'); + ->write(' (--create | --drop | --update | --re-create)', 'REQ_ARG') + ->writeln(' [--dump-sql] [--class-dir=]', 'OPT_ARG'); } /**