2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Base class for Shells
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 19:26:26 +00:00
|
|
|
* @package cake.console.shells
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5012
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-07 05:56:10 +00:00
|
|
|
|
|
|
|
App::uses('TaskCollection', 'Console');
|
|
|
|
App::uses('ConsoleOutput', 'Console');
|
|
|
|
App::uses('ConsoleInput', 'Console');
|
2010-12-07 06:14:17 +00:00
|
|
|
App::uses('ConsoleInputSubcommand', 'Console');
|
2010-12-07 05:56:10 +00:00
|
|
|
App::uses('ConsoleOptionParser', 'Console');
|
2011-03-08 05:56:21 +00:00
|
|
|
App::uses('File', 'Utility');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Base class for command-line utilities for automating programmer chores.
|
|
|
|
*
|
2010-12-24 19:26:26 +00:00
|
|
|
* @package cake.console.shells
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Shell extends Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-14 03:40:45 +00:00
|
|
|
/**
|
|
|
|
* Output constants for making verbose and quiet shells.
|
|
|
|
*/
|
|
|
|
const VERBOSE = 2;
|
|
|
|
const NORMAL = 1;
|
|
|
|
const QUIET = 0;
|
|
|
|
|
2010-10-10 04:40:34 +00:00
|
|
|
/**
|
|
|
|
* An instance of ConsoleOptionParser that has been configured for this class.
|
|
|
|
*
|
|
|
|
* @var ConsoleOptionParser
|
|
|
|
*/
|
|
|
|
public $OptionParser;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* If true, the script will ask for permission to perform actions.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $interactive = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains command switches parsed from the command line.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $params = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-18 02:37:57 +00:00
|
|
|
/**
|
|
|
|
* The command (method/task) that is being run.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $command;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains arguments parsed from the command line.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $args = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The name of the shell in camelized.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains tasks to load and instantiate
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $tasks = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains the loaded tasks
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $taskNames = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains models to load and instantiate
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $uses = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-08 01:35:03 +00:00
|
|
|
/**
|
|
|
|
* Task Collection for the command, used to create Tasks.
|
|
|
|
*
|
|
|
|
* @var TaskCollection
|
|
|
|
*/
|
|
|
|
public $Tasks;
|
|
|
|
|
2010-10-10 00:55:00 +00:00
|
|
|
/**
|
|
|
|
* Normalized map of tasks.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_taskMap = array();
|
|
|
|
|
2010-10-04 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* stdout object.
|
|
|
|
*
|
|
|
|
* @var ConsoleOutput
|
|
|
|
*/
|
|
|
|
public $stdout;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stderr object.
|
|
|
|
*
|
|
|
|
* @var ConsoleOutput
|
|
|
|
*/
|
|
|
|
public $stderr;
|
|
|
|
|
2010-10-06 02:44:47 +00:00
|
|
|
/**
|
|
|
|
* stdin object
|
|
|
|
*
|
|
|
|
* @var ConsoleInput
|
|
|
|
*/
|
|
|
|
public $stdin;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructs this Shell instance.
|
|
|
|
*
|
|
|
|
*/
|
2010-10-24 18:55:16 +00:00
|
|
|
function __construct($stdout = null, $stderr = null, $stdin = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->name == null) {
|
2010-10-10 05:56:23 +00:00
|
|
|
$this->name = Inflector::underscore(str_replace(array('Shell', 'Task'), '', get_class($this)));
|
2008-10-29 19:25:49 +00:00
|
|
|
}
|
2010-10-24 18:55:16 +00:00
|
|
|
$this->Tasks = new TaskCollection($this);
|
2010-10-04 04:40:04 +00:00
|
|
|
|
|
|
|
$this->stdout = $stdout;
|
|
|
|
$this->stderr = $stderr;
|
2010-10-05 03:26:38 +00:00
|
|
|
$this->stdin = $stdin;
|
2010-10-04 04:40:04 +00:00
|
|
|
if ($this->stdout == null) {
|
|
|
|
$this->stdout = new ConsoleOutput('php://stdout');
|
|
|
|
}
|
|
|
|
if ($this->stderr == null) {
|
|
|
|
$this->stderr = new ConsoleOutput('php://stderr');
|
|
|
|
}
|
2010-10-05 03:26:38 +00:00
|
|
|
if ($this->stdin == null) {
|
|
|
|
$this->stdin = new ConsoleInput('php://stdin');
|
|
|
|
}
|
2011-04-22 19:53:02 +00:00
|
|
|
|
2010-11-21 05:47:42 +00:00
|
|
|
$parent = get_parent_class($this);
|
2010-11-18 04:40:19 +00:00
|
|
|
if ($this->tasks !== null && $this->tasks !== false) {
|
2010-11-21 05:47:42 +00:00
|
|
|
$this->_mergeVars(array('tasks'), $parent, true);
|
2010-11-18 04:40:19 +00:00
|
|
|
}
|
|
|
|
if ($this->uses !== null && $this->uses !== false) {
|
2010-11-21 05:47:42 +00:00
|
|
|
$this->_mergeVars(array('uses'), $parent, false);
|
2010-11-18 04:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Initializes the Shell
|
|
|
|
* acts as constructor for subclasses
|
|
|
|
* allows configuration of tasks prior to shell execution
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function initialize() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_loadModels();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-07-19 22:22:50 +00:00
|
|
|
* Starts up the Shell
|
2008-05-30 11:40:08 +00:00
|
|
|
* allows for checking and configuring prior to command or main execution
|
|
|
|
* can be overriden in subclasses
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function startup() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_welcome();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Displays a header for the shell
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _welcome() {
|
2009-09-26 21:36:45 +00:00
|
|
|
$this->out();
|
2010-10-04 03:58:07 +00:00
|
|
|
$this->out('<info>Welcome to CakePHP v' . Configure::version() . ' Console</info>');
|
2009-09-21 20:52:08 +00:00
|
|
|
$this->hr();
|
2010-10-23 04:11:31 +00:00
|
|
|
$this->out('App : '. APP_DIR);
|
|
|
|
$this->out('Path: '. APP_PATH);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-04-04 07:14:00 +00:00
|
|
|
* if public $uses = true
|
2008-05-30 11:40:08 +00:00
|
|
|
* Loads AppModel file and constructs AppModel class
|
|
|
|
* makes $this->AppModel available to subclasses
|
2010-04-04 07:14:00 +00:00
|
|
|
* if public $uses is an array of models will load those models
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _loadModels() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->uses === null || $this->uses === false) {
|
|
|
|
return;
|
|
|
|
}
|
2010-12-29 15:44:15 +00:00
|
|
|
App::import('Core', 'ClassRegistry');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($this->uses !== true && !empty($this->uses)) {
|
|
|
|
$uses = is_array($this->uses) ? $this->uses : array($this->uses);
|
2008-10-10 02:14:57 +00:00
|
|
|
|
|
|
|
$modelClassName = $uses[0];
|
|
|
|
if (strpos($uses[0], '.') !== false) {
|
|
|
|
list($plugin, $modelClassName) = explode('.', $uses[0]);
|
|
|
|
}
|
|
|
|
$this->modelClass = $modelClassName;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
foreach ($uses as $modelClass) {
|
2009-11-16 00:55:20 +00:00
|
|
|
list($plugin, $modelClass) = pluginSplit($modelClass, true);
|
2010-07-06 02:19:22 +00:00
|
|
|
$this->{$modelClass} = ClassRegistry::init($plugin . $modelClass);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-04-04 07:14:00 +00:00
|
|
|
* Loads tasks defined in public $tasks
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function loadTasks() {
|
2010-07-08 01:35:03 +00:00
|
|
|
if ($this->tasks === true || empty($this->tasks) || empty($this->Tasks)) {
|
2008-10-29 19:25:49 +00:00
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-10 00:55:00 +00:00
|
|
|
$this->_taskMap = TaskCollection::normalizeObjectArray((array)$this->tasks);
|
|
|
|
foreach ($this->_taskMap as $task => $properties) {
|
2010-07-08 01:35:03 +00:00
|
|
|
$this->taskNames[] = $task;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-10-29 19:25:49 +00:00
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-10 01:40:16 +00:00
|
|
|
/**
|
|
|
|
* Check to see if this shell has a task with the provided name.
|
|
|
|
*
|
|
|
|
* @param string $task The task name to check.
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
|
|
|
public function hasTask($task) {
|
|
|
|
return isset($this->_taskMap[Inflector::camelize($task)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if this shell has a callable method by the given name.
|
|
|
|
*
|
|
|
|
* @param string $name The method name to check.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasMethod($name) {
|
2010-10-10 01:52:12 +00:00
|
|
|
if (empty($this->_reflection)) {
|
|
|
|
$this->_reflection = new ReflectionClass($this);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$method = $this->_reflection->getMethod($name);
|
|
|
|
if (!$method->isPublic() || substr($name, 0, 1) === '_') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($method->getDeclaringClass() != $this->_reflection) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (ReflectionException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-10 01:40:16 +00:00
|
|
|
}
|
|
|
|
|
2010-10-16 05:28:59 +00:00
|
|
|
/**
|
|
|
|
* Dispatch a command to another Shell. Similar to Object::requestAction()
|
|
|
|
* but intended for running shells from other shells.
|
|
|
|
*
|
2010-10-16 05:37:43 +00:00
|
|
|
* ### Usage:
|
2011-04-22 19:53:02 +00:00
|
|
|
*
|
2010-10-16 05:37:43 +00:00
|
|
|
* With a string commmand:
|
|
|
|
*
|
|
|
|
* `return $this->dispatchShell('schema create DbAcl');`
|
|
|
|
*
|
|
|
|
* With an array command:
|
|
|
|
*
|
2011-04-22 19:53:02 +00:00
|
|
|
* `return $this->dispatchShell('schema', 'create', 'i18n', '--dry');`
|
2010-10-16 05:37:43 +00:00
|
|
|
*
|
2011-04-22 19:53:02 +00:00
|
|
|
* @param mixed $command Either an array of args similar to $argv. Or a string command, that can be
|
2010-10-16 05:28:59 +00:00
|
|
|
* exploded on space to simulate argv.
|
|
|
|
* @return mixed. The return of the other shell.
|
|
|
|
*/
|
2010-10-16 05:37:43 +00:00
|
|
|
public function dispatchShell() {
|
|
|
|
$args = func_get_args();
|
|
|
|
if (is_string($args[0]) && count($args) == 1) {
|
|
|
|
$args = explode(' ', $args[0]);
|
2010-10-16 05:28:59 +00:00
|
|
|
}
|
2010-10-16 05:37:43 +00:00
|
|
|
|
|
|
|
$Dispatcher = new ShellDispatcher($args, false);
|
2010-10-16 05:28:59 +00:00
|
|
|
return $Dispatcher->dispatch();
|
|
|
|
}
|
|
|
|
|
2010-10-10 02:48:34 +00:00
|
|
|
/**
|
|
|
|
* Runs the Shell with the provided argv
|
|
|
|
*
|
|
|
|
* @param array $argv Array of arguments to run the shell with. This array should be missing the shell name.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function runCommand($command, $argv) {
|
2010-10-10 04:21:16 +00:00
|
|
|
$isTask = $this->hasTask($command);
|
|
|
|
$isMethod = $this->hasMethod($command);
|
|
|
|
$isMain = $this->hasMethod('main');
|
|
|
|
|
2010-10-10 05:32:20 +00:00
|
|
|
if ($isTask || $isMethod && $command !== 'execute') {
|
2010-10-10 03:42:38 +00:00
|
|
|
array_shift($argv);
|
2010-10-10 04:21:16 +00:00
|
|
|
}
|
2010-10-10 04:59:45 +00:00
|
|
|
|
2011-04-22 21:16:32 +00:00
|
|
|
try {
|
|
|
|
$this->OptionParser = $this->getOptionParser();
|
|
|
|
list($this->params, $this->args) = $this->OptionParser->parse($argv, $command);
|
|
|
|
} catch (ConsoleException $e) {
|
|
|
|
return $this->out($this->OptionParser->help($command));
|
|
|
|
}
|
|
|
|
|
2010-10-11 05:58:12 +00:00
|
|
|
$this->command = $command;
|
2010-10-20 03:35:58 +00:00
|
|
|
if (!empty($this->params['help'])) {
|
2010-10-23 04:36:40 +00:00
|
|
|
return $this->_displayHelp($command);
|
2010-10-20 03:35:58 +00:00
|
|
|
}
|
|
|
|
|
2010-10-10 05:32:20 +00:00
|
|
|
if (($isTask || $isMethod || $isMain) && $command !== 'execute' ) {
|
2010-10-10 04:21:16 +00:00
|
|
|
$this->startup();
|
|
|
|
}
|
2010-10-11 05:58:12 +00:00
|
|
|
|
2010-10-10 04:21:16 +00:00
|
|
|
if ($isTask) {
|
|
|
|
$command = Inflector::camelize($command);
|
2010-10-10 02:48:34 +00:00
|
|
|
return $this->{$command}->runCommand('execute', $argv);
|
|
|
|
}
|
2010-10-10 04:21:16 +00:00
|
|
|
if ($isMethod) {
|
2010-10-10 02:48:34 +00:00
|
|
|
return $this->{$command}();
|
|
|
|
}
|
2010-10-10 04:21:16 +00:00
|
|
|
if ($isMain) {
|
2010-10-10 02:48:34 +00:00
|
|
|
return $this->main();
|
|
|
|
}
|
2010-10-16 05:24:23 +00:00
|
|
|
return $this->out($this->OptionParser->help($command));
|
2010-10-10 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2010-10-23 03:30:35 +00:00
|
|
|
/**
|
|
|
|
* Display the help in the correct format
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-10-23 04:36:40 +00:00
|
|
|
protected function _displayHelp($command) {
|
2010-10-23 03:30:35 +00:00
|
|
|
$format = 'text';
|
|
|
|
if (!empty($this->args[0]) && $this->args[0] == 'xml') {
|
|
|
|
$format = 'xml';
|
|
|
|
$this->output->outputAs(ConsoleOutput::RAW);
|
|
|
|
} else {
|
|
|
|
$this->_welcome();
|
|
|
|
}
|
|
|
|
return $this->out($this->OptionParser->help($command, $format));
|
|
|
|
}
|
|
|
|
|
2010-10-10 02:48:34 +00:00
|
|
|
/**
|
|
|
|
* Gets the option parser instance and configures it.
|
|
|
|
* By overriding this method you can configure the ConsoleOptionParser before returning it.
|
|
|
|
*
|
|
|
|
* @return ConsoleOptionParser
|
|
|
|
*/
|
2010-10-10 05:56:23 +00:00
|
|
|
public function getOptionParser() {
|
2010-10-10 02:48:34 +00:00
|
|
|
$parser = new ConsoleOptionParser($this->name);
|
|
|
|
return $parser;
|
|
|
|
}
|
|
|
|
|
2010-10-10 00:55:00 +00:00
|
|
|
/**
|
|
|
|
* Overload get for lazy building of tasks
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __get($name) {
|
|
|
|
if (empty($this->{$name}) && in_array($name, $this->taskNames)) {
|
|
|
|
$properties = $this->_taskMap[$name];
|
|
|
|
$this->{$name} = $this->Tasks->load($properties['class'], $properties['settings']);
|
2010-10-24 19:47:07 +00:00
|
|
|
$this->{$name}->args =& $this->args;
|
|
|
|
$this->{$name}->params =& $this->params;
|
2010-10-10 00:55:00 +00:00
|
|
|
$this->{$name}->initialize();
|
|
|
|
$this->{$name}->loadTasks();
|
|
|
|
}
|
|
|
|
return $this->{$name};
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Prompts the user for input, and returns it.
|
|
|
|
*
|
|
|
|
* @param string $prompt Prompt text.
|
|
|
|
* @param mixed $options Array or string of options.
|
|
|
|
* @param string $default Default input value.
|
|
|
|
* @return Either the default value, or the user-provided input.
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function in($prompt, $options = null, $default = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$this->interactive) {
|
|
|
|
return $default;
|
|
|
|
}
|
2010-10-05 03:26:38 +00:00
|
|
|
$in = $this->_getInput($prompt, $options, $default);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($options && is_string($options)) {
|
|
|
|
if (strpos($options, ',')) {
|
|
|
|
$options = explode(',', $options);
|
|
|
|
} elseif (strpos($options, '/')) {
|
|
|
|
$options = explode('/', $options);
|
|
|
|
} else {
|
|
|
|
$options = array($options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_array($options)) {
|
2009-07-23 21:04:40 +00:00
|
|
|
while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
|
2010-10-05 03:26:38 +00:00
|
|
|
$in = $this->_getInput($prompt, $options, $default);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($in) {
|
|
|
|
return $in;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-05 03:26:38 +00:00
|
|
|
/**
|
|
|
|
* Prompts the user for input, and returns it.
|
|
|
|
*
|
|
|
|
* @param string $prompt Prompt text.
|
|
|
|
* @param mixed $options Array or string of options.
|
|
|
|
* @param string $default Default input value.
|
|
|
|
* @return Either the default value, or the user-provided input.
|
|
|
|
*/
|
|
|
|
protected function _getInput($prompt, $options, $default) {
|
|
|
|
if (!is_array($options)) {
|
|
|
|
$printOptions = '';
|
|
|
|
} else {
|
|
|
|
$printOptions = '(' . implode('/', $options) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($default === null) {
|
2010-10-24 21:58:44 +00:00
|
|
|
$this->stdout->write('<question>' . $prompt . '</question>' . " $printOptions \n" . '> ', 0);
|
2010-10-05 03:26:38 +00:00
|
|
|
} else {
|
2010-10-24 21:58:44 +00:00
|
|
|
$this->stdout->write('<question>' . $prompt . '</question>' . " $printOptions \n" . "[$default] > ", 0);
|
2010-10-05 03:26:38 +00:00
|
|
|
}
|
|
|
|
$result = $this->stdin->read();
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
$this->_stop(1);
|
|
|
|
}
|
|
|
|
$result = trim($result);
|
|
|
|
|
|
|
|
if ($default != null && empty($result)) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2010-10-15 02:58:23 +00:00
|
|
|
/**
|
|
|
|
* Wrap a block of text.
|
2011-04-22 19:53:02 +00:00
|
|
|
* Allows you to set the width, and indenting on a block of text.
|
2010-10-15 02:58:23 +00:00
|
|
|
*
|
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `width` The width to wrap to. Defaults to 72
|
|
|
|
* - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
|
|
|
|
* - `indent` Indent the text with the string provided. Defaults to null.
|
|
|
|
*
|
|
|
|
* @param string $text Text the text to format.
|
2011-04-22 19:53:02 +00:00
|
|
|
* @param mixed $options Array of options to use, or an integer to wrap the text to.
|
2010-10-15 02:58:23 +00:00
|
|
|
* @return string Wrapped / indented text
|
2010-10-16 03:09:28 +00:00
|
|
|
* @see String::wrap()
|
2010-10-15 02:58:23 +00:00
|
|
|
*/
|
|
|
|
public function wrapText($text, $options = array()) {
|
2010-10-16 03:09:28 +00:00
|
|
|
return String::wrap($text, $options);
|
2010-10-15 02:58:23 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-09-26 21:02:54 +00:00
|
|
|
* Outputs a single or multiple messages to stdout. If no parameters
|
|
|
|
* are passed outputs just a newline.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-14 03:44:50 +00:00
|
|
|
* ### Output levels
|
|
|
|
*
|
|
|
|
* There are 3 built-in output level. Shell::QUIET, Shell::NORMAL, Shell::VERBOSE.
|
|
|
|
* The verbose and quiet output levels, map to the `verbose` and `quiet` output switches
|
|
|
|
* present in most shells. Using Shell::QUIET for a message means it will always display.
|
|
|
|
* While using Shell::VERBOSE means it will only display when verbose output is toggled.
|
|
|
|
*
|
2009-04-24 19:04:07 +00:00
|
|
|
* @param mixed $message A string or a an array of strings to output
|
2009-09-21 12:18:09 +00:00
|
|
|
* @param integer $newlines Number of newlines to append
|
2010-10-14 03:44:50 +00:00
|
|
|
* @param integer $level The message's output level, see above.
|
2009-10-15 04:23:54 +00:00
|
|
|
* @return integer Returns the number of bytes returned from writing to stdout.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-10-14 03:40:45 +00:00
|
|
|
public function out($message = null, $newlines = 1, $level = Shell::NORMAL) {
|
|
|
|
$currentLevel = Shell::NORMAL;
|
|
|
|
if (!empty($this->params['verbose'])) {
|
|
|
|
$currentLevel = Shell::VERBOSE;
|
|
|
|
}
|
|
|
|
if (!empty($this->params['quiet'])) {
|
|
|
|
$currentLevel = Shell::QUIET;
|
|
|
|
}
|
|
|
|
if ($level <= $currentLevel) {
|
|
|
|
return $this->stdout->write($message, $newlines);
|
|
|
|
}
|
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-09-26 21:02:54 +00:00
|
|
|
* Outputs a single or multiple error messages to stderr. If no parameters
|
|
|
|
* are passed outputs just a newline.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-04-24 19:04:07 +00:00
|
|
|
* @param mixed $message A string or a an array of strings to output
|
2009-09-21 12:18:09 +00:00
|
|
|
* @param integer $newlines Number of newlines to append
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function err($message = null, $newlines = 1) {
|
2010-10-04 04:40:04 +00:00
|
|
|
$this->stderr->write($message, $newlines);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-04-24 19:04:07 +00:00
|
|
|
* Returns a single or multiple linefeeds sequences.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-09-21 12:18:09 +00:00
|
|
|
* @param integer $multiplier Number of times the linefeed sequence should be repeated
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
2009-04-24 19:04:07 +00:00
|
|
|
* @return string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-09-21 12:18:09 +00:00
|
|
|
function nl($multiplier = 1) {
|
2010-10-04 04:40:04 +00:00
|
|
|
return str_repeat(ConsoleOutput::LF, $multiplier);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-04-24 19:04:07 +00:00
|
|
|
* Outputs a series of minus characters to the standard output, acts as a visual separator.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-09-21 12:18:09 +00:00
|
|
|
* @param integer $newlines Number of newlines to pre- and append
|
2010-10-04 03:58:07 +00:00
|
|
|
* @param integer $width Width of the line, defaults to 63
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-10-04 03:58:07 +00:00
|
|
|
public function hr($newlines = 0, $width = 63) {
|
2009-09-21 12:18:09 +00:00
|
|
|
$this->out(null, $newlines);
|
2010-10-04 03:58:07 +00:00
|
|
|
$this->out(str_repeat('-', $width));
|
2009-09-21 12:18:09 +00:00
|
|
|
$this->out(null, $newlines);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-21 12:18:09 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-04-16 09:00:47 +00:00
|
|
|
* Displays a formatted error message
|
|
|
|
* and exits the application with status code 1
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-04-16 09:00:47 +00:00
|
|
|
* @param string $title Title of the error
|
|
|
|
* @param string $message An optional error message
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function error($title, $message = null) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', '<error>Error:</error> %s', $title));
|
2009-04-16 09:00:47 +00:00
|
|
|
|
|
|
|
if (!empty($message)) {
|
|
|
|
$this->err($message);
|
|
|
|
}
|
|
|
|
$this->_stop(1);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-04 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Clear the console
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function clear() {
|
|
|
|
if (empty($this->params['noclear'])) {
|
|
|
|
if ( DS === '/') {
|
|
|
|
passthru('clear');
|
|
|
|
} else {
|
|
|
|
passthru('cls');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a file at given path
|
|
|
|
*
|
|
|
|
* @param string $path Where to put the file.
|
|
|
|
* @param string $contents Content to put in the file.
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function createFile($path, $contents) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$path = str_replace(DS . DS, DS, $path);
|
2009-09-26 21:36:45 +00:00
|
|
|
|
|
|
|
$this->out();
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_file($path) && $this->interactive === true) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', '<warning>File `%s` exists</warning>', $path));
|
|
|
|
$key = $this->in(__d('cake_console', 'Do you want to overwrite?'), array('y', 'n', 'q'), 'n');
|
2009-09-26 21:36:45 +00:00
|
|
|
|
2009-07-23 21:04:40 +00:00
|
|
|
if (strtolower($key) == 'q') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', '<error>Quitting</error>.'), 2);
|
2009-09-26 21:36:45 +00:00
|
|
|
$this->_stop();
|
2009-07-23 21:04:40 +00:00
|
|
|
} elseif (strtolower($key) != 'y') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Skip `%s`', $path), 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-10-24 21:58:44 +00:00
|
|
|
} else {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'Creating file %s', $path));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-24 21:58:44 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($File = new File($path, true)) {
|
|
|
|
$data = $File->prepare($contents);
|
|
|
|
$File->write($data);
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', '<success>Wrote</success> `%s`', $path));
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Action to create a Unit Test
|
|
|
|
*
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _checkUnitTest() {
|
2011-02-05 02:56:24 +00:00
|
|
|
if (App::import('Vendor', 'phpunit', array('file' => 'PHPUnit' . DS . 'Autoload.php'))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (@include 'PHPUnit' . DS . 'Autoload.php') {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-18 01:29:30 +00:00
|
|
|
$prompt = 'PHPUnit is not installed. Do you want to bake unit test files anyway?';
|
2009-09-26 21:36:45 +00:00
|
|
|
$unitTest = $this->in($prompt, array('y','n'), 'y');
|
2009-07-23 21:04:40 +00:00
|
|
|
$result = strtolower($unitTest) == 'y' || strtolower($unitTest) == 'yes';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($result) {
|
2009-09-26 21:36:45 +00:00
|
|
|
$this->out();
|
2010-10-18 01:29:30 +00:00
|
|
|
$this->out('You can download PHPUnit from http://phpunit.de');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Makes absolute file path easier to read
|
|
|
|
*
|
|
|
|
* @param string $file Absolute file path
|
|
|
|
* @return sting short path
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function shortPath($file) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$shortPath = str_replace(ROOT, null, $file);
|
2009-07-23 20:51:24 +00:00
|
|
|
$shortPath = str_replace('..' . DS, '', $shortPath);
|
|
|
|
return str_replace(DS . DS, DS, $shortPath);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates the proper controller path for the specified controller class name
|
|
|
|
*
|
|
|
|
* @param string $name Controller class name
|
|
|
|
* @return string Path to controller
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _controllerPath($name) {
|
2011-03-16 04:14:37 +00:00
|
|
|
return Inflector::underscore($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates the proper controller plural name for the specified controller class name
|
|
|
|
*
|
|
|
|
* @param string $name Controller class name
|
|
|
|
* @return string Controller plural name
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _controllerName($name) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return Inflector::pluralize(Inflector::camelize($name));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates the proper controller camelized name (singularized) for the specified name
|
|
|
|
*
|
|
|
|
* @param string $name Name
|
|
|
|
* @return string Camelized and singularized controller name
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _modelName($name) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return Inflector::camelize(Inflector::singularize($name));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-11 03:58:22 +00:00
|
|
|
* Creates the proper underscored model key for associations
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-11 03:58:22 +00:00
|
|
|
* @param string $name Model class name
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Singular model key
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _modelKey($name) {
|
2010-05-11 03:58:22 +00:00
|
|
|
return Inflector::underscore($name) . '_id';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates the proper model name from a foreign key
|
|
|
|
*
|
|
|
|
* @param string $key Foreign key
|
|
|
|
* @return string Model name
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _modelNameFromKey($key) {
|
2009-09-26 21:36:45 +00:00
|
|
|
return Inflector::camelize(str_replace('_id', '', $key));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* creates the singular name for use in views.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string $name
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _singularName($name) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return Inflector::variable(Inflector::singularize($name));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates the plural name for views
|
|
|
|
*
|
|
|
|
* @param string $name Name to use
|
|
|
|
* @return string Plural name for views
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _pluralName($name) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return Inflector::variable(Inflector::pluralize($name));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates the singular human name used in views
|
|
|
|
*
|
|
|
|
* @param string $name Controller name
|
|
|
|
* @return string Singular human name
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _singularHumanName($name) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates the plural human name used in views
|
|
|
|
*
|
|
|
|
* @param string $name Controller name
|
|
|
|
* @return string Plural human name
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _pluralHumanName($name) {
|
2010-04-04 21:06:31 +00:00
|
|
|
return Inflector::humanize(Inflector::underscore($name));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-06-06 23:59:58 +00:00
|
|
|
/**
|
|
|
|
* Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
|
|
|
|
*
|
|
|
|
* @param string $pluginName Name of the plugin you want ie. DebugKit
|
|
|
|
* @return string $path path to the correct plugin.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-06-06 23:59:58 +00:00
|
|
|
function _pluginPath($pluginName) {
|
2009-10-14 13:47:07 +00:00
|
|
|
return App::pluginPath($pluginName);
|
2009-06-06 23:59:58 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|