Skip to content

Instantly share code, notes, and snippets.

@hartym
Created June 25, 2009 14:20
Show Gist options
  • Save hartym/135890 to your computer and use it in GitHub Desktop.
Save hartym/135890 to your computer and use it in GitHub Desktop.
Index: lib/Doctrine/Cli.php
===================================================================
--- lib/Doctrine/Cli.php (revision 5937)
+++ lib/Doctrine/Cli.php (working copy)
@@ -21,7 +21,7 @@
/**
* Command line interface class
- * Interface for easily executing Doctrine_Task classes from a
+ * Interface for easily executing Doctrine_Task classes from a
* command line interface
*
* @package Doctrine
@@ -44,14 +44,14 @@
/**
* __construct
*
- * @param string $config
+ * @param string $config
* @return void
*/
public function __construct($config = array())
{
$this->_config = $config;
$this->_formatter = new Doctrine_Cli_AnsiColorFormatter();
-
+
$this->loadTasks();
}
@@ -105,7 +105,7 @@
{
$taskName = str_replace('-', '_', $args[1]);
$taskClass = 'Doctrine_Task_' . Doctrine_Inflector::classify($taskName);
-
+
return $taskClass;
}
@@ -117,45 +117,41 @@
* @throws Doctrine_Cli_Exception $e
*/
protected function _run($args)
- {
+ {
$this->_scriptName = $args[0];
-
+
$arg1 = isset($args[1]) ? $args[1]:null;
-
+
if ( ! $arg1 || $arg1 == 'help') {
echo $this->printTasks(null, $arg1 == 'help' ? true:false);
return;
}
-
+
if (isset($args[1]) && isset($args[2]) && $args[2] === 'help') {
echo $this->printTasks($args[1], true);
return;
}
-
+
$taskClass = $this->_getTaskClassFromArgs($args);
-
+
if ( ! class_exists($taskClass)) {
throw new Doctrine_Cli_Exception('Cli task could not be found: ' . $taskClass);
}
-
+
unset($args[0]);
unset($args[1]);
-
+
$this->_taskInstance = new $taskClass($this);
-
+
$args = $this->prepareArgs($args);
-
+
$this->_taskInstance->setArguments($args);
-
- try {
- if ($this->_taskInstance->validate()) {
- $this->_taskInstance->execute();
- } else {
- echo $this->_formatter->format('Requires arguments missing!!', 'ERROR') . "\n\n";
- echo $this->printTasks($arg1, true);
- }
- } catch (Exception $e) {
- throw new Doctrine_Cli_Exception($e->getMessage());
+
+ if ($this->_taskInstance->validate()) {
+ $this->_taskInstance->execute();
+ } else {
+ echo $this->_formatter->format('Requires arguments missing!!', 'ERROR') . "\n\n";
+ echo $this->printTasks($arg1, true);
}
}
@@ -169,22 +165,22 @@
protected function prepareArgs($args)
{
$taskInstance = $this->_taskInstance;
-
+
$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
if (is_array($this->_config) && !empty($this->_config)) {
foreach ($this->_config as $key => $value) {
@@ -193,7 +189,7 @@
}
}
}
-
+
// Now lets fill in the entered arguments to the prepared array
$copy = $args;
foreach ($prepared as $key => $value) {
@@ -203,71 +199,71 @@
$copy = array_values($copy);
}
}
-
+
return $prepared;
}
/**
* Prints an index of all the available tasks in the CLI instance
- *
+ *
* @return void
*/
public function printTasks($task = null, $full = false)
{
$task = Doctrine_Inflector::classify(str_replace('-', '_', $task));
-
+
$tasks = $this->getLoadedTasks();
-
+
echo $this->_formatter->format("Doctrine Command Line Interface", 'HEADER') . "\n\n";
-
+
foreach ($tasks as $taskName)
{
if ($task != null && strtolower($task) != strtolower($taskName)) {
continue;
}
-
+
$className = 'Doctrine_Task_' . $taskName;
$taskInstance = new $className();
- $taskInstance->taskName = str_replace('_', '-', Doctrine_Inflector::tableize($taskName));
-
+ $taskInstance->taskName = str_replace('_', '-', Doctrine_Inflector::tableize($taskName));
+
$syntax = $this->_scriptName . ' ' . $taskInstance->getTaskName();
-
- echo $this->_formatter->format($syntax, 'INFO');
-
+
+ echo $this->_formatter->format($syntax, 'INFO');
+
if ($full) {
- echo " - " . $taskInstance->getDescription() . "\n";
-
+ echo " - " . $taskInstance->getDescription() . "\n";
+
$args = null;
-
+
$requiredArguments = $taskInstance->getRequiredArgumentsDescriptions();
-
+
if ( ! empty($requiredArguments)) {
foreach ($requiredArguments as $name => $description) {
$args .= $this->_formatter->format($name, "ERROR");
-
+
if (isset($this->_config[$name])) {
$args .= " - " . $this->_formatter->format($this->_config[$name], 'COMMENT');
} else {
$args .= " - " . $description;
}
-
+
$args .= "\n";
}
}
-
+
$optionalArguments = $taskInstance->getOptionalArgumentsDescriptions();
-
+
if ( ! empty($optionalArguments)) {
foreach ($optionalArguments as $name => $description) {
$args .= $name . ' - ' . $description."\n";
}
}
-
+
if ($args) {
echo "\n" . $this->_formatter->format('Arguments:', 'HEADER') . "\n" . $args;
}
}
-
+
echo "\n";
}
}
@@ -284,11 +280,11 @@
if ($directory === null) {
$directory = Doctrine::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Task';
}
-
+
$parent = new ReflectionClass('Doctrine_Task');
-
+
$tasks = array();
-
+
if (is_dir($directory)) {
foreach ((array) $directory as $dir) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
@@ -297,14 +293,14 @@
foreach ($it as $file) {
$e = explode('.', $file->getFileName());
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
-
+
$className = 'Doctrine_Task_' . $e[0];
-
+
if ( ! class_exists($className)) {
require_once($file->getPathName());
-
+
$class = new ReflectionClass($className);
-
+
if ($class->isSubClassOf($parent)) {
$tasks[$e[0]] = $e[0];
}
@@ -324,7 +320,7 @@
}
$this->_tasks = array_merge($this->_tasks, $tasks);
-
+
return $this->_tasks;
}
@@ -336,21 +332,21 @@
public function getLoadedTasks()
{
$parent = new ReflectionClass('Doctrine_Task');
-
+
$classes = get_declared_classes();
-
+
$tasks = array();
-
+
foreach ($classes as $className) {
$class = new ReflectionClass($className);
-
+
if ($class->isSubClassOf($parent)) {
$task = str_replace('Doctrine_Task_', '', $className);
-
+
$tasks[$task] = $task;
}
}
-
+
return array_merge($this->_tasks, $tasks);
}
-}
\ No newline at end of file
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment