2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Base class for Shells
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) 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
|
|
|
* @since CakePHP(tm) v 1.2.0.5012
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
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');
|
2013-03-21 00:37:13 +00:00
|
|
|
App::uses('ClassRegistry', 'Utility');
|
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.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console
|
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
|
|
|
/**
|
2012-11-28 22:30:47 +00:00
|
|
|
* Output constant making verbose shells.
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2010-10-14 03:40:45 +00:00
|
|
|
*/
|
|
|
|
const VERBOSE = 2;
|
2012-11-28 22:30:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output constant for making normal shells.
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2012-11-28 22:30:47 +00:00
|
|
|
*/
|
2010-10-14 03:40:45 +00:00
|
|
|
const NORMAL = 1;
|
2012-11-28 22:30:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output constants for making quiet shells.
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2012-11-28 22:30:47 +00:00
|
|
|
*/
|
2010-10-14 03:40:45 +00:00
|
|
|
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.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-12-11 02:31:21 +00:00
|
|
|
/**
|
|
|
|
* The name of the plugin the shell belongs to.
|
|
|
|
* Is automatically set by ShellDispatcher when a shell is constructed.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $plugin = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains tasks to load and instantiate
|
|
|
|
*
|
|
|
|
* @var array
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::$tasks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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
|
|
|
|
*/
|
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
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::$uses
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $uses = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-03-20 06:52:01 +00:00
|
|
|
/**
|
|
|
|
* This shell's primary model class name, the first model in the $uses property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $modelClass = null;
|
|
|
|
|
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;
|
|
|
|
|
2014-04-29 00:18:27 +00:00
|
|
|
/**
|
|
|
|
* The number of bytes last written to the output stream
|
|
|
|
* used when overwriting the previous message.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $_lastWritten = 0;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructs this Shell instance.
|
|
|
|
*
|
2011-07-26 01:46:52 +00:00
|
|
|
* @param ConsoleOutput $stdout A ConsoleOutput object for stdout.
|
|
|
|
* @param ConsoleOutput $stderr A ConsoleOutput object for stderr.
|
|
|
|
* @param ConsoleInput $stdin A ConsoleInput object for stdin.
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
public function __construct($stdout = null, $stderr = null, $stdin = null) {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$this->name) {
|
2011-05-29 10:32:30 +00:00
|
|
|
$this->name = Inflector::camelize(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
|
|
|
|
2012-12-18 04:38:47 +00:00
|
|
|
$this->stdout = $stdout ? $stdout : new ConsoleOutput('php://stdout');
|
|
|
|
$this->stderr = $stderr ? $stderr : new ConsoleOutput('php://stderr');
|
|
|
|
$this->stdin = $stdin ? $stdin : new ConsoleInput('php://stdin');
|
|
|
|
|
2012-07-06 20:30:13 +00:00
|
|
|
$this->_useLogger();
|
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
|
|
|
}
|
2012-03-20 06:52:01 +00:00
|
|
|
if (!empty($this->uses)) {
|
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
|
|
|
|
*
|
2011-07-26 01:46:52 +00:00
|
|
|
* @return void
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::initialize
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function initialize() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_loadModels();
|
2013-09-27 10:53:18 +00:00
|
|
|
$this->loadTasks();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-07-26 01:46:52 +00:00
|
|
|
* Starts up the Shell and displays the welcome message.
|
|
|
|
* Allows for checking and configuring prior to command or main execution
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 01:46:52 +00:00
|
|
|
* Override this method if you want to remove the welcome information,
|
|
|
|
* or otherwise modify the pre-command flow.
|
|
|
|
*
|
|
|
|
* @return void
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::startup
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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
|
|
|
|
*
|
2011-07-26 01:46:52 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _welcome() {
|
2009-09-26 21:36:45 +00:00
|
|
|
$this->out();
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out(__d('cake_console', '<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
|
2009-09-21 20:52:08 +00:00
|
|
|
$this->hr();
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out(__d('cake_console', 'App : %s', APP_DIR));
|
2011-07-31 18:49:33 +00:00
|
|
|
$this->out(__d('cake_console', 'Path: %s', APP));
|
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
|
|
|
/**
|
2012-03-20 06:52:01 +00:00
|
|
|
* If $uses is an array load each of the models in the array
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _loadModels() {
|
2012-03-20 06:52:01 +00:00
|
|
|
if (is_array($this->uses)) {
|
|
|
|
list(, $this->modelClass) = pluginSplit(current($this->uses));
|
|
|
|
foreach ($this->uses as $modelClass) {
|
|
|
|
$this->loadModel($modelClass);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-20 06:52:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-03-20 06:52:01 +00:00
|
|
|
/**
|
|
|
|
* Lazy loads models using the loadModel() method if declared in $uses
|
|
|
|
*
|
2014-05-28 03:16:05 +00:00
|
|
|
* @param string $name The name of the model to look for.
|
2012-03-20 06:52:01 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __isset($name) {
|
|
|
|
if (is_array($this->uses)) {
|
|
|
|
foreach ($this->uses as $modelClass) {
|
|
|
|
list(, $class) = pluginSplit($modelClass);
|
|
|
|
if ($name === $class) {
|
|
|
|
return $this->loadModel($modelClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-10 02:14:57 +00:00
|
|
|
|
2012-03-20 06:52:01 +00:00
|
|
|
/**
|
|
|
|
* Loads and instantiates models required by this shell.
|
|
|
|
*
|
|
|
|
* @param string $modelClass Name of model class to load
|
|
|
|
* @param mixed $id Initial ID the instanced model class should have
|
|
|
|
* @return mixed true when single model found and instance created, error returned if model not found.
|
|
|
|
* @throws MissingModelException if the model class cannot be found.
|
|
|
|
*/
|
|
|
|
public function loadModel($modelClass = null, $id = null) {
|
|
|
|
if ($modelClass === null) {
|
|
|
|
$modelClass = $this->modelClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->uses = ($this->uses) ? (array)$this->uses : array();
|
|
|
|
if (!in_array($modelClass, $this->uses)) {
|
|
|
|
$this->uses[] = $modelClass;
|
2012-12-18 04:38:47 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-03-20 06:52:01 +00:00
|
|
|
list($plugin, $modelClass) = pluginSplit($modelClass, true);
|
|
|
|
if (!isset($this->modelClass)) {
|
|
|
|
$this->modelClass = $modelClass;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-12-18 04:38:47 +00:00
|
|
|
|
2012-03-20 06:52:01 +00:00
|
|
|
$this->{$modelClass} = ClassRegistry::init(array(
|
|
|
|
'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id
|
|
|
|
));
|
|
|
|
if (!$this->{$modelClass}) {
|
|
|
|
throw new MissingModelException($modelClass);
|
|
|
|
}
|
2012-12-18 04:38:47 +00:00
|
|
|
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
|
|
|
/**
|
2010-04-04 07:14:00 +00:00
|
|
|
* Loads tasks defined in public $tasks
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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);
|
2012-12-20 12:47:03 +00:00
|
|
|
$this->taskNames = array_merge($this->taskNames, array_keys($this->_taskMap));
|
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.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::hasTask
|
2010-10-10 01:40:16 +00:00
|
|
|
*/
|
|
|
|
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.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::hasMethod
|
2010-10-10 01:40:16 +00:00
|
|
|
*/
|
|
|
|
public function hasMethod($name) {
|
2010-10-10 01:52:12 +00:00
|
|
|
try {
|
2011-11-06 21:32:51 +00:00
|
|
|
$method = new ReflectionMethod($this, $name);
|
2010-10-10 01:52:12 +00:00
|
|
|
if (!$method->isPublic() || substr($name, 0, 1) === '_') {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($method->getDeclaringClass()->name === 'Shell') {
|
2010-10-10 01:52:12 +00:00
|
|
|
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
|
|
|
*
|
2011-04-25 19:17:59 +00:00
|
|
|
* With a string command:
|
2010-10-16 05:37:43 +00:00
|
|
|
*
|
|
|
|
* `return $this->dispatchShell('schema create DbAcl');`
|
|
|
|
*
|
2011-07-26 01:46:52 +00:00
|
|
|
* Avoid using this form if you have string arguments, with spaces in them.
|
|
|
|
* The dispatched will be invoked incorrectly. Only use this form for simple
|
|
|
|
* command dispatching.
|
|
|
|
*
|
2010-10-16 05:37:43 +00:00
|
|
|
* 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-07-29 02:03:44 +00:00
|
|
|
* @return mixed The return of the other shell.
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::dispatchShell
|
2010-10-16 05:28:59 +00:00
|
|
|
*/
|
2010-10-16 05:37:43 +00:00
|
|
|
public function dispatchShell() {
|
|
|
|
$args = func_get_args();
|
2012-09-14 18:26:00 +00:00
|
|
|
if (is_string($args[0]) && count($args) === 1) {
|
2010-10-16 05:37:43 +00:00
|
|
|
$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
|
|
|
/**
|
2011-07-26 01:46:52 +00:00
|
|
|
* Runs the Shell with the provided argv.
|
|
|
|
*
|
|
|
|
* Delegates calls to Tasks and resolves methods inside the class. Commands are looked
|
|
|
|
* up with the following order:
|
|
|
|
*
|
|
|
|
* - Method on the shell.
|
|
|
|
* - Matching task name.
|
|
|
|
* - `main()` method.
|
|
|
|
*
|
|
|
|
* If a shell implements a `main()` method, all missing method calls will be sent to
|
|
|
|
* `main()` with the original method name in the argv.
|
2010-10-10 02:48:34 +00:00
|
|
|
*
|
2011-07-26 01:46:52 +00:00
|
|
|
* @param string $command The command name to run on this shell. If this argument is empty,
|
|
|
|
* and the shell has a `main()` method, that will be called instead.
|
2010-10-10 02:48:34 +00:00
|
|
|
* @param array $argv Array of arguments to run the shell with. This array should be missing the shell name.
|
|
|
|
* @return void
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::runCommand
|
2010-10-10 02:48:34 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2012-08-28 02:07:28 +00:00
|
|
|
$this->OptionParser = $this->getOptionParser();
|
2011-04-22 21:16:32 +00:00
|
|
|
try {
|
|
|
|
list($this->params, $this->args) = $this->OptionParser->parse($argv, $command);
|
|
|
|
} catch (ConsoleException $e) {
|
2011-10-04 03:08:48 +00:00
|
|
|
$this->out($this->OptionParser->help($command));
|
|
|
|
return false;
|
2011-04-22 21:16:32 +00:00
|
|
|
}
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2012-07-06 20:30:13 +00:00
|
|
|
if (!empty($this->params['quiet'])) {
|
|
|
|
$this->_useLogger(false);
|
|
|
|
}
|
2012-11-22 17:40:46 +00:00
|
|
|
if (!empty($this->params['plugin'])) {
|
|
|
|
CakePlugin::load($this->params['plugin']);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:31 +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();
|
|
|
|
}
|
2011-10-04 03:08:48 +00:00
|
|
|
$this->out($this->OptionParser->help($command));
|
|
|
|
return false;
|
2010-10-10 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2010-10-23 03:30:35 +00:00
|
|
|
/**
|
|
|
|
* Display the help in the correct format
|
|
|
|
*
|
2014-05-28 03:16:05 +00:00
|
|
|
* @param string $command The command to get help for.
|
2010-10-23 03:30:35 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-10-23 04:36:40 +00:00
|
|
|
protected function _displayHelp($command) {
|
2010-10-23 03:30:35 +00:00
|
|
|
$format = 'text';
|
2013-02-12 02:38:08 +00:00
|
|
|
if (!empty($this->args[0]) && $this->args[0] === 'xml') {
|
2010-10-23 03:30:35 +00:00
|
|
|
$format = 'xml';
|
2011-08-06 20:26:58 +00:00
|
|
|
$this->stdout->outputAs(ConsoleOutput::RAW);
|
2010-10-23 03:30:35 +00:00
|
|
|
} 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.
|
2013-12-05 23:38:32 +00:00
|
|
|
*
|
2010-10-10 02:48:34 +00:00
|
|
|
* By overriding this method you can configure the ConsoleOptionParser before returning it.
|
|
|
|
*
|
|
|
|
* @return ConsoleOptionParser
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::getOptionParser
|
2010-10-10 02:48:34 +00:00
|
|
|
*/
|
2010-10-10 05:56:23 +00:00
|
|
|
public function getOptionParser() {
|
2011-12-11 02:31:21 +00:00
|
|
|
$name = ($this->plugin ? $this->plugin . '.' : '') . $this->name;
|
|
|
|
$parser = new ConsoleOptionParser($name);
|
2010-10-10 02:48:34 +00:00
|
|
|
return $parser;
|
|
|
|
}
|
|
|
|
|
2010-10-10 00:55:00 +00:00
|
|
|
/**
|
|
|
|
* Overload get for lazy building of tasks
|
|
|
|
*
|
2014-05-28 03:16:05 +00:00
|
|
|
* @param string $name The property name to access.
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return Shell Object of Task
|
2010-10-10 00:55:00 +00:00
|
|
|
*/
|
|
|
|
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.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $options Array or string of options.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $default Default input value.
|
2011-07-29 03:24:31 +00:00
|
|
|
* @return mixed Either the default value, or the user-provided input.
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::in
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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;
|
|
|
|
}
|
2012-01-28 16:45:44 +00:00
|
|
|
$originalOptions = $options;
|
|
|
|
$in = $this->_getInput($prompt, $originalOptions, $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)) {
|
2012-01-18 02:57:22 +00:00
|
|
|
$options = array_merge(
|
|
|
|
array_map('strtolower', $options),
|
|
|
|
array_map('strtoupper', $options),
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
while ($in === '' || !in_array($in, $options)) {
|
2012-01-28 16:45:44 +00:00
|
|
|
$in = $this->_getInput($prompt, $originalOptions, $default);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-10 13:37:06 +00:00
|
|
|
return $in;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $options Array or string of options.
|
2010-10-05 03:26:38 +00:00
|
|
|
* @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) {
|
2013-07-01 12:15:54 +00:00
|
|
|
return $this->_stop(1);
|
2010-10-05 03:26:38 +00:00
|
|
|
}
|
|
|
|
$result = trim($result);
|
|
|
|
|
2011-10-10 13:37:06 +00:00
|
|
|
if ($default !== null && ($result === '' || $result === null)) {
|
2010-10-05 03:26:38 +00:00
|
|
|
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
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `width` The width to wrap to. Defaults to 72
|
2010-10-15 02:58:23 +00:00
|
|
|
* - `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.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param string|int|array $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
|
2015-01-05 00:00:57 +00:00
|
|
|
* @see CakeText::wrap()
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::wrapText
|
2010-10-15 02:58:23 +00:00
|
|
|
*/
|
|
|
|
public function wrapText($text, $options = array()) {
|
2015-01-05 00:00:57 +00:00
|
|
|
return CakeText::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
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* There are 3 built-in output level. Shell::QUIET, Shell::NORMAL, Shell::VERBOSE.
|
2010-10-14 03:44:50 +00:00
|
|
|
* The verbose and quiet output levels, map to the `verbose` and `quiet` output switches
|
2013-07-05 12:36:40 +00:00
|
|
|
* present in most shells. Using Shell::QUIET for a message means it will always display.
|
2010-10-14 03:44:50 +00:00
|
|
|
* While using Shell::VERBOSE means it will only display when verbose output is toggled.
|
|
|
|
*
|
2014-07-30 20:11:03 +00:00
|
|
|
* @param string|array $message A string or an array of strings to output
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $newlines Number of newlines to append
|
|
|
|
* @param int $level The message's output level, see above.
|
|
|
|
* @return int|bool Returns the number of bytes returned from writing to stdout.
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::out
|
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) {
|
2014-04-29 00:18:27 +00:00
|
|
|
$this->_lastWritten = $this->stdout->write($message, $newlines);
|
|
|
|
return $this->_lastWritten;
|
2010-10-14 03:40:45 +00:00
|
|
|
}
|
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2014-04-29 00:18:27 +00:00
|
|
|
/**
|
|
|
|
* Overwrite some already output text.
|
|
|
|
*
|
|
|
|
* Useful for building progress bars, or when you want to replace
|
|
|
|
* text already output to the screen with new text.
|
|
|
|
*
|
|
|
|
* **Warning** You cannot overwrite text that contains newlines.
|
|
|
|
*
|
|
|
|
* @param array|string $message The message to output.
|
|
|
|
* @param int $newlines Number of newlines to append.
|
|
|
|
* @param int $size The number of bytes to overwrite. Defaults to the
|
|
|
|
* length of the last message output.
|
|
|
|
* @return int|bool Returns the number of bytes returned from writing to stdout.
|
|
|
|
*/
|
|
|
|
public function overwrite($message, $newlines = 1, $size = null) {
|
|
|
|
$size = $size ? $size : $this->_lastWritten;
|
|
|
|
|
|
|
|
// Output backspaces.
|
|
|
|
$this->out(str_repeat("\x08", $size), 0);
|
|
|
|
|
|
|
|
$newBytes = $this->out($message, 0);
|
|
|
|
|
|
|
|
// Fill any remaining bytes with spaces.
|
|
|
|
$fill = $size - $newBytes;
|
|
|
|
if ($fill > 0) {
|
|
|
|
$this->out(str_repeat(' ', $fill), 0);
|
|
|
|
}
|
|
|
|
if ($newlines) {
|
|
|
|
$this->out($this->nl($newlines), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2014-07-30 20:11:03 +00:00
|
|
|
* @param string|array $message A string or an array of strings to output
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $newlines Number of newlines to append
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return void
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::err
|
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
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $multiplier Number of times the linefeed sequence should be repeated
|
2009-04-24 19:04:07 +00:00
|
|
|
* @return string
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::nl
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public 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
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $newlines Number of newlines to pre- and append
|
|
|
|
* @param int $width Width of the line, defaults to 63
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return void
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::hr
|
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
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return void
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::error
|
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);
|
|
|
|
}
|
2013-07-01 12:15:54 +00:00
|
|
|
return $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
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::clear
|
2010-10-04 04:40:04 +00:00
|
|
|
*/
|
|
|
|
public function clear() {
|
|
|
|
if (empty($this->params['noclear'])) {
|
2011-04-25 19:17:59 +00:00
|
|
|
if (DS === '/') {
|
2010-10-04 04:40:04 +00:00
|
|
|
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.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::createFile
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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();
|
|
|
|
|
2013-07-18 11:09:00 +00:00
|
|
|
if (is_file($path) && empty($this->params['force']) && $this->interactive === true) {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', '<warning>File `%s` exists</warning>', $path));
|
2011-04-25 19:17:59 +00:00
|
|
|
$key = $this->in(__d('cake_console', 'Do you want to overwrite?'), array('y', 'n', 'q'), 'n');
|
2009-09-26 21:36:45 +00:00
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtolower($key) === 'q') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', '<error>Quitting</error>.'), 2);
|
2013-07-01 12:15:54 +00:00
|
|
|
return $this->_stop();
|
2013-02-12 02:38:08 +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
|
|
|
|
2011-06-21 17:44:42 +00:00
|
|
|
$File = new File($path, true);
|
2011-12-14 02:59:58 +00:00
|
|
|
if ($File->exists() && $File->writable()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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;
|
|
|
|
}
|
2012-12-21 15:05:32 +00:00
|
|
|
|
2012-12-18 04:38:47 +00:00
|
|
|
$this->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2);
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Action to create a Unit Test
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _checkUnitTest() {
|
2012-05-19 15:35:55 +00:00
|
|
|
if (class_exists('PHPUnit_Framework_TestCase')) {
|
2011-02-05 02:56:24 +00:00
|
|
|
return true;
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2012-05-19 15:35:55 +00:00
|
|
|
} elseif (@include 'PHPUnit' . DS . 'Autoload.php') {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2012-05-19 15:35:55 +00:00
|
|
|
return true;
|
|
|
|
} elseif (App::import('Vendor', 'phpunit', array('file' => 'PHPUnit' . DS . 'Autoload.php'))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-19 15:35:55 +00:00
|
|
|
|
2011-04-25 19:17:59 +00:00
|
|
|
$prompt = __d('cake_console', 'PHPUnit is not installed. Do you want to bake unit test files anyway?');
|
2011-12-01 07:21:31 +00:00
|
|
|
$unitTest = $this->in($prompt, array('y', 'n'), 'y');
|
2013-02-12 02:38:08 +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();
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out(__d('cake_console', 'You can download PHPUnit from %s', '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
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return string short path
|
2011-11-22 05:33:07 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::shortPath
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2011-12-05 11:21:18 +00:00
|
|
|
* Creates the proper model camelized name (singularized) for the specified name
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $name Name
|
2011-12-05 11:21:18 +00:00
|
|
|
* @return string Camelized and singularized model name
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2014-05-28 03:16:05 +00:00
|
|
|
* @param string $name The plural underscored value.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return string name
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return string path path to the correct plugin.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _pluginPath($pluginName) {
|
2011-05-12 04:18:51 +00:00
|
|
|
if (CakePlugin::loaded($pluginName)) {
|
|
|
|
return CakePlugin::path($pluginName);
|
|
|
|
}
|
|
|
|
return current(App::path('plugins')) . $pluginName . DS;
|
2009-06-06 23:59:58 +00:00
|
|
|
}
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2012-07-06 20:30:13 +00:00
|
|
|
/**
|
|
|
|
* Used to enable or disable logging stream output to stdout and stderr
|
|
|
|
* If you don't wish to see in your stdout or stderr everything that is logged
|
|
|
|
* through CakeLog, call this function with first param as false
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $enable whether to enable CakeLog output or not
|
2012-07-06 20:30:13 +00:00
|
|
|
* @return void
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-07-06 20:30:13 +00:00
|
|
|
protected function _useLogger($enable = true) {
|
|
|
|
if (!$enable) {
|
|
|
|
CakeLog::drop('stdout');
|
|
|
|
CakeLog::drop('stderr');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CakeLog::config('stdout', array(
|
2013-06-21 14:43:01 +00:00
|
|
|
'engine' => 'Console',
|
2012-07-06 20:30:13 +00:00
|
|
|
'types' => array('notice', 'info'),
|
|
|
|
'stream' => $this->stdout,
|
|
|
|
));
|
|
|
|
CakeLog::config('stderr', array(
|
2013-06-21 14:43:01 +00:00
|
|
|
'engine' => 'Console',
|
2012-07-06 20:30:13 +00:00
|
|
|
'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
|
|
|
|
'stream' => $this->stderr,
|
|
|
|
));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|