2007-10-10 01:39:48 +04:00
|
|
|
<?php
|
|
|
|
/*
|
2007-10-20 06:30:15 +04:00
|
|
|
* $Id: Cli.php 2761 2007-10-07 23:42:29Z zYne $
|
2007-10-10 01:39:48 +04:00
|
|
|
*
|
|
|
|
* 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>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2007-10-16 01:18:13 +04:00
|
|
|
* Doctrine_Cli
|
2007-10-10 01:39:48 +04:00
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @subpackage Cli
|
|
|
|
* @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_Cli
|
2007-10-10 02:15:14 +04:00
|
|
|
{
|
2007-10-20 10:12:20 +04:00
|
|
|
protected $tasks = array(),
|
|
|
|
$taskInstance = null,
|
|
|
|
$formatter = null,
|
|
|
|
$scriptName = null,
|
|
|
|
$message = null,
|
|
|
|
$config = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-16 01:22:31 +04:00
|
|
|
/**
|
|
|
|
* __construct
|
|
|
|
*
|
|
|
|
* @param string $config
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-11 07:23:33 +04:00
|
|
|
public function __construct($config = array())
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
2007-10-20 10:12:20 +04:00
|
|
|
$this->formatter = new Doctrine_Cli_AnsiColorFormatter();
|
2007-10-20 21:20:56 +04:00
|
|
|
|
|
|
|
$this->loadTasks();
|
2007-10-20 10:12:20 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
/**
|
|
|
|
* notify
|
|
|
|
*
|
|
|
|
* @param string $notification
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-20 21:20:56 +04:00
|
|
|
public function notify($notification = null, $style = 'HEADER')
|
2007-10-20 10:12:20 +04:00
|
|
|
{
|
2007-10-20 21:20:56 +04:00
|
|
|
echo $this->formatter->format($this->taskInstance->getTaskName(), 'INFO') . ' - ' . $this->formatter->format($notification, $style) . "\n";
|
2007-10-20 10:12:20 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
/**
|
|
|
|
* notifyException
|
|
|
|
*
|
|
|
|
* @param string $exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function notifyException($exception)
|
|
|
|
{
|
|
|
|
echo $this->formatter->format($exception->getMessage(), 'ERROR') . "\n";
|
2007-10-11 07:23:33 +04:00
|
|
|
}
|
2007-10-20 06:30:15 +04:00
|
|
|
|
2007-10-16 01:22:31 +04:00
|
|
|
/**
|
|
|
|
* run
|
|
|
|
*
|
|
|
|
* @param string $args
|
|
|
|
* @return void
|
2007-10-20 06:30:15 +04:00
|
|
|
* @throws new Doctrine_Cli_Exception
|
2007-10-16 01:22:31 +04:00
|
|
|
*/
|
2007-10-10 02:15:14 +04:00
|
|
|
public function run($args)
|
|
|
|
{
|
2007-10-20 10:12:20 +04:00
|
|
|
try {
|
|
|
|
$this->_run($args);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->notifyException($exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getTaskClassFromArgs($args)
|
|
|
|
{
|
|
|
|
$taskName = str_replace('-', '_', $args[1]);
|
|
|
|
$taskClass = 'Doctrine_Task_' . Doctrine::classify($taskName);
|
|
|
|
|
|
|
|
return $taskClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _run($args)
|
|
|
|
{
|
2007-10-10 07:45:02 +04:00
|
|
|
$this->scriptName = $args[0];
|
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
$arg1 = isset($args[1]) ? $args[1]:null;
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $arg1 || $arg1 == 'help') {
|
2007-10-20 10:12:20 +04:00
|
|
|
echo $this->printTasks(null, $arg1 == 'help' ? true:false);
|
2007-10-10 06:31:11 +04:00
|
|
|
return;
|
2007-10-10 02:15:14 +04:00
|
|
|
}
|
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
if (isset($args[1]) && isset($args[2]) && $args[2] == 'help') {
|
|
|
|
echo $this->printTasks($args[1], true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$taskClass = $this->_getTaskClassFromArgs($args);
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! class_exists($taskClass)) {
|
2007-10-20 10:12:20 +04:00
|
|
|
throw new Doctrine_Cli_Exception('Cli task could not be found: ' . $taskClass);
|
|
|
|
}
|
|
|
|
|
2007-10-10 02:15:14 +04:00
|
|
|
unset($args[0]);
|
|
|
|
unset($args[1]);
|
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
$this->taskInstance = new $taskClass($this);
|
2007-10-10 02:15:14 +04:00
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
$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";
|
2007-10-20 21:20:56 +04:00
|
|
|
echo $this->printTasks($arg1, true);
|
2007-10-16 01:18:13 +04:00
|
|
|
}
|
2007-10-20 10:12:20 +04:00
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new Doctrine_Cli_Exception($e->getMessage());
|
2007-10-10 02:15:14 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-20 06:30:15 +04:00
|
|
|
|
2007-10-16 01:22:31 +04:00
|
|
|
/**
|
|
|
|
* prepareArgs
|
|
|
|
*
|
|
|
|
* @param string $args
|
2007-10-20 06:30:15 +04:00
|
|
|
* @return array $prepared
|
2007-10-16 01:22:31 +04:00
|
|
|
*/
|
2007-10-20 10:12:20 +04:00
|
|
|
protected function prepareArgs($args)
|
2007-10-11 07:23:33 +04:00
|
|
|
{
|
2007-10-20 10:12:20 +04:00
|
|
|
$taskInstance = $this->taskInstance;
|
|
|
|
|
2007-10-11 08:11:50 +04:00
|
|
|
$args = array_values($args);
|
|
|
|
|
2007-10-11 08:13:23 +04:00
|
|
|
// First lets load populate an array with all the possible arguments. required and optional
|
2007-10-11 08:11:50 +04:00
|
|
|
$prepared = array();
|
|
|
|
|
|
|
|
$requiredArguments = $taskInstance->getRequiredArguments();
|
|
|
|
foreach ($requiredArguments as $key => $arg) {
|
|
|
|
$prepared[$arg] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$optionalArguments = $taskInstance->getOptionalArguments();
|
|
|
|
foreach ($optionalArguments as $key => $arg) {
|
|
|
|
$prepared[$arg] = null;
|
|
|
|
}
|
|
|
|
|
2007-10-11 08:13:23 +04:00
|
|
|
// If we have a config array then lets try and fill some of the arguments with the config values
|
2007-10-11 07:23:33 +04:00
|
|
|
if (is_array($this->config) && !empty($this->config)) {
|
|
|
|
foreach ($this->config as $key => $value) {
|
2007-10-11 08:11:50 +04:00
|
|
|
if (array_key_exists($key, $prepared)) {
|
|
|
|
$prepared[$key] = $value;
|
2007-10-11 07:23:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 08:13:23 +04:00
|
|
|
// Now lets fill in the entered arguments to the prepared array
|
2007-10-11 08:11:50 +04:00
|
|
|
$copy = $args;
|
|
|
|
foreach ($prepared as $key => $value) {
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $value && !empty($copy)) {
|
2007-10-11 08:11:50 +04:00
|
|
|
$prepared[$key] = $copy[0];
|
|
|
|
unset($copy[0]);
|
|
|
|
$copy = array_values($copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $prepared;
|
2007-10-11 07:23:33 +04:00
|
|
|
}
|
2007-10-20 06:30:15 +04:00
|
|
|
|
2007-10-16 01:22:31 +04:00
|
|
|
/**
|
|
|
|
* printTasks
|
|
|
|
*
|
2007-10-20 06:30:15 +04:00
|
|
|
* Prints an index of all the available tasks in the CLI instance
|
|
|
|
*
|
2007-10-16 01:22:31 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-20 10:12:20 +04:00
|
|
|
public function printTasks($task = null, $full = false)
|
2007-10-10 06:31:11 +04:00
|
|
|
{
|
2007-10-20 10:12:20 +04:00
|
|
|
$task = Doctrine::classify(str_replace('-', '_', $task));
|
|
|
|
|
2007-10-20 21:20:56 +04:00
|
|
|
$tasks = $this->getLoadedTasks();
|
2007-10-10 06:31:11 +04:00
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
echo $this->formatter->format("Doctrine Command Line Interface", 'HEADER') . "\n\n";
|
2007-10-10 07:45:02 +04:00
|
|
|
|
2007-10-10 06:31:11 +04:00
|
|
|
foreach ($tasks as $taskName)
|
|
|
|
{
|
2007-10-20 10:12:20 +04:00
|
|
|
if ($task != null && strtolower($task) != strtolower($taskName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:18:13 +04:00
|
|
|
$className = 'Doctrine_Task_' . $taskName;
|
2007-10-10 06:31:11 +04:00
|
|
|
$taskInstance = new $className();
|
2007-10-20 10:12:20 +04:00
|
|
|
$taskInstance->taskName = str_replace('_', '-', Doctrine::tableize($taskName));
|
2007-10-10 07:45:02 +04:00
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
$syntax = $this->scriptName . ' ' . $taskInstance->getTaskName();
|
2007-10-10 06:31:11 +04:00
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
echo $this->formatter->format($syntax, 'INFO');
|
2007-10-10 07:45:02 +04:00
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
if ($full) {
|
|
|
|
echo " - " . $taskInstance->getDescription() . "\n";
|
|
|
|
|
|
|
|
$args = null;
|
|
|
|
|
|
|
|
$requiredArguments = $taskInstance->getRequiredArgumentsDescriptions();
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($requiredArguments)) {
|
2007-10-20 10:12:20 +04:00
|
|
|
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";
|
|
|
|
}
|
2007-10-10 07:45:02 +04:00
|
|
|
}
|
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
$optionalArguments = $taskInstance->getOptionalArgumentsDescriptions();
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($optionalArguments)) {
|
2007-10-20 10:12:20 +04:00
|
|
|
foreach ($optionalArguments as $name => $description) {
|
|
|
|
$args .= $name . ' - ' . $description."\n";
|
|
|
|
}
|
2007-10-10 07:45:02 +04:00
|
|
|
}
|
2007-10-10 06:31:11 +04:00
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
if ($args) {
|
|
|
|
echo "\n" . $this->formatter->format('Arguments:', 'HEADER') . "\n" . $args;
|
|
|
|
}
|
2007-10-10 07:45:02 +04:00
|
|
|
}
|
|
|
|
|
2007-10-20 10:12:20 +04:00
|
|
|
echo "\n";
|
2007-10-10 06:31:11 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-16 01:22:31 +04:00
|
|
|
/**
|
|
|
|
* loadTasks
|
|
|
|
*
|
|
|
|
* @param string $directory
|
2007-10-20 06:30:15 +04:00
|
|
|
* @return array $loadedTasks
|
2007-10-16 01:22:31 +04:00
|
|
|
*/
|
2007-10-10 06:31:11 +04:00
|
|
|
public function loadTasks($directory = null)
|
|
|
|
{
|
|
|
|
if ($directory === null) {
|
2007-10-20 21:20:56 +04:00
|
|
|
$directory = Doctrine::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Task';
|
2007-10-10 06:31:11 +04:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:18:13 +04:00
|
|
|
$parent = new ReflectionClass('Doctrine_Task');
|
2007-10-10 06:31:11 +04:00
|
|
|
|
|
|
|
$tasks = array();
|
|
|
|
|
|
|
|
foreach ((array) $directory as $dir) {
|
|
|
|
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
|
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY);
|
|
|
|
|
|
|
|
foreach ($it as $file) {
|
|
|
|
$e = explode('.', $file->getFileName());
|
|
|
|
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
|
|
|
|
|
2007-10-16 01:18:13 +04:00
|
|
|
$className = 'Doctrine_Task_' . $e[0];
|
2007-10-10 06:31:11 +04:00
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! class_exists($className)) {
|
2007-10-20 21:20:56 +04:00
|
|
|
require_once($file->getPathName());
|
|
|
|
|
|
|
|
$class = new ReflectionClass($className);
|
|
|
|
|
|
|
|
if ($class->isSubClassOf($parent)) {
|
|
|
|
$tasks[$e[0]] = $e[0];
|
|
|
|
}
|
2007-10-10 06:31:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-10 07:45:02 +04:00
|
|
|
$this->tasks = array_merge($this->tasks, $tasks);
|
|
|
|
|
|
|
|
return $this->tasks;
|
2007-10-10 06:31:11 +04:00
|
|
|
}
|
2007-10-20 21:20:56 +04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2007-10-20 19:31:14 +04:00
|
|
|
}
|