From 821cc6e51c11207182ab2cffdac51e87afb39ea2 Mon Sep 17 00:00:00 2001 From: "Jonathan.Wage" <Jonathan.Wage@625475ce-881a-0410-a577-b389adb331d8> Date: Sat, 20 Oct 2007 17:20:56 +0000 Subject: [PATCH] Added confirmation for db drop and added ability to make cli interactive. --- lib/Doctrine/Cli.php | 47 ++++++++++++++++++----- lib/Doctrine/Task.php | 32 ++++++++++++++-- lib/Doctrine/Task/Dql.php | 73 ++++++++++++++++++++++++++++++++++++ lib/Doctrine/Task/DropDb.php | 10 ++++- 4 files changed, 147 insertions(+), 15 deletions(-) create mode 100644 lib/Doctrine/Task/Dql.php diff --git a/lib/Doctrine/Cli.php b/lib/Doctrine/Cli.php index 503ee5033..eb018aebf 100644 --- a/lib/Doctrine/Cli.php +++ b/lib/Doctrine/Cli.php @@ -49,6 +49,8 @@ class Doctrine_Cli { $this->config = $config; $this->formatter = new Doctrine_Cli_AnsiColorFormatter(); + + $this->loadTasks(); } /** @@ -57,9 +59,9 @@ class Doctrine_Cli * @param string $notification * @return void */ - public function notify($notification = null) + public function notify($notification = null, $style = 'HEADER') { - echo $this->formatter->format($this->taskInstance->getTaskName(), 'INFO') . ' - ' . $this->formatter->format($notification, 'HEADER') . "\n"; + echo $this->formatter->format($this->taskInstance->getTaskName(), 'INFO') . ' - ' . $this->formatter->format($notification, $style) . "\n"; } /** @@ -137,7 +139,7 @@ class Doctrine_Cli $this->taskInstance->execute(); } else { echo $this->formatter->format('Requires arguments missing!!', 'ERROR') . "\n\n"; - echo $this->printTasks($taskName, true); + echo $this->printTasks($arg1, true); } } catch (Exception $e) { throw new Doctrine_Cli_Exception($e->getMessage()); @@ -202,7 +204,7 @@ class Doctrine_Cli { $task = Doctrine::classify(str_replace('-', '_', $task)); - $tasks = $this->loadTasks(); + $tasks = $this->getLoadedTasks(); echo $this->formatter->format("Doctrine Command Line Interface", 'HEADER') . "\n\n"; @@ -257,7 +259,7 @@ class Doctrine_Cli echo "\n"; } } - + /** * loadTasks * @@ -267,7 +269,7 @@ class Doctrine_Cli public function loadTasks($directory = null) { if ($directory === null) { - $directory = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Task'; + $directory = Doctrine::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Task'; } $parent = new ReflectionClass('Doctrine_Task'); @@ -281,13 +283,17 @@ class Doctrine_Cli foreach ($it as $file) { $e = explode('.', $file->getFileName()); if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) { - require_once($file->getPathName()); $className = 'Doctrine_Task_' . $e[0]; - $class = new ReflectionClass($className); - if ($class->isSubClassOf($parent)) { - $tasks[] = $e[0]; + if (!class_exists($className)) { + require_once($file->getPathName()); + + $class = new ReflectionClass($className); + + if ($class->isSubClassOf($parent)) { + $tasks[$e[0]] = $e[0]; + } } } } @@ -297,4 +303,25 @@ class Doctrine_Cli return $this->tasks; } + + 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); + } } diff --git a/lib/Doctrine/Task.php b/lib/Doctrine/Task.php index 8658f1bd1..9eaa88484 100644 --- a/lib/Doctrine/Task.php +++ b/lib/Doctrine/Task.php @@ -56,15 +56,39 @@ abstract class Doctrine_Task $this->taskName = str_replace('_', '-', Doctrine::tableize(str_replace('Doctrine_Task_', '', get_class($this)))); } - public function notify($message) + /** + * notify + * + * @param string $notification + * @return void + */ + public function notify() { - if (is_object($message)) { - return $message->notify($message); + if (is_object($this->dispatcher) && method_exists($this->dispatcher, 'notify')) { + $args = func_get_args(); + + return call_user_func_array(array($this->dispatcher, 'notify'), $args); } else { - return $message; + return $notification; } } + /** + * ask + * + * @return void + */ + public function ask() + { + $args = func_get_args(); + + call_user_func_array(array($this, 'notify'), $args); + + $answer = strtolower(trim(fgets(STDIN))); + + return $answer; + } + /** * execute * diff --git a/lib/Doctrine/Task/Dql.php b/lib/Doctrine/Task/Dql.php new file mode 100644 index 000000000..81934d1d5 --- /dev/null +++ b/lib/Doctrine/Task/Dql.php @@ -0,0 +1,73 @@ +<?php +/* + * $Id: Dql.php 2761 2007-10-07 23:42:29Z zYne $ + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This software consists of voluntary contributions made by many individuals + * and is licensed under the LGPL. For more information, see + * <http://www.phpdoctrine.com>. + */ + +/** + * Doctrine_Task_Dql + * + * @package Doctrine + * @subpackage Task + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision: 2761 $ + * @author Jonathan H. Wage <jwage@mac.com> + */ +class Doctrine_Task_Dql extends Doctrine_Task +{ + public $description = 'Execute dql query and display the results', + $requiredArguments = array('models_path' => 'Specify path to your Doctrine_Record definitions.', + 'dql_query' => 'Specify the complete dql query to execute.'), + $optionalArguments = array(); + + public function execute() + { + Doctrine::loadModels($this->getArgument('models_path')); + + $dql = $this->getArgument('dql_query'); + + $query = new Doctrine_Query(); + + $this->notify('executing: "' . $dql . '"'); + + $results = $query->query($dql); + + $this->printResults($results); + } + + protected function printResults($data) + { + $array = $data->toArray(true); + + $yaml = Doctrine_Parser::dump($array, 'yml'); + $lines = explode("\n", $yaml); + + unset($lines[0]); + $lines[1] = $data->getTable()->getOption('name') . ':'; + + foreach ($lines as $yamlLine) { + $line = trim($yamlLine); + + if ($line) { + $this->notify($yamlLine); + } + } + } +} \ No newline at end of file diff --git a/lib/Doctrine/Task/DropDb.php b/lib/Doctrine/Task/DropDb.php index 6c42f19d5..e45ae5497 100644 --- a/lib/Doctrine/Task/DropDb.php +++ b/lib/Doctrine/Task/DropDb.php @@ -38,8 +38,16 @@ class Doctrine_Task_DropDb extends Doctrine_Task public function execute() { + $answer = $this->ask('Are you sure you wish to drop your databases? (y/n)'); + + if ($answer != 'y') { + $this->notify('Successfully cancelled'); + + return; + } + Doctrine::dropDatabases($this->getArgument('connection')); - $this->dispatcher->notify('Successfully dropped all databases'); + $this->notify('Successfully dropped all databases'); } } \ No newline at end of file