2009-08-24 01:41:34 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools;
|
|
|
|
|
|
|
|
use Doctrine\Common\Util\Inflector,
|
2009-08-31 20:21:29 +04:00
|
|
|
Doctrine\ORM\Tools\Cli\Printers\AbstractPrinter,
|
|
|
|
Doctrine\ORM\Tools\Cli\Tasks\AbstractTask,
|
|
|
|
Doctrine\ORM\Tools\Cli\Printers\AnsiColorPrinter;
|
2009-08-24 01:41:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic CLI Runner of Tasks
|
|
|
|
*
|
|
|
|
* To include a new Task support, create a task:
|
|
|
|
*
|
|
|
|
* [php]
|
2009-08-25 08:46:46 +04:00
|
|
|
* class MyProject\Tools\Cli\Tasks\MyTask extends Doctrine\ORM\Tools\Cli\AbstractTask {
|
2009-08-24 01:41:34 +04:00
|
|
|
* public function run();
|
|
|
|
* public function basicHelp();
|
|
|
|
* public function extendedHelp();
|
|
|
|
* public function validate();
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* And then, include the support to it in your command-line script:
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $cli = new Doctrine\ORM\Tools\Cli();
|
|
|
|
* $cli->addTask('myTask', 'MyProject\Tools\Cli\Tasks\MyTask');
|
|
|
|
*
|
|
|
|
* To execute, just type any classify-able name:
|
|
|
|
*
|
|
|
|
* [bash]
|
|
|
|
* cli.php my-task
|
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
|
|
|
class Cli
|
|
|
|
{
|
2009-08-25 08:46:46 +04:00
|
|
|
/**
|
|
|
|
* @var AbstractPrinter CLI Printer instance
|
2009-08-24 01:41:34 +04:00
|
|
|
*/
|
|
|
|
private $_printer = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Available tasks
|
|
|
|
*/
|
|
|
|
private $_tasks = array();
|
|
|
|
|
2009-08-25 08:46:46 +04:00
|
|
|
/**
|
|
|
|
* The CLI processor of tasks
|
|
|
|
*
|
|
|
|
* @param AbstractPrinter $printer CLI Output printer
|
|
|
|
*/
|
|
|
|
public function __construct(AbstractPrinter $printer = null)
|
2009-08-24 01:41:34 +04:00
|
|
|
{
|
|
|
|
//$this->_printer = new Printer\Normal();
|
2009-08-31 20:21:29 +04:00
|
|
|
$this->_printer = $printer ?: new AnsiColorPrinter;
|
2009-08-24 01:41:34 +04:00
|
|
|
|
|
|
|
// Include core tasks
|
2009-08-31 20:21:29 +04:00
|
|
|
$ns = 'Doctrine\ORM\Tools\Cli\Tasks';
|
2009-08-24 01:41:34 +04:00
|
|
|
|
|
|
|
$this->addTasks(array(
|
2009-09-30 08:35:43 +04:00
|
|
|
'help' => $ns . '\HelpTask',
|
|
|
|
'version' => $ns . '\VersionTask',
|
|
|
|
'schema-tool' => $ns . '\SchemaToolTask',
|
|
|
|
'run-sql' => $ns . '\RunSqlTask',
|
|
|
|
'run-dql' => $ns . '\RunDqlTask',
|
|
|
|
'convert-mapping' => $ns . '\ConvertMappingTask',
|
2009-10-15 00:18:36 +04:00
|
|
|
'generate-proxies'=> $ns . '\GenerateProxiesTask'
|
2009-08-24 01:41:34 +04:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2009-08-25 08:46:46 +04:00
|
|
|
/**
|
|
|
|
* Add a collection of tasks to the CLI.
|
|
|
|
* To include them, just call the method with the following structure:
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $cli->addTasks(array(
|
|
|
|
* 'my-custom-task' => 'MyProject\Cli\Tasks\MyCustomTask',
|
|
|
|
* ...
|
|
|
|
* ));
|
|
|
|
*
|
|
|
|
* @param array $tasks CLI Tasks to be included
|
|
|
|
*/
|
2009-08-24 01:41:34 +04:00
|
|
|
public function addTasks($tasks)
|
|
|
|
{
|
|
|
|
foreach ($tasks as $name => $class) {
|
|
|
|
$this->addTask($name, $class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-25 08:46:46 +04:00
|
|
|
/**
|
|
|
|
* Add a single task to CLI.
|
|
|
|
* Example of inclusion support to a single task:
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $cli->addTask('my-custom-task', 'MyProject\Cli\Tasks\MyCustomTask');
|
|
|
|
*
|
|
|
|
* @param string $name CLI Task name
|
|
|
|
* @param string $class CLI Task class (FQCN - Fully Qualified Class Name)
|
|
|
|
*/
|
2009-08-24 01:41:34 +04:00
|
|
|
public function addTask($name, $class)
|
|
|
|
{
|
|
|
|
// Convert $name into a class equivalent
|
|
|
|
// (ie. 'show_version' => 'showVersion')
|
|
|
|
$name = $this->_processTaskName($name);
|
|
|
|
|
|
|
|
$this->_tasks[$name] = $class;
|
|
|
|
}
|
|
|
|
|
2009-08-25 08:46:46 +04:00
|
|
|
/**
|
|
|
|
* Processor of CLI Tasks. Handles multiple task calls, instantiate
|
|
|
|
* respective classes and run them.
|
|
|
|
*
|
|
|
|
* @param array $args CLI Arguments
|
|
|
|
*/
|
2009-08-24 01:41:34 +04:00
|
|
|
public function run($args = array())
|
|
|
|
{
|
|
|
|
// Remove script file argument
|
|
|
|
$scriptFile = array_shift($args);
|
|
|
|
|
|
|
|
// Automatically prepend 'help' task if:
|
|
|
|
// 1- No arguments were passed
|
|
|
|
// 2- First item is not a valid task name
|
2009-08-31 20:21:29 +04:00
|
|
|
if (empty($args) || ! isset($this->_tasks[$this->_processTaskName($args[0])])) {
|
2009-08-24 01:41:34 +04:00
|
|
|
array_unshift($args, 'help');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process all sent arguments
|
2009-08-31 20:21:29 +04:00
|
|
|
$processedArgs = $this->_processArguments($args);
|
2009-08-24 01:41:34 +04:00
|
|
|
|
|
|
|
try {
|
2009-10-08 03:39:34 +04:00
|
|
|
$this->_printer->writeln('Doctrine Command Line Interface' . PHP_EOL, 'HEADER');
|
2009-09-01 17:43:53 +04:00
|
|
|
|
2009-08-24 01:41:34 +04:00
|
|
|
// Handle possible multiple tasks on a single command
|
|
|
|
foreach($processedArgs as $taskData) {
|
|
|
|
// Retrieve the task name and arguments
|
|
|
|
$taskName = $this->_processTaskName($taskData['name']);
|
|
|
|
$taskArguments = $taskData['args'];
|
|
|
|
|
|
|
|
// Check if task exists
|
2009-08-24 14:18:10 +04:00
|
|
|
if (isset($this->_tasks[$taskName]) && class_exists($this->_tasks[$taskName], true)) {
|
2009-08-24 01:41:34 +04:00
|
|
|
// Instantiate and execute the task
|
|
|
|
$task = new $this->_tasks[$taskName]();
|
2009-08-25 08:46:46 +04:00
|
|
|
$task->setAvailableTasks($this->_tasks);
|
2009-08-24 01:41:34 +04:00
|
|
|
$task->setPrinter($this->_printer);
|
|
|
|
$task->setArguments($taskArguments);
|
|
|
|
|
2009-09-09 00:13:09 +04:00
|
|
|
if (
|
|
|
|
(isset($taskArguments['help']) && $taskArguments['help']) ||
|
|
|
|
(isset($taskArguments['h']) && $taskArguments['h'])
|
|
|
|
) {
|
2009-08-25 08:46:46 +04:00
|
|
|
$task->extendedHelp(); // User explicitly asked for help option
|
2009-08-24 01:41:34 +04:00
|
|
|
} else if ($this->_isTaskValid($task)) {
|
|
|
|
$task->run();
|
|
|
|
} else {
|
2009-10-08 03:39:34 +04:00
|
|
|
$this->_printer->write(PHP_EOL);
|
2009-08-24 01:41:34 +04:00
|
|
|
$task->basicHelp(); // Fallback of not-valid task arguments
|
2009-10-08 03:39:34 +04:00
|
|
|
$this->_printer->write(PHP_EOL);
|
2009-08-24 01:41:34 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-08-27 02:03:39 +04:00
|
|
|
throw \Doctrine\Common\DoctrineException::taskDoesNotExist($taskName);
|
2009-08-24 01:41:34 +04:00
|
|
|
}
|
|
|
|
}
|
2009-08-24 14:18:10 +04:00
|
|
|
} catch (\Doctrine\Common\DoctrineException $e) {
|
2009-09-01 17:43:53 +04:00
|
|
|
$this->_printer->writeln(
|
2009-10-07 22:52:11 +04:00
|
|
|
$taskName . ': ' . $e->getMessage() . PHP_EOL . PHP_EOL . $e->getTraceAsString(), 'ERROR'
|
2009-08-24 01:41:34 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the given task name and return it formatted
|
|
|
|
*
|
|
|
|
* @param string $taskName Task name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function _processTaskName($taskName)
|
|
|
|
{
|
|
|
|
$taskName = str_replace('-', '_', $taskName);
|
|
|
|
|
|
|
|
return Inflector::classify($taskName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes arguments and returns a structured hierachy.
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* cli.php foo -abc --option=value bar --option -a=value --optArr=value1,value2
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* 0 => array(
|
|
|
|
* 'name' => 'foo',
|
|
|
|
* 'args' => array(
|
|
|
|
* 'a' => true,
|
|
|
|
* 'b' => true,
|
|
|
|
* 'c' => true,
|
|
|
|
* 'option' => 'value',
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* 1 => array(
|
|
|
|
* 'option' => true,
|
|
|
|
* 'a' => 'value',
|
|
|
|
* 'optArr' => array(
|
|
|
|
* 'value1', 'value2'
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* Based on implementation of Patrick Fisher <patrick@pwfisher.com> available at:
|
|
|
|
*
|
|
|
|
* http://pwfisher.com/nucleus/index.php?itemid=45
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function _processArguments($args = array())
|
|
|
|
{
|
2009-09-09 00:13:09 +04:00
|
|
|
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
|
|
|
|
$regex = '/\s*[,]?\s*"([^"]*)"|\s*[,]?\s*([^,]*)/i';
|
2009-08-24 01:41:34 +04:00
|
|
|
$preparedArgs = array();
|
|
|
|
$out = & $preparedArgs;
|
|
|
|
|
|
|
|
foreach ($args as $arg){
|
|
|
|
// --foo --bar=baz
|
|
|
|
if (substr($arg, 0, 2) == '--'){
|
|
|
|
$eqPos = strpos($arg, '=');
|
|
|
|
|
|
|
|
// --foo
|
|
|
|
if ($eqPos === false){
|
|
|
|
$key = substr($arg, 2);
|
|
|
|
$out[$key] = isset($out[$key]) ? $out[$key] : true;
|
|
|
|
// --bar=baz
|
|
|
|
} else {
|
|
|
|
$key = substr($arg, 2, $eqPos - 2);
|
2009-09-09 00:13:09 +04:00
|
|
|
$value = substr($arg, $eqPos + 1);
|
|
|
|
$value = (strpos($value, ' ') !== false) ? $value
|
|
|
|
: array_values(array_filter(explode(',', $value), function ($v) { return trim($v) != ''; }));
|
|
|
|
$out[$key] = ( ! is_array($value) || (is_array($value) && count($value) > 1))
|
|
|
|
? $value : $value[0];
|
2009-08-24 01:41:34 +04:00
|
|
|
}
|
|
|
|
// -k=value -abc
|
|
|
|
} else if (substr($arg, 0, 1) == '-'){
|
|
|
|
// -k=value
|
|
|
|
if (substr($arg, 2, 1) == '='){
|
|
|
|
$key = substr($arg, 1, 1);
|
2009-09-09 00:13:09 +04:00
|
|
|
$value = substr($arg, 3);
|
|
|
|
$value = (strpos($value, ' ') !== false) ? $value
|
|
|
|
: array_values(array_filter(explode(',', $value), function ($v) { return trim($v) != ''; }));
|
|
|
|
$out[$key] = ( ! is_array($value) || (is_array($value) && count($value) > 1))
|
|
|
|
? $value : $value[0];
|
2009-08-24 01:41:34 +04:00
|
|
|
// -abc
|
|
|
|
} else {
|
|
|
|
$chars = str_split(substr($arg, 1));
|
|
|
|
|
|
|
|
foreach ($chars as $char){
|
|
|
|
$key = $char;
|
|
|
|
$out[$key] = isset($out[$key]) ? $out[$key] : true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// plain-arg
|
|
|
|
} else {
|
|
|
|
$key = count($preparedArgs);
|
|
|
|
$preparedArgs[$key] = array(
|
|
|
|
'name' => $arg,
|
|
|
|
'args' => array()
|
|
|
|
);
|
|
|
|
$out = & $preparedArgs[$key]['args'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $preparedArgs;
|
|
|
|
}
|
|
|
|
|
2009-08-25 08:46:46 +04:00
|
|
|
/**
|
|
|
|
* Checks if CLI Task is valid based on given arguments.
|
|
|
|
*
|
|
|
|
* @param AbstractTask $task CLI Task instance
|
|
|
|
*/
|
|
|
|
private function _isTaskValid(AbstractTask $task)
|
2009-08-24 01:41:34 +04:00
|
|
|
{
|
2009-08-25 08:46:46 +04:00
|
|
|
// TODO: Should we change the behavior and check for
|
|
|
|
// required and optional arguments here?
|
2009-08-24 01:41:34 +04:00
|
|
|
return $task->validate();
|
|
|
|
}
|
|
|
|
}
|