updating shells for new conventions, better handling of paths, more help

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5058 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-05-12 21:18:07 +00:00
parent 2e8e37860d
commit 846c665497
7 changed files with 92 additions and 77 deletions

View file

@ -28,7 +28,7 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class ConsoleDispatcher {
class ShellDispatcher {
/**
* Standard input stream.
*
@ -64,20 +64,20 @@ class ConsoleDispatcher {
*
* @var string
*/
var $script = null;
var $shell = null;
/**
* The class name of the script that was invoked.
*
* @var string
*/
var $scriptClass = null;
var $shellClass = null;
/**
* The command called if public methods are available.
*
* @var string
*/
var $scriptCommand = null;
var $shellCommand = null;
/**
@ -85,30 +85,30 @@ class ConsoleDispatcher {
*
* @var array
*/
var $scriptPaths = array();
var $shellPaths = array();
/**
* The path to the current script location.
*
* @var string
*/
var $scriptPath = null;
var $shellPath = null;
/**
* The name of the script in lowercase underscore.
*
* @var string
*/
var $scriptName = null;
var $shellName = null;
/**
* Constructs this ConsoleDispatcher instance.
* Constructs this ShellDispatcher instance.
*
* @param array $args the argv.
* @return void
*/
function ConsoleDispatcher($args = array()) {
function ShellDispatcher($args = array()) {
$this->__construct($args);
}
@ -148,7 +148,7 @@ class ConsoleDispatcher {
$this->stdin = fopen('php://stdin', 'r');
$this->stdout = fopen('php://stdout', 'w');
$this->stderr = fopen('php://stderr', 'w');
if (!isset($this->args[0]) || !isset($this->params['working'])) {
$this->stdout("\nCakePHP Console: ");
$this->stdout('This file has been loaded incorrectly and cannot continue.');
@ -157,7 +157,7 @@ class ConsoleDispatcher {
$this->stdout('(http://manual.cakephp.org/)');
exit();
}
if (basename(__FILE__) != basename($this->args[0])) {
$this->stdout("\nCakePHP Console: ");
$this->stdout('Warning: the dispatcher may have been loaded incorrectly, which could lead to unexpected results...');
@ -175,9 +175,9 @@ class ConsoleDispatcher {
$this->shiftArgs();
$this->scriptPaths = array(
VENDORS . 'scripts' . DS,
APP . 'vendors' . DS . 'scripts' . DS,
$this->shellPaths = array(
APP . 'vendors' . DS . 'shells' . DS,
VENDORS . 'shells' . DS,
CONSOLE_LIBS
);
}
@ -238,24 +238,24 @@ class ConsoleDispatcher {
}
$protectedCommands = array('initialize', 'main','in','out','err','hr',
'createFile', 'isDir','copyDir','Object','toString',
'requestAction','log','cakeError', 'ConsoleDispatcher',
'requestAction','log','cakeError', 'ShellDispatcher',
'__initConstants','__initEnvironment','__construct',
'dispatch','__bootstrap','getInput','stdout','stderr','parseParams','shiftArgs'
);
if (isset($this->args[0])) {
// Load requested script
$this->script = $this->args[0];
// Load requested shell
$this->shell = $this->args[0];
$this->shiftArgs();
$this->scriptName = Inflector::camelize($this->script);
$this->scriptClass = $this->scriptName . 'Script';
$this->shellName = Inflector::camelize($this->shell);
$this->shellClass = $this->shellName . 'Script';
if (method_exists($this, $this->script) && !in_array($this->script, $protectedCommands)) {
$this->{$this->script}();
if (method_exists($this, $this->shell) && !in_array($this->shell, $protectedCommands)) {
$this->{$this->shell}();
} else {
$loaded = false;
foreach($this->scriptPaths as $path) {
$this->scriptPath = $path . $this->script . ".php";
if (file_exists($this->scriptPath)) {
foreach($this->shellPaths as $path) {
$this->shellPath = $path . $this->shell . ".php";
if (file_exists($this->shellPath)) {
$loaded = true;
break;
}
@ -263,19 +263,19 @@ class ConsoleDispatcher {
if (!$loaded) {
$this->stdout('Unable to dispatch requested script: ', false);
$this->stdout("'".$this->script.".php' does not exist in: \n" . implode("\nor ", $this->scriptPaths));
$this->stdout("'".$this->shell.".php' does not exist in: \n" . implode("\nor ", $this->shellPaths));
exit();
} else {
require CONSOLE_LIBS . 'cake_script.php';
require $this->scriptPath;
if(class_exists($this->scriptClass)) {
$script = new $this->scriptClass($this);
require CONSOLE_LIBS . 'shell.php';
require $this->shellPath;
if(class_exists($this->shellClass)) {
$shell = new $this->shellClass($this);
$command = null;
if(isset($this->args[0])) {
$command = $this->args[0];
}
$classMethods = get_class_methods($script);
$classMethods = get_class_methods($shell);
$privateMethod = $missingCommand = false;
if((in_array($command, $classMethods) || in_array(strtolower($command), $classMethods)) && strpos($command, '_', 0) === 0) {
@ -290,25 +290,25 @@ class ConsoleDispatcher {
$missingCommand = true;
}
if($command == 'help') {
if(method_exists($script, 'help')) {
$script->initialize();
$script->help();
if(method_exists($shell, 'help')) {
$shell->initialize();
$shell->help();
} else {
$this->help();
}
} else if($missingCommand && method_exists($script, 'main')) {
$script->initialize();
$script->main();
} else if(!$privateMethod && method_exists($script, $command)) {
$script->command = $command;
} else if($missingCommand && method_exists($shell, 'main')) {
$shell->initialize();
$shell->main();
} else if(!$privateMethod && method_exists($shell, $command)) {
$shell->command = $command;
$this->shiftArgs();
$script->initialize();
$script->{$command}();
$shell->initialize();
$shell->{$command}();
} else {
$this->stderr("Unknown {$this->scriptName} command '$command'.\nFor usage, try 'cake {$this->script} help'.\n\n");
$this->stderr("Unknown {$this->shellName} command '$command'.\nFor usage, try 'cake {$this->shell} help'.\n\n");
}
} else {
$this->stderr('Class '.$this->scriptClass.' could not be loaded');
$this->stderr('Class '.$this->shellClass.' could not be loaded');
}
}
}
@ -381,7 +381,7 @@ class ConsoleDispatcher {
}
}
$app = 'app';
$app = basename($this->params['working']);
if(isset($this->params['app'])) {
if($this->params['app']{0} == '/') {
$this->params['working'] = $this->params['app'];
@ -389,7 +389,8 @@ class ConsoleDispatcher {
$app = $this->params['app'];
}
}
if(empty($this->params['working'])) {
if(in_array($app, array('cake', 'console'))){
$app = 'app';
$this->params['working'] = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . $app;
}
}
@ -412,26 +413,34 @@ class ConsoleDispatcher {
* @return void
*/
function help() {
print_r($this->args);
$this->stdout("\nPaths:");
$this->stdout(" -working: " . $this->params['working']);
$this->stdout(" -app: ". APP_DIR);
$this->stdout(" -root: " . ROOT);
$this->stdout(" -cake: " . CORE_PATH);
$this->stdout("\nAvailable Scripts:");
foreach($this->scriptPaths as $path) {
$this->stdout("\n " . $path . ":");
foreach (listClasses($path) as $script) {
if ($script != 'cake_script.php') {
$this->stdout("\t - " . r('.php', '', $script));
foreach($this->shellPaths as $path) {
if(is_dir($path)) {
$shells = listClasses($path);
$path = r(CORE_PATH, '', $path);
$this->stdout("\n " . $path . ":");
if(empty($shells)) {
$this->stdout("\t - none");
} else {
foreach ($shells as $shell) {
if ($shell != 'shell.php') {
$this->stdout("\t - " . r('.php', '', $shell));
}
}
}
}
}
}
$this->stdout("\nTo run a command, type 'cake script_name [args]'");
$this->stdout("To get help on a specific command, type 'cake script_name help'");
}
}
if (!defined('DISABLE_AUTO_DISPATCH')) {
$dispatcher = new ConsoleDispatcher($argv);
$dispatcher = new ShellDispatcher($argv);
}
?>

View file

@ -31,7 +31,7 @@ uses ('controller'.DS.'components'.DS.'acl', 'model'.DS.'db_acl');
* @package cake
* @subpackage cake.cake.scripts
*/
class AclScript extends CakeScript {
class AclShell extends Shell {
/**
* Enter description here...
*
@ -51,7 +51,7 @@ class AclScript extends CakeScript {
*/
var $dataSource = 'default';
/**
* override intialize of the CakeScript
* override intialize of the Shell
*
*/
function initialize () {

View file

@ -34,7 +34,7 @@
* @package cake
* @subpackage cake.cake.scripts
*/
class BakeScript extends CakeScript {
class BakeShell extends Shell {
/**
* Associated controller name.
*

View file

@ -26,7 +26,7 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class Bake2Script extends CakeScript {
class Bake2Shell extends Shell {
var $task = null;
@ -52,9 +52,18 @@ class Bake2Script extends CakeScript {
}
function _loadTask($taskName = null) {
if(!$taskName) {
$taskName = $this->task;
}
if(!$taskName) {
return null;
}
$loaded = false;
foreach($this->Dispatch->scriptPaths as $path) {
$this->taskPath = $path . 'tasks' . DS . $taskName.'_task.php';
foreach($this->Dispatch->shellPaths as $path) {
$this->taskPath = $path . 'tasks' . DS . $taskName.'.php';
if (file_exists($this->taskPath)) {
$loaded = true;
break;
@ -62,7 +71,7 @@ class Bake2Script extends CakeScript {
}
if ($loaded) {
require CONSOLE_LIBS . 'tasks' . DS . 'bake_task.php';
require CONSOLE_LIBS . 'tasks' . DS . 'bake.php';
require $this->taskPath;
$this->taskClass = $taskName.'Task';
@ -75,12 +84,9 @@ class Bake2Script extends CakeScript {
function _readConfigFile($fileName) {
$fileLineArray = file($fileName);
foreach($fileLineArray as $fileLine) {
$dataLine = trim($fileLine);
$delimiter = strpos($dataLine, '=');
if ($delimiter > 0) {
$key = strtolower(trim(substr($dataLine, 0, $delimiter)));
$value = trim(substr($dataLine, $delimiter + 1));

View file

@ -30,7 +30,7 @@
* @package cake
* @subpackage cake.cake.scritps
*/
class ConsoleScript extends CakeScript {
class ConsoleShell extends Shell {
var $ignoreList = array(T_WHITESPACE, T_OPEN_TAG, T_CLOSE_TAG);
var $returnList = array(T_FOREACH, T_DO, T_WHILE, T_FOR, T_IF, T_RETURN,
T_CLASS, T_FUNCTION, T_INTERFACE, T_PRINT, T_ECHO,

View file

@ -32,12 +32,12 @@
* @package cake
* @subpackage cake.cake.scripts
*/
class CakeScript extends Object {
class Shell extends Object {
/**
* ConsoleDispatcher object
* ShellDispatcher object
*
* @var object An instance of the ConsoleDispatcher object that loaded this script
* @var object An instance of the ShellDispatcher object that loaded this script
*/
var $Dispatch = null;
/**
@ -66,18 +66,18 @@ class CakeScript extends Object {
*/
var $args = array();
/**
* Constructs this CakeScript instance.
* Constructs this Shell instance.
*
*/
function __construct(&$dispatch) {
$this->Dispatch = & $dispatch;
$this->params = & $this->Dispatch->params;
$this->args = & $this->Dispatch->args;
$this->name = & $this->Dispatch->scriptName;
$this->command = & $this->Dispatch->scriptCommand;
$this->name = & $this->Dispatch->shellName;
$this->command = & $this->Dispatch->shellCommand;
}
/**
* Initializes the CakeScript
* Initializes the Shell
* can be overriden in subclasses
*
* @return null
@ -193,7 +193,7 @@ class CakeScript extends Object {
$command = $this->command;
}
if (count($this->args) < $expectedNum) {
$this->displayError('Wrong number of parameters: '.count($this->args), 'Please type \'cake '.$this->Dispatch->script.' help\' for help on usage of the '.$command.' command.');
$this->displayError('Wrong number of parameters: '.count($this->args), 'Please type \'cake '.$this->Dispatch->shell.' help\' for help on usage of the '.$command.' command.');
}
}
/**

View file

@ -26,17 +26,17 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class BakeTask extends CakeScript {
class BakeTask extends Shell {
/**
* Constructs this BakeTask instance.
*
*/
function __construct(&$script) {
$this->Dispatch = & $script->Dispatch;
$this->task = & $script->task;
$this->params = & $script->Dispatch->params;
$this->args = & $script->Dispatch->args;
function __construct(&$shell) {
$this->Dispatch = & $shell->Dispatch;
$this->task = & $shell->task;
$this->params = & $shell->Dispatch->params;
$this->args = & $shell->Dispatch->args;
}
/**