Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 645 lines (586 sloc) 15.348 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
<?php
/* SVN FILE: $Id$ */

/**
* Base class for Shells
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.console.libs
* @since CakePHP(tm) v 1.2.0.5012
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/

/**
* Base class for command-line utilities for automating programmer chores.
*
* @package cake
* @subpackage cake.cake.console.libs
*/
class Shell extends Object {

/**
* An instance of the ShellDispatcher object that loaded this script
*
* @var ShellDispatcher
* @access public
*/
var $Dispatch = null;

/**
* If true, the script will ask for permission to perform actions.
*
* @var boolean
* @access public
*/
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 DATABASE_CONFIG
* @access public
*/
var $DbConfig = null;

/**
* Contains command switches parsed from the command line.
*
* @var array
* @access public
*/
var $params = array();

/**
* Contains arguments parsed from the command line.
*
* @var array
* @access public
*/
var $args = array();

/**
* The file name of the shell that was invoked.
*
* @var string
* @access public
*/
var $shell = null;

/**
* The class name of the shell that was invoked.
*
* @var string
* @access public
*/
var $className = null;

/**
* The command called if public methods are available.
*
* @var string
* @access public
*/
var $command = null;

/**
* The name of the shell in camelized.
*
* @var string
* @access public
*/
var $name = null;

/**
* An alias for the shell
*
* @var string
* @access public
*/
var $alias = null;

/**
* Contains tasks to load and instantiate
*
* @var array
* @access public
*/
var $tasks = array();

/**
* Contains the loaded tasks
*
* @var array
* @access public
*/
var $taskNames = array();

/**
* Contains models to load and instantiate
*
* @var array
* @access public
*/
var $uses = array();

/**
* Constructs this Shell instance.
*
*/
function __construct(&$dispatch) {
$vars = array('params', 'args', 'shell', 'shellCommand' => 'command');
foreach ($vars as $key => $var) {
if (is_string($key)) {
$this->{$var} =& $dispatch->{$key};
} else {
$this->{$var} =& $dispatch->{$var};
}
}

if ($this->name == null) {
$this->name = get_class($this);
}

if ($this->alias == null) {
$this->alias = $this->name;
}

ClassRegistry::addObject($this->name, $this);
ClassRegistry::map($this->name, $this->alias);

if (!PHP5 && isset($this->args[0])) {
if (strpos($this->name, low(Inflector::camelize($this->args[0]))) !== false) {
$dispatch->shiftArgs();
}
if (low($this->command) == low(Inflector::variable($this->args[0])) && method_exists($this, $this->command)) {
$dispatch->shiftArgs();
}
}

$this->Dispatch =& $dispatch;
}

/**
* Initializes the Shell
* acts as constructor for subclasses
* allows configuration of tasks prior to shell execution
*
* @access public
*/
function initialize() {
$this->_loadModels();
}

/**
* Starts up the the Shell
* allows for checking and configuring prior to command or main execution
* can be overriden in subclasses
*
* @access public
*/
function startup() {
$this->_welcome();
}

/**
* Displays a header for the shell
*
* @access protected
*/
function _welcome() {
$this->Dispatcher->clear();
$this->out("\nWelcome to CakePHP v" . Configure::version() . " Console");
$this->out("---------------------------------------------------------------");
$this->out('App : '. $this->params['app']);
$this->out('Path: '. $this->params['working']);
$this->hr();
}

/**
* Loads database file and constructs DATABASE_CONFIG class
* makes $this->DbConfig available to subclasses
*
* @return bool
* @access protected
*/
function _loadDbConfig() {
if (config('database') && class_exists('DATABASE_CONFIG')) {
$this->DbConfig =& new DATABASE_CONFIG();
return true;
}
$this->err('Database config could not be loaded');
$this->out('Run \'bake\' to create the database configuration');
return false;
}

/**
* if var $uses = true
* Loads AppModel file and constructs AppModel class
* makes $this->AppModel available to subclasses
* if var $uses is an array of models will load those models
*
* @return bool
* @access protected
*/
function _loadModels() {
if ($this->uses === null || $this->uses === false) {
return;
}

if ($this->uses === true && App::import('Model', 'AppModel')) {
$this->AppModel =& new AppModel(false, false, false);
return true;
}

if ($this->uses !== true && !empty($this->uses)) {
$uses = is_array($this->uses) ? $this->uses : array($this->uses);

$modelClassName = $uses[0];
if (strpos($uses[0], '.') !== false) {
list($plugin, $modelClassName) = explode('.', $uses[0]);
}
$this->modelClass = $modelClassName;

foreach ($uses as $modelClass) {
$plugin = null;
if (strpos($modelClass, '.') !== false) {
list($plugin, $modelClass) = explode('.', $modelClass);
$plugin = $plugin . '.';
}
if (PHP5) {
$this->{$modelClass} = ClassRegistry::init($plugin . $modelClass);
} else {
$this->{$modelClass} =& ClassRegistry::init($plugin . $modelClass);
}
}
return true;
}
return false;
}

/**
* Loads tasks defined in var $tasks
*
* @return bool
* @access public
*/
function loadTasks() {
if ($this->tasks === null || $this->tasks === false || $this->tasks === true || empty($this->tasks)) {
return true;
}

$tasks = $this->tasks;
if (!is_array($tasks)) {
$tasks = array($tasks);
}

foreach ($tasks as $taskName) {
$task = Inflector::underscore($taskName);
$taskClass = Inflector::camelize($taskName . 'Task');

if (!class_exists($taskClass)) {
foreach ($this->Dispatch->shellPaths as $path) {
$taskPath = $path . 'tasks' . DS . $task.'.php';
if (file_exists($taskPath)) {
require_once $taskPath;
break;
}
}
}
if (ClassRegistry::isKeySet($taskClass)) {
$this->taskNames[] = $taskName;
if (!PHP5) {
$this->{$taskName} =& ClassRegistry::getObject($taskClass);
} else {
$this->{$taskName} = ClassRegistry::getObject($taskClass);
}
} else {
$this->taskNames[] = $taskName;
if (!PHP5) {
$this->{$taskName} =& new $taskClass($this->Dispatch);
} else {
$this->{$taskName} = new $taskClass($this->Dispatch);
}
}

if (!isset($this->{$taskName})) {
$this->err("Task '".$taskName."' could not be loaded");
$this->_stop();
}
}

return true;
}

/**
* 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.
* @access public
*/
function in($prompt, $options = null, $default = null) {
if (!$this->interactive) {
return $default;
}
$in = $this->Dispatch->getInput($prompt, $options, $default);

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)) {
while ($in == '' || ($in && (!in_array(low($in), $options) && !in_array(up($in), $options)) && !in_array($in, $options))) {
$in = $this->Dispatch->getInput($prompt, $options, $default);
}
}
if ($in) {
return $in;
}
}

/**
* Outputs to the stdout filehandle.
*
* @param string $string String to output.
* @param boolean $newline If true, the outputs gets an added newline.
* @access public
*/
function out($string, $newline = true) {
if (is_array($string)) {
$str = '';
foreach ($string as $message) {
$str .= $message ."\n";
}
$string = $str;
}
return $this->Dispatch->stdout($string, $newline);
}

/**
* Outputs to the stderr filehandle.
*
* @param string $string Error text to output.
* @access public
*/
function err($string) {
if (is_array($string)) {
$str = '';
foreach ($string as $message) {
$str .= $message ."\n";
}
$string = $str;
}
return $this->Dispatch->stderr($string."\n");
}

/**
* Outputs a series of minus characters to the standard output, acts as a visual separator.
*
* @param boolean $newline If true, the outputs gets an added newline.
* @access public
*/
function hr($newline = false) {
if ($newline) {
$this->out("\n");
}
$this->out('---------------------------------------------------------------');
if ($newline) {
$this->out("\n");
}
}

/**
* Displays a formatted error message and exits the application
*
* @param string $title Title of the error message
* @param string $msg Error message
* @access public
*/
function error($title, $msg) {
$out = "$title\n";
$out .= "$msg\n";
$out .= "\n";
$this->err($out);
$this->_stop();
}

/**
* Will check the number args matches otherwise throw an error
*
* @param integer $expectedNum Expected number of paramters
* @param string $command Command
* @access protected
*/
function _checkArgs($expectedNum, $command = null) {
if (!$command) {
$command = $this->command;
}
if (count($this->args) < $expectedNum) {
$this->error("Wrong number of parameters: ".count($this->args), "Expected: {$expectedNum}\nPlease type 'cake {$this->shell} help' for help on usage of the {$this->name} {$command}");
}
}

/**
* 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
* @access public
*/
function createFile ($path, $contents) {
$path = str_replace(DS . DS, DS, $path);
$this->out("\n" . sprintf(__("Creating file %s", true), $path));
if (is_file($path) && $this->interactive === true) {
$key = $this->in(__("File exists, overwrite?", true). " {$path}", array('y', 'n', 'q'), 'n');
if (low($key) == 'q') {
$this->out(__("Quitting.", true) ."\n");
exit;
} elseif (low($key) != 'y') {
$this->out(__("Skip", true) ." {$path}\n");
return false;
}
}
if (!class_exists('File')) {
uses('file');
}

if ($File = new File($path, true)) {
$data = $File->prepare($contents);
$File->write($data);
$this->out(__("Wrote", true) ." {$path}");
return true;
} else {
$this->err(__("Error! Could not write to", true)." {$path}.\n");
return false;
}
}

/**
* Outputs usage text on the standard output. Implement it in subclasses.
*
* @access public
*/
function help() {
if ($this->command != null) {
$this->err("Unknown {$this->name} command '$this->command'.\nFor usage, try 'cake {$this->shell} help'.\n\n");
} else {
$this->Dispatch->help();
}
}

/**
* Action to create a Unit Test
*
* @return boolean Success
* @access protected
*/
function _checkUnitTest() {
if (App::import('vendor', 'simpletest' . DS . 'simpletest')) {
return true;
}
$unitTest = $this->in('SimpleTest is not installed. Do you want to bake unit test files anyway?', array('y','n'), 'y');
$result = low($unitTest) == 'y' || low($unitTest) == 'yes';

if ($result) {
$this->out("\nYou can download SimpleTest from http://simpletest.org", true);
}
return $result;
}

/**
* Makes absolute file path easier to read
*
* @param string $file Absolute file path
* @return sting short path
* @access public
*/
function shortPath($file) {
$shortPath = str_replace(ROOT, null, $file);
$shortPath = str_replace('..'.DS, '', $shortPath);
return r(DS.DS, DS, $shortPath);
}

/**
* Creates the proper controller path for the specified controller class name
*
* @param string $name Controller class name
* @return string Path to controller
* @access protected
*/
function _controllerPath($name) {
return low(Inflector::underscore($name));
}

/**
* Creates the proper controller plural name for the specified controller class name
*
* @param string $name Controller class name
* @return string Controller plural name
* @access protected
*/
function _controllerName($name) {
return Inflector::pluralize(Inflector::camelize($name));
}

/**
* Creates the proper controller camelized name (singularized) for the specified name
*
* @param string $name Name
* @return string Camelized and singularized controller name
* @access protected
*/
function _modelName($name) {
return Inflector::camelize(Inflector::singularize($name));
}

/**
* Creates the proper singular model key for associations
*
* @param string $name Controller class name
* @return string Singular model key
* @access protected
*/
function _modelKey($name) {
return Inflector::underscore(Inflector::singularize($name)).'_id';
}

/**
* Creates the proper model name from a foreign key
*
* @param string $key Foreign key
* @return string Model name
* @access protected
*/
function _modelNameFromKey($key) {
$name = str_replace('_id', '',$key);
return Inflector::camelize($name);
}

/**
* creates the singular name for use in views.
*
* @param string $name
* @return string $name
* @access protected
*/
function _singularName($name) {
return Inflector::variable(Inflector::singularize($name));
}

/**
* Creates the plural name for views
*
* @param string $name Name to use
* @return string Plural name for views
* @access protected
*/
function _pluralName($name) {
return Inflector::variable(Inflector::pluralize($name));
}

/**
* Creates the singular human name used in views
*
* @param string $name Controller name
* @return string Singular human name
* @access protected
*/
function _singularHumanName($name) {
return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
}

/**
* Creates the plural human name used in views
*
* @param string $name Controller name
* @return string Plural human name
* @access protected
*/
function _pluralHumanName($name) {
return Inflector::humanize(Inflector::underscore(Inflector::pluralize($name)));
}

/**
* 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.
**/
function _pluginPath($pluginName) {
$pluginPaths = App::path('plugins');
$pluginDirName = Inflector::underscore($pluginName);
foreach ($pluginPaths as $path) {
if (is_dir($path . $pluginDirName)) {
return $path . $pluginDirName . DS ;
}
}
return $pluginPaths[0] . $pluginDirName . DS;
}
}
?>
Something went wrong with that request. Please try again.