updating console scripts, added script path searching, for /vendors and /app/vendors, class names must be like "AclScript" in /scripts/acl.php, all scripts now extend CakeScript

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4998 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-05-03 22:40:22 +00:00
parent 73f22d074b
commit d68228b704
4 changed files with 88 additions and 465 deletions

View file

@ -26,18 +26,12 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
uses (
'object', 'configure', 'set', 'session', 'security',
'inflector', 'model'.DS.'connection_manager',
'model'.DS.'datasources'.DS.'dbo_source', 'model'.DS.'model'
);
require_once(CAKE.'app_model.php');
uses ('controller'.DS.'components'.DS.'acl', 'model'.DS.'db_acl');
/**
* @package cake
* @subpackage cake.cake.scripts
*/
class Acl extends ConsoleScript {
class AclScript extends CakeScript {
/**
* Enter description here...
*
@ -63,11 +57,6 @@ class Acl extends ConsoleScript {
* @param unknown_type $args
*/
function main () {
/*if (function_exists('ini_set')) {
ini_set('display_errors', '1');
ini_set('error_reporting', '7');
ini_set('max_execution_time',0);
}*/
$this->dataSource = 'default';
if (isset($this->params['datasource'])) {
@ -88,9 +77,10 @@ class Acl extends ConsoleScript {
exit();
}
pr($this);
die();
//pr($this);
//die();
$command = null;
if(!in_array($command, array('help'))) {
if(!file_exists(CONFIGS.'database.php')) {
$this->out('');
@ -506,7 +496,7 @@ class Acl extends ConsoleScript {
$driver = '';
while ($driver == '') {
$driver = $this->getInput('What database driver would you like to use?', array('mysql','mysqli','mssql','sqlite','postgres', 'odbc'), 'mysql');
$driver = $this->in('What database driver would you like to use?', array('mysql','mysqli','mssql','sqlite','postgres', 'odbc'), 'mysql');
if ($driver == '') {
$this->out('The database driver supplied was empty. Please supply a database driver.');
}
@ -539,7 +529,7 @@ class Acl extends ConsoleScript {
$host = '';
while ($host == '') {
$host = $this->getInput('What is the hostname for the database server?', null, 'localhost');
$host = $this->in('What is the hostname for the database server?', null, 'localhost');
if ($host == '') {
$this->out('The host name you supplied was empty. Please supply a hostname.');
}
@ -547,7 +537,7 @@ class Acl extends ConsoleScript {
$login = '';
while ($login == '') {
$login = $this->getInput('What is the database username?', null, 'root');
$login = $this->in('What is the database username?', null, 'root');
if ($login == '') {
$this->out('The database username you supplied was empty. Please try again.');
@ -557,9 +547,9 @@ class Acl extends ConsoleScript {
$blankPassword = false;
while ($password == '' && $blankPassword == false) {
$password = $this->getInput('What is the database password?');
$password = $this->in('What is the database password?');
if ($password == '') {
$blank = $this->getInput('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
$blank = $this->in('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
if($blank == 'y')
{
$blankPassword = true;
@ -569,7 +559,7 @@ class Acl extends ConsoleScript {
$database = '';
while ($database == '') {
$database = $this->getInput('What is the name of the database you will be using?', null, 'cake');
$database = $this->in('What is the name of the database you will be using?', null, 'cake');
if ($database == '') {
$this->out('The database name you supplied was empty. Please try again.');
@ -579,7 +569,7 @@ class Acl extends ConsoleScript {
$prefix = '';
while ($prefix == '') {
$prefix = $this->getInput('Enter a table prefix?', null, 'n');
$prefix = $this->in('Enter a table prefix?', null, 'n');
}
if(low($prefix) == 'n') {
$prefix = '';
@ -596,7 +586,7 @@ class Acl extends ConsoleScript {
$this->out("Database: $database");
$this->out("Table prefix: $prefix");
$this->hr(true);
$looksGood = $this->getInput('Look okay?', array('y', 'n'), 'y');
$looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
$this->bakeDbConfig($driver, $connect, $host, $login, $password, $database, $prefix);
@ -629,36 +619,6 @@ class Acl extends ConsoleScript {
$filename = CONFIGS.'database.php';
$this->__createFile($filename, $out);
}
/**
* 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.
*/
function getInput($prompt, $options = null, $default = null) {
if (!is_array($options)) {
$print_options = '';
} else {
$print_options = '(' . implode('/', $options) . ')';
}
if($default == null) {
$this->out('');
$this->out($prompt . " $print_options \n" . '> ', false);
} else {
$this->out('');
$this->out($prompt . " $print_options \n" . "[$default] > ", false);
}
$result = trim(fgets($this->stdin));
if($default != null && empty($result)) {
return $default;
} else {
return $result;
}
}
}
?>

View file

@ -32,7 +32,7 @@
* @package cake
* @subpackage cake.cake.scripts
*/
class ConsoleScript extends Object {
class CakeScript extends Object {
/**
* ConsoleDispatcher object

View file

@ -30,7 +30,7 @@
* @package cake
* @subpackage cake.cake.scritps
*/
class Console extends ConsoleScript {
class ConsoleScipt extends CakeScript {
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

@ -75,34 +75,42 @@ class ConsoleDispatcher {
* @var string
*/
var $scriptClass = null;
/**
* The path location of scripts.
*
* @var array
*/
var $scriptPaths = array();
function ConsoleDispatcher($args = array()) {
$this->__construct($args);
$this->__construct($args);
}
function initConstants() {
function __initConstants() {
if (function_exists('ini_set')) {
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
ini_set('html_errors', false);
ini_set('implicit_flush', true);
ini_set('max_execution_time', 60 * 5);
}
define('PHP5', (phpversion() >= 5));
define('DS', DIRECTORY_SEPARATOR);
define('CAKE_CORE_INCLUDE_PATH', dirname(dirname(dirname(__FILE__))));
define('ROOT', dirname($this->params['working']));
define('APP_DIR', basename($this->params['working']));
define('APP_PATH', ROOT . DS . APP_DIR . DS);
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
define('THE_CORE', CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS);
define('DISABLE_DEFAULT_ERROR_HANDLING', true);
define('DISABLE_DEFAULT_ERROR_HANDLING', false);
}
function initEnvironment() {
if (function_exists('ini_set')) {
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
ini_set('max_execution_time', 60 * 5);
}
function __initEnvironment() {
$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.');
$this->stdout('Please make sure that ' . DIRECTORY_SEPARATOR . 'cake' . DIRECTORY_SEPARATOR . 'scripts is in your system path,');
$this->stdout('and check the manual for the correct usage of this command.');
@ -110,30 +118,33 @@ class ConsoleDispatcher {
exit();
}
if (__FILE__ != $this->args[0]) {
$this->stdout('Warning: this file may have been loaded incorrectly, which could lead to unexpected results...');
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...');
if ($this->getInput('Continue anyway?', array('y', 'n'), 'y') == 'n') {
exit();
}
}
$this->shiftArgs();
}
function __construct($args = array()) {
$this->parseParams($args);
$this->initEnvironment();
$this->initConstants();
if (!$this->bootstrap()) {
$this->stdout("\nCakePHP Console: ");
$this->stdout("\nUnable to load Cake core:");
$this->stdout("\tMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH);
exit();
}
$this->stdout("\nWelcome to CakePHP v" . Configure::version() . " Console");
if (!isset($this->args[0]) || $this->args[0] != 'help') {
$this->stdout("Type 'cake help' for help\n");
}
$this->scriptPaths = array(
VENDORS . 'scritps' . DS,
APP . 'vendors' . DS . 'scripts' . DS,
SCRIPTS
);
}
function __construct($args = array()) {
$this->parseParams($args);
$this->__initConstants();
$this->__initEnvironment();
$this->dispatch();
die("\n");
}
@ -143,30 +154,48 @@ class ConsoleDispatcher {
* @return void
*/
function dispatch() {
$this->stdout("\nWelcome to CakePHP v" . Configure::version() . " Console");
if (!isset($this->args[0]) || $this->args[0] != 'help') {
$this->stdout("Type 'cake help' for help\n");
}
if (isset($this->args[0])) {
// Load requested script
$script = $this->args[0];
$this->script = SCRIPTS . $script . ".php";
$this->scriptClass = Inflector::camelize($script);
$this->shiftArgs();
if (method_exists($this, $script)) {
$this->{$script}();
} elseif (!file_exists($this->script)) {
$this->stdout('Unable to dispatch to requested script: ', false);
$this->stdout("'{$script}.php' does not exist in " . SCRIPTS);
exit();
} else {
require SCRIPTS . 'console_script.php';
require $this->script;
$Script = $this->scriptClass;
$cli = new $Script($this);
$cli->main();
$loaded = false;
foreach($this->scriptPaths as $path) {
$this->script = $path . $script . ".php";
$this->scriptClass = Inflector::camelize($script).'Script';
if (file_exists($this->script)) {
$loaded = true;
break;
}
}
if (!$loaded) {
$this->stdout('Unable to dispatch requested script: ', false);
$this->stdout("'{$script}.php' does not exist in: \n" . implode("\nor ", $this->scriptPaths));
exit();
} else {
require SCRIPTS . 'cake_script.php';
require $this->script;
if(class_exists($this->scriptClass)) {
$cli = new $this->scriptClass($this);
$cli->main();
} else {
$this->stderr('Class '.$this->scriptClass.' could not be loaded');
}
}
}
} else {
$this->stdout('Available commands:');
foreach (listClasses(THE_CORE . 'scripts') as $script) {
if ($script != 'dispatch.php' && $script != 'console_script.php') {
foreach (listClasses(CAKE . 'scripts') as $script) {
if ($script != 'dispatch.php' && $script != 'cake_script.php') {
$this->stdout("\t - " . r('.php', '', $script));
}
}
@ -181,10 +210,9 @@ class ConsoleDispatcher {
*/
function bootstrap() {
$includes = array(
THE_CORE . 'basics.php',
THE_CORE . 'config'.DS.'paths.php',
THE_CORE . 'dispatcher.php',
THE_CORE . 'scripts'.DS.'templates'.DS.'skel'.DS.'config'.DS.'core.php'
CORE_PATH . 'cake' . DS . 'basics.php',
CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php',
CORE_PATH . 'cake' . DS . 'scripts'.DS.'templates'.DS.'skel'.DS.'config'.DS.'core.php'
);
foreach ($includes as $inc) {
@ -194,9 +222,10 @@ class ConsoleDispatcher {
}
}
$libraries = array('session', 'configure', 'inflector', 'model'.DS.'connection_manager', 'debugger', 'security', 'controller' . DS . 'controller');
$libraries = array('object', 'session', 'configure', 'inflector', 'model'.DS.'connection_manager',
'debugger', 'security', 'controller' . DS . 'controller');
foreach ($libraries as $inc) {
if (!file_exists(LIBS.$inc.'.php')) {
if (!file_exists(LIBS . $inc . '.php')) {
$this->stdout("Failed to load Cake core class " . ucwords($inc));
$this->stdout("(" . LIBS.$inc.".php)");
return false;
@ -223,10 +252,8 @@ class ConsoleDispatcher {
}
if($default == null) {
$this->stdout('');
$this->stdout($prompt . " $print_options \n" . '> ', false);
} else {
$this->stdout('');
$this->stdout($prompt . " $print_options \n" . "[$default] > ", false);
}
$result = trim(fgets($this->stdin));
@ -256,7 +283,7 @@ class ConsoleDispatcher {
* @param string $string Error text to output.
*/
function stderr($string) {
fwrite($this->stderr, $string, true);
fwrite($this->stderr, 'Error: '. $string);
}
/**
* Parses command line options
@ -296,371 +323,7 @@ class ConsoleDispatcher {
$this->stdout("\nConsole Help:");
$this->stdout('-------------');
print_r($this->args);
print_r($this->params);
echo implode('\n', $this->params);
}
}
$app = 'app';
$root = dirname(dirname(dirname(__FILE__)));
$core = null;
$here = $argv[0];
$help = null;
$project = null;
switch ($argv[$i]) {
case '-a':
case '-app':
$app = $argv[$i + 1];
break;
case '-c':
case '-core':
$core = $argv[$i + 1];
break;
case '-r':
case '-root':
$root = $argv[$i + 1];
break;
case '-p':
case '-project':
$project = true;
$projectPath = $argv[$i + 1];
$app = $argv[$i + 1];
break;
}
if(!is_dir($app)) {
$project = true;
$projectPath = $app;
}
if($project) {
$app = $projectPath;
}
$shortPath = str_replace($root, '', $app);
$shortPath = str_replace('..'.DS, '', $shortPath);
$shortPath = str_replace(DS.DS, DS, $shortPath);
$pathArray = explode(DS, $shortPath);
if(end($pathArray) != '') {
$appDir = array_pop($pathArray);
} else {
array_pop($pathArray);
$appDir = array_pop($pathArray);
}
$rootDir = implode(DS, $pathArray);
$rootDir = str_replace(DS.DS, DS, $rootDir);
if(!$rootDir) {
$rootDir = $root;
$projectPath = $root.DS.$appDir;
}
define ('ROOT', $rootDir);
define ('APP_DIR', $appDir);
define ('DEBUG', 1);
/**
* Base class for command-line utilities for automating programmer chores.
*
* @package cake
* @subpackage cake.cake.scripts
*/
class CakeConsoleScript extends Object {
/**
* CakeConsoleDispatcher object
*
* @var object An instance of the ConsoleDispatcher object that loaded this script
*/
var $dispatch = null;
/**
* If true, the script will ask for permission to perform actions.
*
* @var boolean
*/
var $interactive = true;
/**
* Holds the DATABASE_CONFIG object for the app. Null if database.php could not be found,
* or the app does not exist.
*
* @var object
*/
var $dbConfig = null;
/**
* Contains command switches parsed from the command line.
*
* @var array
*/
var $params = array();
/**
* Contains arguments parsed from the command line.
*
* @var array
*/
var $args = array();
/**
* Initializes this CakeConsoleScript instance.
*
*/
function __construct(&$dispatch) {
$this->dispatch =& $dispatch;
$this->params = $this->dispatch->params;
$this->args = $this->dispatch->args;
if(file_exists(CONFIGS.'database.php')) {
require_once (CONFIGS . 'database.php');
$this->dbConfig = new DATABASE_CONFIG();
}
}
/**
* Main-loop method.
*
*/
function main() {
$this->out('');
$this->out('');
$this->out('Baking...');
$this->hr();
$this->out('Name: '. APP_DIR);
$this->out('Path: '. ROOT.DS.APP_DIR);
$this->hr();
if(empty($this->dbConfig)) {
$this->out('');
$this->out('Your database configuration was not found. Take a moment to create one:');
}
require_once (CONFIGS . 'database.php');
$this->stdout('[M]odel');
$this->stdout('[C]ontroller');
$this->stdout('[V]iew');
$invalidSelection = true;
while ($invalidSelection) {
$classToBake = strtoupper($this->in('What would you like to Bake?', array('M', 'V', 'C')));
switch($classToBake) {
case 'M':
$invalidSelection = false;
$this->doModel();
break;
case 'V':
$invalidSelection = false;
$this->doView();
break;
case 'C':
$invalidSelection = false;
$this->doController();
break;
default:
$this->stdout('You have made an invalid selection. Please choose a type of class to Bake by entering M, V, or C.');
}
}
}
/**
* 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.
*/
function in($prompt, $options = null, $default = null) {
return $this->dispatch->getInput($prompt, $options, $default);
}
/**
* Outputs to the stdout filehandle.
*
* @param string $string String to output.
* @param boolean $newline If true, the outputs gets an added newline.
*/
function out($string, $newline = true) {
return $this->dispatch->stdout($string, $newline);
}
/**
* Outputs to the stderr filehandle.
*
* @param string $string Error text to output.
*/
function err($string) {
return $this->dispatch->stderr($string);
}
/**
* Outputs a series of minus characters to the standard output, acts as a visual separator.
*
*/
function hr() {
$this->out('---------------------------------------------------------------');
}
/**
* Creates a file at given path.
*
* @param string $path Where to put the file.
* @param string $contents Content to put in the file.
* @return Success
*/
function createFile ($path, $contents) {
$path = str_replace(DS . DS, DS, $path);
echo "\nCreating file $path\n";
if (is_file($path) && $this->interactive === true) {
fwrite($this->stdout, __("File exists, overwrite?", true). " {$path} (y/n/q):");
$key = trim(fgets($this->stdin));
if ($key == 'q') {
fwrite($this->stdout, __("Quitting.", true) ."\n");
exit;
} elseif ($key == 'a') {
$this->dont_ask = true;
} elseif ($key == 'y') {
} else {
fwrite($this->stdout, __("Skip", true) ." {$path}\n");
return false;
}
}
if ($f = fopen($path, 'w')) {
fwrite($f, $contents);
fclose($f);
fwrite($this->stdout, __("Wrote", true) ."{$path}\n");
return true;
} else {
fwrite($this->stderr, __("Error! Could not write to", true)." {$path}.\n");
return false;
}
}
/**
* Outputs usage text on the standard output.
*
*/
function help() {
$this->stdout('CakePHP Console:');
$this->hr();
$this->stdout('The Bake script generates controllers, views and models for your application.');
$this->stdout('If run with no command line arguments, Bake guides the user through the class');
$this->stdout('creation process. You can customize the generation process by telling Bake');
$this->stdout('where different parts of your application are using command line arguments.');
$this->stdout('');
$this->hr('');
$this->stdout('usage: php bake.php [command] [path...]');
$this->stdout('');
$this->stdout('commands:');
$this->stdout(' -app [path...] Absolute path to Cake\'s app Folder.');
$this->stdout(' -core [path...] Absolute path to Cake\'s cake Folder.');
$this->stdout(' -help Shows this help message.');
$this->stdout(' -project [path...] Generates a new app folder in the path supplied.');
$this->stdout(' -root [path...] Absolute path to Cake\'s \app\webroot Folder.');
$this->stdout('');
}
/**
* Returns true if given path is a directory.
*
* @param string $path
* @return True if given path is a directory.
*/
function isDir($path) {
if(is_dir($path)) {
return true;
} else {
return false;
}
}
/**
* Recursive directory copy.
*
* @param string $fromDir
* @param string $toDir
* @param octal $chmod
* @param boolean $verbose
* @return Success.
*/
function copyDir($fromDir, $toDir, $chmod = 0755, $verbose = false) {
$errors = array();
$messages = array();
if (!is_dir($toDir)) {
uses('folder');
$folder = new Folder();
$folder->mkdirr($toDir, 0755);
}
if (!is_writable($toDir)) {
$errors[] = 'target '.$toDir.' is not writable';
}
if (!is_dir($fromDir)) {
$errors[] = 'source '.$fromDir.' is not a directory';
}
if (!empty($errors)) {
if ($verbose) {
foreach($errors as $err) {
$this->stdout('Error: '.$err);
}
}
return false;
}
$exceptions = array('.','..','.svn');
$handle = opendir($fromDir);
while (false !== ($item = readdir($handle))) {
if (!in_array($item,$exceptions)) {
$from = str_replace('//','/',$fromDir.'/'.$item);
$to = str_replace('//','/',$toDir.'/'.$item);
if (is_file($from)) {
if (@copy($from, $to)) {
chmod($to, $chmod);
touch($to, filemtime($from));
$messages[] = 'File copied from '.$from.' to '.$to;
} else {
$errors[] = 'cannot copy file from '.$from.' to '.$to;
}
}
if (is_dir($from)) {
if (@mkdir($to)) {
chmod($to, $chmod);
$messages[] = 'Directory created: '.$to;
} else {
$errors[] = 'cannot create directory '.$to;
}
$this->copyDir($from,$to,$chmod,$verbose);
}
}
}
closedir($handle);
if ($verbose) {
foreach($errors as $err) {
$this->stdout('Error: '.$err);
}
foreach($messages as $msg) {
$this->stdout($msg);
}
}
return true;
}
function __addAdminRoute($name){
$file = file_get_contents(CONFIGS.'core.php');
if (preg_match('%([/\\t\\x20]*define\\(\'CAKE_ADMIN\',[\\t\\x20\'a-z]*\\);)%', $file, $match)) {
$result = str_replace($match[0], 'define(\'CAKE_ADMIN\', \''.$name.'\');', $file);
if(file_put_contents(CONFIGS.'core.php', $result)){
return true;
} else {
return false;
}
} else {
return false;
}
}
}
?>