Doctrine 1.2.4
Doctrine_Cli Class Reference

Public Member Functions

 __construct (array $config=array(), Doctrine_Cli_Formatter $formatter=null)
 
 getConfig ()
 
 getConfigValue ($name)
 
 getFormatter ()
 
 getLoadedTasks ()
 
 getRegisteredTasks ()
 
 getTaskInstance ()
 
 hasConfigValue ($name, $value=null, $strict=false)
 
 loadTasks ($directory=null)
 
 notify ($notification=null, $style= 'HEADER')
 
 printTasks ($taskName=null, $full=false)
 
 registerIncludedTaskClasses ()
 
 registerTaskClass ($className)
 
 run (array $args)
 
 setConfig (array $config)
 
 setFormatter (Doctrine_Cli_Formatter $formatter)
 
 setRegisteredTasks (array $registeredTask)
 
 setTaskInstance (Doctrine_Task $task)
 
 taskClassIsRegistered ($className)
 
 taskNameIsRegistered ($taskName, &$className=null)
 

Protected Member Functions

 _getTaskClassFromArgs (array $args)
 
 _run (array $args)
 
 assembleArgumentList (array $argumentsDescriptions, array $config, Doctrine_Cli_Formatter $formatter)
 
 classIsTask ($className)
 
 createTaskInstance ($className, Doctrine_Cli $cli)
 
 executeTask (Doctrine_Task $task, array $preparedArguments)
 
 formatExceptionMessage (Exception $exception)
 
 includeAndRegisterDoctrineTaskClasses ($directories=null)
 
 includeAndRegisterTaskClasses ()
 
 includeDoctrineTaskClasses ($directory)
 
 notifyException (Exception $exception)
 
 prepareArgs (array $args)
 

Detailed Description

Definition at line 35 of file Cli.php.

Constructor & Destructor Documentation

Doctrine_Cli::__construct ( array  $config = array(),
Doctrine_Cli_Formatter  $formatter = null 
)

__construct

Parameters
array[$config=array()]
object|null[$formatter=null] Doctrine_Cli_Formatter

Definition at line 77 of file Cli.php.

{
$this->setConfig($config);
$this->setFormatter($formatter ? $formatter : new Doctrine_Cli_AnsiColorFormatter());
}

Member Function Documentation

Doctrine_Cli::_getTaskClassFromArgs ( array  $args)
protected

Old method retained for backwards compatibility

Deprecated:

Definition at line 665 of file Cli.php.

{
return self::TASK_BASE_CLASS . '_' . Doctrine_Inflector::classify(str_replace('-', '_', $args[1]));
}
Doctrine_Cli::_run ( array  $args)
protected

Run the actual task execution with the passed arguments

Parameters
array$argsArray of arguments for this task being executed
Returns
void
Exceptions
Doctrine_Cli_ExceptionIf the requested task has not been registered or if required arguments are missing
Todo:
Continue refactoring for testing

Definition at line 476 of file Cli.php.

{
$this->_scriptName = $args[0];
$requestedTaskName = isset($args[1]) ? $args[1] : null;
if ( ! $requestedTaskName || $requestedTaskName == 'help') {
$this->printTasks(null, $requestedTaskName == 'help' ? true : false);
return;
}
if ($requestedTaskName && isset($args[2]) && $args[2] === 'help') {
$this->printTasks($requestedTaskName, true);
return;
}
if (! $this->taskNameIsRegistered($requestedTaskName, $taskClassName)) {
throw new Doctrine_Cli_Exception("The task \"{$requestedTaskName}\" has not been registered");
}
$taskInstance = $this->createTaskInstance($taskClassName, $this);
$this->setTaskInstance($taskInstance);
$this->executeTask($taskInstance, $this->prepareArgs(array_slice($args, 2)));
}
Doctrine_Cli::assembleArgumentList ( array  $argumentsDescriptions,
array  $config,
Doctrine_Cli_Formatter  $formatter 
)
protected
Parameters
array$argumentsDescriptions
array$config
object$formatterDoctrine_Cli_Formatter
Returns
string

Definition at line 611 of file Cli.php.

{
$argumentList = '';
foreach ($argumentsDescriptions as $name => $description) {
$argumentList .= $formatter->format($name, 'ERROR') . ' - ';
if (isset($config[$name])) {
$argumentList .= $formatter->format($config[$name], 'COMMENT');
} else {
$argumentList .= $description;
}
$argumentList .= "\n";
}
return $argumentList;
}
Doctrine_Cli::classIsTask (   $className)
protected

Returns TRUE if the specified class is a Task, or FALSE otherwise

Parameters
string$className
Returns
bool

Definition at line 359 of file Cli.php.

{
$reflectionClass = new ReflectionClass($className);
return (bool) $reflectionClass->isSubClassOf(self::TASK_BASE_CLASS);
}
Doctrine_Cli::createTaskInstance (   $className,
Doctrine_Cli  $cli 
)
protected

Creates, and returns, a new instance of the specified Task class

Displays a message, and returns FALSE, if there were problems instantiating the class

Parameters
string$className
object$cliDoctrine_Cli
Returns
object Doctrine_Task

Definition at line 374 of file Cli.php.

{
return new $className($cli);
}
Doctrine_Cli::executeTask ( Doctrine_Task  $task,
array  $preparedArguments 
)
protected

Executes the task with the specified prepared arguments

Parameters
object$taskDoctrine_Task
array$preparedArguments
Exceptions
Doctrine_Cli_ExceptionIf required arguments are missing

Definition at line 508 of file Cli.php.

{
$task->setArguments($preparedArguments);
if (! $task->validate()) {
throw new Doctrine_Cli_Exception('Required arguments missing');
}
$task->execute();
}
Doctrine_Cli::formatExceptionMessage ( Exception  $exception)
protected

Formats, and then returns, the message in the specified exception

Parameters
Exception$exception
Returns
string

Definition at line 416 of file Cli.php.

{
$message = $exception->getMessage();
$message .= "\n" . $exception->getTraceAsString();
}
return $this->getFormatter()->format($message, 'ERROR') . "\n";
}
Doctrine_Cli::getConfig ( )
Returns
array

Definition at line 95 of file Cli.php.

{
return $this->_config;
}
Doctrine_Cli::getConfigValue (   $name)

Returns the specified value from the config, or the default value, if specified

Parameters
string$name
Returns
mixed
Exceptions
OutOfBoundsExceptionIf the element does not exist in the config

Definition at line 123 of file Cli.php.

{
if (! isset($this->_config[$name])) {
if (func_num_args() > 1) {
return func_get_arg(1);
}
throw new OutOfBoundsException("The element \"{$name}\" does not exist in the config");
}
return $this->_config[$name];
}
Doctrine_Cli::getFormatter ( )
Returns
object Doctrine_Cli_Formatter

Definition at line 111 of file Cli.php.

{
return $this->_formatter;
}
Doctrine_Cli::getLoadedTasks ( )

Old method retained for backwards compatibility

Deprecated:

Definition at line 675 of file Cli.php.

{
return $this->createOldStyleTaskList($this->getRegisteredTasks());
}
Doctrine_Cli::getRegisteredTasks ( )

Returns an array containing the registered tasks

Returns
array

Definition at line 181 of file Cli.php.

{
return $this->_registeredTask;
}
Doctrine_Cli::getTaskInstance ( )
Returns
object Doctrine_Task

Definition at line 229 of file Cli.php.

{
return $this->_taskInstance;
}
Doctrine_Cli::hasConfigValue (   $name,
  $value = null,
  $strict = false 
)

Returns TRUE if the element in the config has the specified value, or FALSE otherwise

If $value is not passed, this method will return TRUE if the specified element has any value, or FALSE if the element is not set

For strict checking, set $strict to TRUE - the default is FALSE

Parameters
string$name
mixed[$value=null]
bool[$strict=false]
Returns
bool

Definition at line 149 of file Cli.php.

{
if (isset($this->_config[$name])) {
if (func_num_args() < 2) {
return true;
}
if ($strict) {
return $this->_config[$name] === $value;
}
return $this->_config[$name] == $value;
}
return false;
}
Doctrine_Cli::includeAndRegisterDoctrineTaskClasses (   $directories = null)
protected

Includes and registers Doctrine-style tasks from the specified directory / directories

If no directory is given it looks in the default Doctrine/Task folder for the core tasks

Parameters
mixed[$directories=null] Can be a string path or array of paths

Definition at line 258 of file Cli.php.

{
if (is_null($directories)) {
$directories = Doctrine_Core::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Task';
}
foreach ((array) $directories as $directory) {
foreach ($this->includeDoctrineTaskClasses($directory) as $className) {
$this->registerTaskClass($className);
}
}
}
Doctrine_Cli::includeAndRegisterTaskClasses ( )
protected

Called by the constructor, this method includes and registers Doctrine core Tasks and then registers all other loaded Task classes

The second round of registering will pick-up loaded custom Tasks. Methods are provided that will allow users to register Tasks loaded after creating an instance of Doctrine_Cli.

Definition at line 241 of file Cli.php.

{
//Always autoregister custom tasks _unless_ we've been explicitly asked not to
if ($this->getConfigValue('autoregister_custom_tasks', true)) {
}
}
Doctrine_Cli::includeDoctrineTaskClasses (   $directory)
protected

Attempts to include Doctrine-style Task-classes from the specified directory - and nothing more besides

Returns an array containing the names of Task classes included

This method effectively makes two assumptions:

  • The directory contains only Task class-files
  • The class files, and the class in each, follow the Doctrine naming conventions

This means that a file called "Foo.php", say, will be expected to contain a Task class called "Doctrine_Task_Foo". Hence the method's name, "include*Doctrine*TaskClasses".

Parameters
string$directory
Returns
array $taskClassesIncluded
Exceptions
InvalidArgumentExceptionIf the directory does not exist

Definition at line 287 of file Cli.php.

{
if (! is_dir($directory)) {
throw new InvalidArgumentException("The directory \"{$directory}\" does not exist");
}
$taskClassesIncluded = array();
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iterator as $file) {
$baseName = $file->getFileName();
/*
* Class-files must start with an uppercase letter. This additional check will help prevent us
* accidentally running 'executable' scripts that may be mixed-in with the class files.
*/
$matched = (bool) preg_match('/^([A-Z].*?)\.php$/', $baseName, $matches);
if ( ! ($matched && (strpos($baseName, '.inc') === false))) {
continue;
}
$expectedClassName = self::TASK_BASE_CLASS . '_' . $matches[1];
if ( ! class_exists($expectedClassName)) {
require_once($file->getPathName());
}
//So was the expected class included, and is it a task? If so, we'll let the calling function know.
if (class_exists($expectedClassName, false) && $this->classIsTask($expectedClassName)) {
$taskClassesIncluded[] = $expectedClassName;
}
}
return $taskClassesIncluded;
}
Doctrine_Cli::loadTasks (   $directory = null)

Old method retained for backwards compatibility

Deprecated:

Definition at line 654 of file Cli.php.

{
return $this->createOldStyleTaskList($this->getRegisteredTasks());
}
Doctrine_Cli::notify (   $notification = null,
  $style = 'HEADER' 
)

Notify the formatter of a message

Parameters
string$notificationThe notification message
string$styleStyle to format the notification with(INFO, ERROR)
Returns
void

Definition at line 400 of file Cli.php.

{
$formatter = $this->getFormatter();
echo(
$formatter->format($this->getTaskInstance()->getTaskName(), 'INFO') . ' - ' .
$formatter->format($notification, $style) . "\n"
);
}
Doctrine_Cli::notifyException ( Exception  $exception)
protected

Notify the formatter of an exception

N.B. This should really only be called by Doctrine_Cli::run(). Exceptions should be thrown when errors occur: it's up to Doctrine_Cli::run() to determine how those exceptions are reported.

Parameters
Exception$exception
Returns
void

Definition at line 436 of file Cli.php.

{
echo $this->formatExceptionMessage($exception);
}
Doctrine_Cli::prepareArgs ( array  $args)
protected

Prepare the raw arguments for execution. Combines with the required and optional argument list in order to determine a complete array of arguments for the task

Parameters
array$argsArray of raw arguments
Returns
array $prepared Array of prepared arguments
Todo:
Continue refactoring for testing

Definition at line 527 of file Cli.php.

{
$taskInstance = $this->getTaskInstance();
$args = array_values($args);
// First lets load populate an array with all the possible arguments. required and optional
$prepared = array();
$requiredArguments = $taskInstance->getRequiredArguments();
foreach ($requiredArguments as $key => $arg) {
$prepared[$arg] = null;
}
$optionalArguments = $taskInstance->getOptionalArguments();
foreach ($optionalArguments as $key => $arg) {
$prepared[$arg] = null;
}
// If we have a config array then lets try and fill some of the arguments with the config values
foreach ($this->getConfig() as $key => $value) {
if (array_key_exists($key, $prepared)) {
$prepared[$key] = $value;
}
}
// Now lets fill in the entered arguments to the prepared array
$copy = $args;
foreach ($prepared as $key => $value) {
if ( ! $value && !empty($copy)) {
$prepared[$key] = $copy[0];
unset($copy[0]);
$copy = array_values($copy);
}
}
return $prepared;
}
Doctrine_Cli::printTasks (   $taskName = null,
  $full = false 
)

Prints an index of all the available tasks in the CLI instance

Parameters
string|null[$taskName=null]
bool[$full=false]
Todo:
Continue refactoring for testing

Definition at line 573 of file Cli.php.

{
$formatter = $this->getFormatter();
$config = $this->getConfig();
$taskIndex = $formatter->format('Doctrine Command Line Interface', 'HEADER') . "\n\n";
foreach ($this->getRegisteredTasks() as $task) {
if ($taskName && (strtolower($taskName) != strtolower($task->getTaskName()))) {
continue;
}
$taskIndex .= $formatter->format($this->_scriptName . ' ' . $task->getTaskName(), 'INFO');
if ($full) {
$taskIndex .= ' - ' . $task->getDescription() . "\n";
$args = '';
$args .= $this->assembleArgumentList($task->getRequiredArgumentsDescriptions(), $config, $formatter);
$args .= $this->assembleArgumentList($task->getOptionalArgumentsDescriptions(), $config, $formatter);
if ($args) {
$taskIndex .= "\n" . $formatter->format('Arguments:', 'HEADER') . "\n" . $args;
}
}
$taskIndex .= "\n";
}
echo $taskIndex;
}
Doctrine_Cli::registerIncludedTaskClasses ( )

Registers all loaded classes - by default - or the specified loaded Task classes

This method will skip registered task classes, so it can be safely called many times over

Definition at line 384 of file Cli.php.

{
foreach (get_declared_classes() as $className) {
if ($this->classIsTask($className)) {
$this->registerTaskClass($className);
}
}
}
Doctrine_Cli::registerTaskClass (   $className)

Registers the specified included task-class

Parameters
string$className
Exceptions
InvalidArgumentExceptionIf the class does not exist or the task-name is blank
DomainExceptionIf the class is not a Doctrine Task

Definition at line 335 of file Cli.php.

{
//Simply ignore registered classes
if ($this->taskClassIsRegistered($className)) {
return;
}
if ( ! class_exists($className/*, false*/)) {
throw new InvalidArgumentException("The task class \"{$className}\" does not exist");
}
if ( ! $this->classIsTask($className)) {
throw new DomainException("The class \"{$className}\" is not a Doctrine Task");
}
$this->_registeredTask[$className] = $this->createTaskInstance($className, $this);
}
Doctrine_Cli::run ( array  $args)

Public function to run the loaded task with the passed arguments

Parameters
array$args
Returns
void
Exceptions
Doctrine_Cli_Exception
Todo:
Should know more about what we're attempting to run so feedback can be improved. Continue refactoring.

Definition at line 449 of file Cli.php.

{
try {
$this->_run($args);
} catch (Exception $exception) {
//Do not rethrow exceptions by default
if ($this->getConfigValue('rethrow_exceptions', false)) {
throw new $exception($this->formatExceptionMessage($exception));
}
$this->notifyException($exception);
//User error
if ($exception instanceof Doctrine_Cli_Exception) {
$this->printTasks();
}
}
}
Doctrine_Cli::setConfig ( array  $config)
Parameters
array$config

Definition at line 87 of file Cli.php.

{
$this->_config = $config;
}
Doctrine_Cli::setFormatter ( Doctrine_Cli_Formatter  $formatter)
Parameters
object$formatterDoctrine_Cli_Formatter

Definition at line 103 of file Cli.php.

{
$this->_formatter = $formatter;
}
Doctrine_Cli::setRegisteredTasks ( array  $registeredTask)

Sets the array of registered tasks

Parameters
array$registeredTask

Definition at line 171 of file Cli.php.

{
$this->_registeredTask = $registeredTask;
}
Doctrine_Cli::setTaskInstance ( Doctrine_Task  $task)
Parameters
object$taskDoctrine_Task

Definition at line 221 of file Cli.php.

{
$this->_taskInstance = $task;
}
Doctrine_Cli::taskClassIsRegistered (   $className)

Returns TRUE if the specified Task-class is registered, or FALSE otherwise

Parameters
string$className
Returns
bool

Definition at line 192 of file Cli.php.

{
return isset($this->_registeredTask[$className]);
}
Doctrine_Cli::taskNameIsRegistered (   $taskName,
$className = null 
)

Returns TRUE if a task with the specified name is registered, or FALSE otherwise

If a matching task is found, $className is set with the name of the implementing class

Parameters
string$taskName
string|null[&$className=null]
Returns
bool

Definition at line 206 of file Cli.php.

{
foreach ($this->getRegisteredTasks() as $currClassName => $task) {
if ($task->getTaskName() == $taskName) {
$className = $currClassName;
return true;
}
}
return false;
}

The documentation for this class was generated from the following file: