1
0
mirror of synced 2025-02-21 14:43:14 +03:00

[2.0] Added docblocks

This commit is contained in:
guilhermeblanco 2009-08-25 04:46:46 +00:00
parent fd89892cc9
commit 356887c893
8 changed files with 370 additions and 24 deletions

View File

@ -22,6 +22,8 @@
namespace Doctrine\ORM\Tools; namespace Doctrine\ORM\Tools;
use Doctrine\Common\Util\Inflector, use Doctrine\Common\Util\Inflector,
Doctrine\ORM\Tools\Cli\AbstractPrinter,
Doctrine\ORM\Tools\Cli\AbstractTask,
Doctrine\ORM\Tools\Cli\Printer; Doctrine\ORM\Tools\Cli\Printer;
/** /**
@ -30,7 +32,7 @@ use Doctrine\Common\Util\Inflector,
* To include a new Task support, create a task: * To include a new Task support, create a task:
* *
* [php] * [php]
* class MyProject\Tools\Cli\Tasks\MyTask extends Doctrine\ORM\Tools\Cli\Task { * class MyProject\Tools\Cli\Tasks\MyTask extends Doctrine\ORM\Tools\Cli\AbstractTask {
* public function run(); * public function run();
* public function basicHelp(); * public function basicHelp();
* public function extendedHelp(); * public function extendedHelp();
@ -59,7 +61,12 @@ use Doctrine\Common\Util\Inflector,
class Cli class Cli
{ {
/** /**
* @var Cli\AbstractPrinter CLI Printer instance * @var ORM\Configuration Configuration
*/
//private $_configuration = null;
/**
* @var AbstractPrinter CLI Printer instance
*/ */
private $_printer = null; private $_printer = null;
@ -68,8 +75,12 @@ class Cli
*/ */
private $_tasks = array(); private $_tasks = array();
/**
public function __construct($printer = null) * The CLI processor of tasks
*
* @param AbstractPrinter $printer CLI Output printer
*/
public function __construct(AbstractPrinter $printer = null)
{ {
//$this->_printer = new Printer\Normal(); //$this->_printer = new Printer\Normal();
$this->_printer = $printer ?: new Printer\AnsiColor(); $this->_printer = $printer ?: new Printer\AnsiColor();
@ -83,6 +94,18 @@ class Cli
)); ));
} }
/**
* Add a collection of tasks to the CLI.
* To include them, just call the method with the following structure:
*
* [php]
* $cli->addTasks(array(
* 'my-custom-task' => 'MyProject\Cli\Tasks\MyCustomTask',
* ...
* ));
*
* @param array $tasks CLI Tasks to be included
*/
public function addTasks($tasks) public function addTasks($tasks)
{ {
foreach ($tasks as $name => $class) { foreach ($tasks as $name => $class) {
@ -90,6 +113,16 @@ class Cli
} }
} }
/**
* Add a single task to CLI.
* Example of inclusion support to a single task:
*
* [php]
* $cli->addTask('my-custom-task', 'MyProject\Cli\Tasks\MyCustomTask');
*
* @param string $name CLI Task name
* @param string $class CLI Task class (FQCN - Fully Qualified Class Name)
*/
public function addTask($name, $class) public function addTask($name, $class)
{ {
// Convert $name into a class equivalent // Convert $name into a class equivalent
@ -99,6 +132,12 @@ class Cli
$this->_tasks[$name] = $class; $this->_tasks[$name] = $class;
} }
/**
* Processor of CLI Tasks. Handles multiple task calls, instantiate
* respective classes and run them.
*
* @param array $args CLI Arguments
*/
public function run($args = array()) public function run($args = array())
{ {
// Remove script file argument // Remove script file argument
@ -121,18 +160,16 @@ class Cli
$taskName = $this->_processTaskName($taskData['name']); $taskName = $this->_processTaskName($taskData['name']);
$taskArguments = $taskData['args']; $taskArguments = $taskData['args'];
// Always include supported Tasks as argument
$taskArguments['availableTasks'] = $this->_tasks;
// Check if task exists // Check if task exists
if (isset($this->_tasks[$taskName]) && class_exists($this->_tasks[$taskName], true)) { if (isset($this->_tasks[$taskName]) && class_exists($this->_tasks[$taskName], true)) {
// Instantiate and execute the task // Instantiate and execute the task
$task = new $this->_tasks[$taskName](); $task = new $this->_tasks[$taskName]();
$task->setAvailableTasks($this->_tasks);
$task->setPrinter($this->_printer); $task->setPrinter($this->_printer);
$task->setArguments($taskArguments); $task->setArguments($taskArguments);
if (isset($taskArguments['help']) && $taskArguments['help']) { if (isset($taskArguments['help']) && $taskArguments['help']) {
$task->extendedHelp(); // User explicitly asked for task help $task->extendedHelp(); // User explicitly asked for help option
} else if ($this->_isTaskValid($task)) { } else if ($this->_isTaskValid($task)) {
$task->run(); $task->run();
} else { } else {
@ -248,9 +285,15 @@ class Cli
return $preparedArgs; return $preparedArgs;
} }
private function _isTaskValid($task) /**
* Checks if CLI Task is valid based on given arguments.
*
* @param AbstractTask $task CLI Task instance
*/
private function _isTaskValid(AbstractTask $task)
{ {
// TODO: Should we check for required and optional arguments here? // TODO: Should we change the behavior and check for
// required and optional arguments here?
return $task->validate(); return $task->validate();
} }
} }

View File

@ -21,14 +21,41 @@
namespace Doctrine\ORM\Tools\Cli; namespace Doctrine\ORM\Tools\Cli;
/**
* 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 <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
abstract class AbstractPrinter abstract class AbstractPrinter
{ {
/**
* @var resource Output Stream
*/
protected $_stream; protected $_stream;
/**
* @var integer Maximum column size
*/
protected $_maxColumnSize; protected $_maxColumnSize;
/**
* @var array Array of Styles
*/
protected $_styles; protected $_styles;
/**
* Creates an instance of Printer
*
* @param resource $stream Output Stream
*/
public function __construct($stream = STDOUT) public function __construct($stream = STDOUT)
{ {
$this->_stream = $stream; $this->_stream = $stream;
@ -37,6 +64,10 @@ abstract class AbstractPrinter
$this->_initStyles(); $this->_initStyles();
} }
/**
* Initializes Printer Styles
*
*/
protected function _initStyles() protected function _initStyles()
{ {
// Defines base styles // Defines base styles
@ -49,6 +80,18 @@ abstract class AbstractPrinter
)); ));
} }
/**
* 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) public function addStyles($styles)
{ {
foreach ($styles as $name => $style) { foreach ($styles as $name => $style) {
@ -56,11 +99,26 @@ abstract class AbstractPrinter
} }
} }
/**
* 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) public function addStyle($name, Style $style)
{ {
$this->_styles[strtoupper($name)] = $style; $this->_styles[strtoupper($name)] = $style;
} }
/**
* Retrieves a defined Style.
*
* @return Style
*/
public function getStyle($name) public function getStyle($name)
{ {
$name = strtoupper($name); $name = strtoupper($name);

View File

@ -21,36 +21,140 @@
namespace Doctrine\ORM\Tools\Cli; namespace Doctrine\ORM\Tools\Cli;
/**
* CLI Task.
* Provides basic methods and requires implementation of methods that
* each task should implement in order to correctly work.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
abstract class AbstractTask abstract class AbstractTask
{ {
/**
* @var AbstractPrinter CLI Output Printer
*/
protected $_printer; protected $_printer;
/**
* @var array CLI argument options
*/
protected $_arguments; protected $_arguments;
/**
* @var array Available CLI tasks
*/
protected $_availableTasks;
/**
* Defines a CLI Output Printer
*
* @param AbstractPrinter CLI Output Printer
*/
public function setPrinter(AbstractPrinter $printer) public function setPrinter(AbstractPrinter $printer)
{ {
$this->_printer = $printer; $this->_printer = $printer;
} }
/**
* Retrieves currently used CLI Output Printer
*
* @return AbstractPrinter
*/
public function getPrinter() public function getPrinter()
{ {
return $this->_printer; return $this->_printer;
} }
/**
* Defines the CLI arguments
*
* @param array CLI argument options
*/
public function setArguments($arguments) public function setArguments($arguments)
{ {
$this->_arguments = $arguments; $this->_arguments = $arguments;
} }
/**
* Retrieves current CLI arguments
*
* @return array
*/
public function getArguments() public function getArguments()
{ {
return $this->_arguments; return $this->_arguments;
} }
/**
* Defines the available CLI tasks
*
* @param array Available CLI tasks
*/
public function setAvailableTasks($availableTasks)
{
$this->_availableTasks = $availableTasks;
}
/**
* Retrieves the available CLI tasks
*
* @return array
*/
public function getAvailableTasks()
{
return $this->_availableTasks;
}
/**
* Expose to CLI Output Printer the extended help of the given task.
* This means it should detail all parameters, options and the meaning
* of each one.
* This method is executed when user types in CLI the following command:
*
* [bash]
* ./doctrine task --help
*
*/
abstract public function extendedHelp(); abstract public function extendedHelp();
/**
* Expose to CLI Output Printer the basic help of the given task.
* This means it should only expose the basic task call. It is also
* executed when user calls the global help; so this means it should
* not pollute the Printer.
* Basic help exposure is displayed when task does not pass the validate
* (which means when user does not type the required options or when given
* options are invalid, ie: invalid option), or when user requests to have
* description of all available tasks.
* This method is executed when user uses the following commands:
*
* [bash]
* ./doctrine task --invalid-option
* ./doctrine --help
*
*/
abstract public function basicHelp(); abstract public function basicHelp();
/**
* Assures the given arguments matches with required/optional ones.
* This method should be used to introspect arguments to check for
* missing required arguments and also for invalid defined options.
*
* @return boolean
*/
abstract public function validate(); abstract public function validate();
/**
* Safely execution of task.
* Each CLI task should implement this as normal flow execution of
* what is supposed to do.
*
*/
abstract public function run(); abstract public function run();
} }

View File

@ -24,42 +24,65 @@ namespace Doctrine\ORM\Tools\Cli\Printer;
use Doctrine\ORM\Tools\Cli\AbstractPrinter, use Doctrine\ORM\Tools\Cli\AbstractPrinter,
Doctrine\ORM\Tools\Cli\Style; Doctrine\ORM\Tools\Cli\Style;
/**
* CLI Output Printer for ANSI Color terminal
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class AnsiColor extends AbstractPrinter class AnsiColor extends AbstractPrinter
{ {
/**
* @inheritdoc
*/
protected function _initStyles() protected function _initStyles()
{ {
$this->addStyles(array( $this->addStyles(array(
'ERROR' => new Style('RED', 'WHITE', array('BOLD' => true)), 'ERROR' => new Style('WHITE', 'RED', array('BOLD' => true)),
'INFO' => new Style('GREEN', 'DEFAULT', array('BOLD' => true)), 'INFO' => new Style('GREEN', 'DEFAULT', array('BOLD' => true)),
'COMMENT' => new Style('YELLOW', 'DEFAULT'), 'COMMENT' => new Style('DEFAULT', 'YELLOW'),
'HEADER' => new Style('DEFAULT', 'DEFAULT', array('BOLD' => true)), 'HEADER' => new Style('DEFAULT', 'DEFAULT', array('BOLD' => true)),
'NONE' => new Style(), 'NONE' => new Style(),
)); ));
} }
/**
* @inheritdoc
*/
public function format($message, Style $style) public function format($message, Style $style)
{ {
if ( ! $this->_supportsColor()) { if ( ! $this->_supportsColor()) {
return $message; return $message;
} }
$str = $this->getForegroundString($style->getForeground()) $str = $this->_getForegroundString($style->getForeground())
. $this->getBackgroundString($style->getBackground()) . $this->_getBackgroundString($style->getBackground())
. $this->getOptionsString($style->getOptions()); . $this->_getOptionsString($style->getOptions());
$styleSet = ($str != ''); $styleSet = ($str != '');
return $str . $message . ($styleSet ? chr(27) . '[0m' : ''); return $str . $message . ($styleSet ? chr(27) . '[0m' : '');
} }
public function getForegroundString($foreground) /**
* Retrieves the ANSI string representation of requested color name
*
* @param string $background Background color name
* @return string
*/
protected function _getBackgroundString($background)
{ {
if (empty($foreground)) { if (empty($background)) {
return ''; return '';
} }
$esc = chr(27); $esc = chr(27);
switch ($foreground) { switch ($background) {
case 'BLACK': case 'BLACK':
return $esc . '[40m'; return $esc . '[40m';
case 'RED': case 'RED':
@ -82,15 +105,21 @@ class AnsiColor extends AbstractPrinter
} }
} }
public function getBackgroundString($background) /**
* Retrieves the ANSI string representation of requested color name
*
* @param string $foreground Foreground color name
* @return string
*/
protected function _getForegroundString($foreground)
{ {
if (empty($background)) { if (empty($foreground)) {
return ''; return '';
} }
$esc = chr(27); $esc = chr(27);
switch ($background) { switch ($foreground) {
case 'BLACK': case 'BLACK':
return $esc . '[30m'; return $esc . '[30m';
case 'RED': case 'RED':
@ -115,7 +144,13 @@ class AnsiColor extends AbstractPrinter
} }
} }
public function getOptionsString($options) /**
* Retrieves the ANSI string representation of requested options
*
* @param array $options Options
* @return string
*/
protected function _getOptionsString($options)
{ {
if (empty($options)) { if (empty($options)) {
return ''; return '';
@ -157,7 +192,11 @@ class AnsiColor extends AbstractPrinter
return $str; return $str;
} }
/**
* Checks if the current Output Stream supports ANSI Colors
*
* @return boolean
*/
private function _supportsColor() private function _supportsColor()
{ {
return DIRECTORY_SEPARATOR != '\\' && return DIRECTORY_SEPARATOR != '\\' &&

View File

@ -24,8 +24,22 @@ namespace Doctrine\ORM\Tools\Cli\Printer;
use Doctrine\ORM\Tools\Cli\AbstractPrinter, use Doctrine\ORM\Tools\Cli\AbstractPrinter,
Doctrine\ORM\Tools\Cli\Style; Doctrine\ORM\Tools\Cli\Style;
/**
* CLI Output Printer for Normal terminal
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class Normal extends AbstractPrinter class Normal extends AbstractPrinter
{ {
/**
* @inheritdoc
*/
public function format($message, Style $style) public function format($message, Style $style)
{ {
return $message; return $message;

View File

@ -21,14 +21,39 @@
namespace Doctrine\ORM\Tools\Cli; namespace Doctrine\ORM\Tools\Cli;
/**
* CLI Output Style
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class Style class Style
{ {
/**
* @var string Background color
*/
private $_background; private $_background;
/**
* @var string Foreground color
*/
private $_foreground; private $_foreground;
/**
* @var array Formatting options
*/
private $_options = array(); private $_options = array();
/**
* @param string $foreground Foreground color name
* @param string $background Background color name
* @param array $options Formatting options
*/
public function __construct($foreground = null, $background = null, $options = array()) public function __construct($foreground = null, $background = null, $options = array())
{ {
$this->_foreground = strtoupper($foreground); $this->_foreground = strtoupper($foreground);
@ -36,16 +61,31 @@ class Style
$this->_options = $options; $this->_options = $options;
} }
/**
* Retrieves the foreground color name
*
* @return string
*/
public function getForeground() public function getForeground()
{ {
return $this->_foreground; return $this->_foreground;
} }
/**
* Retrieves the background color name
*
* @return string
*/
public function getBackground() public function getBackground()
{ {
return $this->_background; return $this->_background;
} }
/**
* Retrieves the formatting options
*
* @return string
*/
public function getOptions() public function getOptions()
{ {
return $this->_options; return $this->_options;

View File

@ -23,8 +23,22 @@ namespace Doctrine\ORM\Tools\Cli\Task;
use Doctrine\ORM\Tools\Cli\AbstractTask; use Doctrine\ORM\Tools\Cli\AbstractTask;
/**
* CLI Task to display available commands help
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class Help extends AbstractTask class Help extends AbstractTask
{ {
/**
* @inheritdoc
*/
public function extendedHelp() public function extendedHelp()
{ {
$this->getPrinter()->write('help extended help' . PHP_EOL, 'HEADER'); $this->getPrinter()->write('help extended help' . PHP_EOL, 'HEADER');
@ -34,6 +48,9 @@ class Help extends AbstractTask
$this->getPrinter()->write('help extended help' . PHP_EOL, 'NONE'); $this->getPrinter()->write('help extended help' . PHP_EOL, 'NONE');
} }
/**
* @inheritdoc
*/
public function basicHelp() public function basicHelp()
{ {
$this->getPrinter()->write('help basic help' . PHP_EOL, 'HEADER'); $this->getPrinter()->write('help basic help' . PHP_EOL, 'HEADER');
@ -42,12 +59,19 @@ class Help extends AbstractTask
$this->getPrinter()->write('help basic help' . PHP_EOL, 'COMMENT'); $this->getPrinter()->write('help basic help' . PHP_EOL, 'COMMENT');
$this->getPrinter()->write('help basic help' . PHP_EOL, 'NONE'); $this->getPrinter()->write('help basic help' . PHP_EOL, 'NONE');
} }
/**
* @inheritdoc
*/
public function validate() public function validate()
{ {
return true; return true;
} }
/**
* Exposes the available tasks
*
*/
public function run() public function run()
{ {
$this->getPrinter()->write('help run' . PHP_EOL, 'HEADER'); $this->getPrinter()->write('help run' . PHP_EOL, 'HEADER');

View File

@ -23,8 +23,22 @@ namespace Doctrine\ORM\Tools\Cli\Task;
use Doctrine\ORM\Tools\Cli\AbstractTask; use Doctrine\ORM\Tools\Cli\AbstractTask;
/**
* CLI Task to display the doctrine version
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class Version extends AbstractTask class Version extends AbstractTask
{ {
/**
* @inheritdoc
*/
public function extendedHelp() public function extendedHelp()
{ {
$this->getPrinter()->write('version extended help' . PHP_EOL, 'HEADER'); $this->getPrinter()->write('version extended help' . PHP_EOL, 'HEADER');
@ -34,6 +48,9 @@ class Version extends AbstractTask
$this->getPrinter()->write('version extended help' . PHP_EOL, 'NONE'); $this->getPrinter()->write('version extended help' . PHP_EOL, 'NONE');
} }
/**
* @inheritdoc
*/
public function basicHelp() public function basicHelp()
{ {
$this->getPrinter()->write('version basic help' . PHP_EOL, 'HEADER'); $this->getPrinter()->write('version basic help' . PHP_EOL, 'HEADER');
@ -43,11 +60,18 @@ class Version extends AbstractTask
$this->getPrinter()->write('version basic help' . PHP_EOL, 'NONE'); $this->getPrinter()->write('version basic help' . PHP_EOL, 'NONE');
} }
/**
* @inheritdoc
*/
public function validate() public function validate()
{ {
return true; return true;
} }
/**
* Displays the current version of Doctrine
*
*/
public function run() public function run()
{ {
$this->getPrinter()->write('version run' . PHP_EOL, 'HEADER'); $this->getPrinter()->write('version run' . PHP_EOL, 'HEADER');