2007-10-10 01:39:48 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id: Task.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_Cli_Task
|
|
|
|
*
|
|
|
|
* @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-10 07:45:02 +04:00
|
|
|
protected $tasks = array();
|
|
|
|
protected $scriptName = null;
|
|
|
|
|
2007-10-10 02:15:14 +04:00
|
|
|
public function run($args)
|
|
|
|
{
|
2007-10-10 07:45:02 +04:00
|
|
|
$this->scriptName = $args[0];
|
|
|
|
|
2007-10-10 02:15:14 +04:00
|
|
|
if (!isset($args[1])) {
|
2007-10-10 06:31:11 +04:00
|
|
|
echo $this->printTasks();
|
|
|
|
return;
|
2007-10-10 02:15:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($args[0]);
|
2007-10-10 06:31:11 +04:00
|
|
|
$taskName = str_replace('-', '_', $args[1]);
|
2007-10-10 02:15:14 +04:00
|
|
|
unset($args[1]);
|
|
|
|
|
|
|
|
$taskClass = 'Doctrine_Cli_Task_' . Doctrine::classify($taskName);
|
|
|
|
|
|
|
|
if (class_exists($taskClass)) {
|
|
|
|
$taskInstance = new $taskClass();
|
2007-10-10 06:31:11 +04:00
|
|
|
$taskInstance->taskName = str_replace('_', '-', Doctrine::tableize(str_replace('Doctrine_Cli_Task_', '', $taskName)));
|
|
|
|
|
|
|
|
$args = $taskInstance->prepareArgs($args);
|
|
|
|
|
|
|
|
$taskInstance->validate($args);
|
2007-10-10 02:15:14 +04:00
|
|
|
$taskInstance->execute($args);
|
|
|
|
} else {
|
|
|
|
throw new Doctrine_Cli_Exception('Cli task could not be found: '.$taskClass);
|
|
|
|
}
|
|
|
|
}
|
2007-10-10 06:31:11 +04:00
|
|
|
|
|
|
|
public function printTasks()
|
|
|
|
{
|
|
|
|
$tasks = $this->loadTasks();
|
|
|
|
|
|
|
|
echo "\nAvailable Doctrine Command Line Interface Tasks\n";
|
|
|
|
echo str_repeat('-', 40)."\n\n";
|
2007-10-10 07:45:02 +04:00
|
|
|
|
2007-10-10 06:31:11 +04:00
|
|
|
foreach ($tasks as $taskName)
|
|
|
|
{
|
|
|
|
$className = 'Doctrine_Cli_Task_' . $taskName;
|
|
|
|
$taskInstance = new $className();
|
|
|
|
$taskInstance->taskName = str_replace('_', '-', Doctrine::tableize($taskName));
|
|
|
|
|
2007-10-10 07:45:02 +04:00
|
|
|
echo $taskInstance->getDescription() . "\n";
|
|
|
|
|
|
|
|
$syntax = "Syntax: ";
|
2007-10-10 06:31:11 +04:00
|
|
|
|
2007-10-10 07:45:02 +04:00
|
|
|
$syntax .= $this->scriptName . ' ' . $taskInstance->getTaskName();
|
|
|
|
|
|
|
|
if ($required = $taskInstance->getRequiredArguments()) {
|
|
|
|
$syntax .= ' <' . implode('> <', $required) . '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($optional = $taskInstance->getOptionalArguments()) {
|
|
|
|
$syntax .= ' <' . implode('> <', $optional) . '>';
|
2007-10-10 06:31:11 +04:00
|
|
|
}
|
|
|
|
|
2007-10-10 07:45:02 +04:00
|
|
|
echo $syntax."\n";
|
|
|
|
|
|
|
|
$args = null;
|
|
|
|
if ($requiredArguments = $taskInstance->getRequiredArgumentsDescriptions()) {
|
|
|
|
foreach ($requiredArguments as $name => $description) {
|
|
|
|
$args .= '*' . $name . ' - ' . $description."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($optionalArguments = $taskInstance->getOptionalArgumentsDescriptions()) {
|
2007-10-10 22:21:36 +04:00
|
|
|
foreach ($optionalArguments as $name => $description) {
|
2007-10-10 07:45:02 +04:00
|
|
|
$args .= $name . ' - ' . $description."\n";
|
|
|
|
}
|
2007-10-10 06:31:11 +04:00
|
|
|
}
|
|
|
|
|
2007-10-10 07:45:02 +04:00
|
|
|
if ($args) {
|
2007-10-10 22:21:36 +04:00
|
|
|
echo "\nArguments (* = required):\n";
|
2007-10-10 07:45:02 +04:00
|
|
|
echo $args;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\n".str_repeat("-", 40)."\n";
|
2007-10-10 06:31:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadTasks($directory = null)
|
|
|
|
{
|
|
|
|
if ($directory === null) {
|
|
|
|
$directory = dirname(__FILE__). DIRECTORY_SEPARATOR . 'Cli' . DIRECTORY_SEPARATOR . 'Task';
|
|
|
|
}
|
|
|
|
|
|
|
|
$parent = new ReflectionClass('Doctrine_Cli_Task');
|
|
|
|
|
|
|
|
$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) {
|
|
|
|
require_once($file->getPathName());
|
|
|
|
|
|
|
|
$className = 'Doctrine_Cli_Task_' . $e[0];
|
|
|
|
$class = new ReflectionClass($className);
|
|
|
|
|
|
|
|
if ($class->isSubClassOf($parent)) {
|
|
|
|
$tasks[] = $e[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10 02:15:14 +04:00
|
|
|
}
|