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
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs
|
|
|
|
* @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-07-08 01:35:03 +00:00
|
|
|
App::import('Shell', 'TaskCollection');
|
2010-10-04 04:40:04 +00:00
|
|
|
require_once CAKE . 'console' . DS . 'console_output.php';
|
2010-10-05 03:26:38 +00:00
|
|
|
require_once CAKE . 'console' . DS . 'console_input.php';
|
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.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Shell extends Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* An instance of the ShellDispatcher object that loaded this script
|
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @var ShellDispatcher
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $Dispatch = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2010-10-10 00:55:00 +00:00
|
|
|
/**
|
|
|
|
* Shell paths
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $shellPaths = array();
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The file name of the shell that was invoked.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $shell = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The class name of the shell that was invoked.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $className = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The command called if public methods are available.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $command = null;
|
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-10-29 19:25:49 +00:00
|
|
|
/**
|
|
|
|
* An alias for the shell
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $alias = 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-05 03:26:38 +00:00
|
|
|
function __construct(&$dispatch, $stdout = null, $stderr = null, $stdin = null) {
|
2010-10-04 04:40:04 +00:00
|
|
|
$vars = array('params', 'args', 'shell', 'shellCommand' => 'command', 'shellPaths');
|
2009-09-26 21:36:45 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($vars as $key => $var) {
|
|
|
|
if (is_string($key)) {
|
|
|
|
$this->{$var} =& $dispatch->{$key};
|
|
|
|
} else {
|
|
|
|
$this->{$var} =& $dispatch->{$var};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->name == null) {
|
2008-10-29 19:25:49 +00:00
|
|
|
$this->name = get_class($this);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-29 19:25:49 +00:00
|
|
|
if ($this->alias == null) {
|
|
|
|
$this->alias = $this->name;
|
|
|
|
}
|
2010-10-04 04:40:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Dispatch =& $dispatch;
|
2010-10-10 00:55:00 +00:00
|
|
|
$this->Tasks = new TaskCollection($this, $dispatch);
|
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');
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +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() {
|
2010-10-04 04:40:04 +00:00
|
|
|
$this->clear();
|
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();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->out('App : '. $this->params['app']);
|
|
|
|
$this->out('Path: '. $this->params['working']);
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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) {
|
|
|
|
if (!empty($command) && $this->hasTask($command)) {
|
2010-10-10 03:30:56 +00:00
|
|
|
$command = Inflector::camelize($command);
|
2010-10-10 03:42:38 +00:00
|
|
|
array_shift($argv);
|
2010-10-10 02:48:34 +00:00
|
|
|
return $this->{$command}->runCommand('execute', $argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->parser = $this->_getOptionParser();
|
|
|
|
list($this->params, $this->args) = $this->parser->parse($argv);
|
|
|
|
if (isset($this->params['help'])) {
|
|
|
|
return $this->out($this->parser->help());
|
|
|
|
}
|
|
|
|
if ($this->hasMethod($command)) {
|
2010-10-10 03:42:38 +00:00
|
|
|
array_shift($argv);
|
2010-10-10 02:48:34 +00:00
|
|
|
return $this->{$command}();
|
|
|
|
}
|
|
|
|
if ($this->hasMethod('main')) {
|
|
|
|
return $this->main();
|
|
|
|
}
|
|
|
|
throw new RuntimeException(sprintf(__('Unhandled method `%s`'), $command));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the option parser instance and configures it.
|
|
|
|
* By overriding this method you can configure the ConsoleOptionParser before returning it.
|
|
|
|
*
|
|
|
|
* @return ConsoleOptionParser
|
|
|
|
*/
|
|
|
|
protected function _getOptionParser() {
|
|
|
|
$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']);
|
|
|
|
$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) {
|
|
|
|
$this->stdout->write($prompt . " $printOptions \n" . '> ', 0);
|
|
|
|
} else {
|
|
|
|
$this->stdout->write($prompt . " $printOptions \n" . "[$default] > ", 0);
|
|
|
|
}
|
|
|
|
$result = $this->stdin->read();
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
$this->_stop(1);
|
|
|
|
}
|
|
|
|
$result = trim($result);
|
|
|
|
|
|
|
|
if ($default != null && empty($result)) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
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
|
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-04-05 03:19:38 +00:00
|
|
|
public function out($message = null, $newlines = 1) {
|
2010-10-04 04:40:04 +00:00
|
|
|
return $this->stdout->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-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) {
|
2010-10-04 01:53:23 +00:00
|
|
|
$this->err(sprintf(__('<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
|
|
|
/**
|
|
|
|
* Will check the number args matches otherwise throw an error
|
|
|
|
*
|
|
|
|
* @param integer $expectedNum Expected number of paramters
|
|
|
|
* @param string $command Command
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _checkArgs($expectedNum, $command = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$command) {
|
|
|
|
$command = $this->command;
|
|
|
|
}
|
|
|
|
if (count($this->args) < $expectedNum) {
|
2009-09-26 21:36:45 +00:00
|
|
|
$message[] = "Got: " . count($this->args);
|
|
|
|
$message[] = "Expected: {$expectedNum}";
|
|
|
|
$message[] = "Please type `cake {$this->shell} help` for help";
|
|
|
|
$message[] = "on usage of the {$this->name} {$command}.";
|
|
|
|
$this->error('Wrong number of parameters', $message);
|
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 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();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('Creating file %s'), $path));
|
2009-09-26 21:36:45 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_file($path) && $this->interactive === true) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$prompt = sprintf(__('File `%s` exists, overwrite?'), $path);
|
2009-09-26 21:36:45 +00:00
|
|
|
$key = $this->in($prompt, array('y', 'n', 'q'), 'n');
|
|
|
|
|
2009-07-23 21:04:40 +00:00
|
|
|
if (strtolower($key) == 'q') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('Quitting.'), 2);
|
2009-09-26 21:36:45 +00:00
|
|
|
$this->_stop();
|
2009-07-23 21:04:40 +00:00
|
|
|
} elseif (strtolower($key) != 'y') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('Skip `%s`'), $path), 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!class_exists('File')) {
|
2009-07-23 20:53:37 +00:00
|
|
|
require LIBS . 'file.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($File = new File($path, true)) {
|
|
|
|
$data = $File->prepare($contents);
|
|
|
|
$File->write($data);
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('Wrote `%s`'), $path));
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->err(sprintf(__('Could not write to `%s`.'), $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
|
|
|
/**
|
|
|
|
* Outputs usage text on the standard output. Implement it in subclasses.
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function help() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->command != null) {
|
2009-09-26 21:36:45 +00:00
|
|
|
$this->err("Unknown {$this->name} command `{$this->command}`.");
|
|
|
|
$this->err("For usage, try `cake {$this->shell} help`.", 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$this->Dispatch->help();
|
|
|
|
}
|
|
|
|
}
|
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() {
|
2008-10-09 17:42:10 +00:00
|
|
|
if (App::import('vendor', 'simpletest' . DS . 'simpletest')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-26 21:36:45 +00:00
|
|
|
$prompt = 'SimpleTest is not installed. Do you want to bake unit test files anyway?';
|
|
|
|
$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();
|
|
|
|
$this->out('You can download SimpleTest from http://simpletest.org');
|
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) {
|
2009-07-23 21:04:40 +00:00
|
|
|
return strtolower(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
|
|
|
}
|