00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Shell extends Object {
00036
00037
00038
00039
00040
00041
00042 var $Dispatch = null;
00043
00044
00045
00046
00047
00048
00049 var $interactive = true;
00050
00051
00052
00053
00054
00055
00056
00057 var $DbConfig = null;
00058
00059
00060
00061
00062
00063
00064 var $params = array();
00065
00066
00067
00068
00069
00070
00071 var $args = array();
00072
00073
00074
00075
00076
00077
00078 var $shell = null;
00079
00080
00081
00082
00083
00084
00085 var $className = null;
00086
00087
00088
00089
00090
00091
00092 var $command = null;
00093
00094
00095
00096
00097
00098
00099 var $name = null;
00100
00101
00102
00103
00104
00105
00106 var $tasks = array();
00107
00108
00109
00110
00111
00112
00113 var $taskNames = array();
00114
00115
00116
00117
00118
00119
00120 var $uses = array();
00121
00122
00123
00124
00125 function __construct(&$dispatch) {
00126 $vars = array('params', 'args', 'shell', 'shellCommand'=> 'command');
00127 foreach ($vars as $key => $var) {
00128 if (is_string($key)) {
00129 $this->{$var} =& $dispatch->{$key};
00130 } else {
00131 $this->{$var} =& $dispatch->{$var};
00132 }
00133 }
00134
00135 $this->className = get_class($this);
00136
00137 if ($this->name == null) {
00138 $this->name = str_replace(array('shell', 'Shell', 'task', 'Task'), '', $this->className);
00139 }
00140
00141 $shellKey = Inflector::underscore($this->className);
00142 ClassRegistry::addObject($shellKey, $this);
00143 ClassRegistry::map($shellKey, $shellKey);
00144
00145 if (!PHP5 && isset($this->args[0])) {
00146 if(strpos($this->className, low(Inflector::camelize($this->args[0]))) !== false) {
00147 $dispatch->shiftArgs();
00148 }
00149 if (low($this->command) == low(Inflector::variable($this->args[0])) && method_exists($this, $this->command)) {
00150 $dispatch->shiftArgs();
00151 }
00152 }
00153
00154 $this->Dispatch =& $dispatch;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163 function initialize() {
00164 $this->_loadModels();
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 function startup() {
00174 $this->_welcome();
00175 }
00176
00177
00178
00179
00180
00181 function _welcome() {
00182 $this->out("\nWelcome to CakePHP v" . Configure::version() . " Console");
00183 $this->out("---------------------------------------------------------------");
00184 $this->out('App : '. $this->params['app']);
00185 $this->out('Path: '. $this->params['working']);
00186 $this->hr();
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 function _loadDbConfig() {
00196 if (config('database') && class_exists('DATABASE_CONFIG')) {
00197 $this->DbConfig =& new DATABASE_CONFIG();
00198 return true;
00199 }
00200 $this->err('Database config could not be loaded');
00201 $this->out('Run \'bake\' to create the database configuration');
00202 return false;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 function _loadModels() {
00214 if ($this->uses === null || $this->uses === false) {
00215 return;
00216 }
00217
00218 if ($this->uses === true && App::import('Model', 'AppModel')) {
00219 $this->AppModel =& new AppModel(false, false, false);
00220 return true;
00221 }
00222
00223 if ($this->uses !== true && !empty($this->uses)) {
00224 $uses = is_array($this->uses) ? $this->uses : array($this->uses);
00225 $this->modelClass = $uses[0];
00226
00227 foreach ($uses as $modelClass) {
00228 if (PHP5) {
00229 $this->{$modelClass} = ClassRegistry::init($modelClass);
00230 } else {
00231 $this->{$modelClass} =& ClassRegistry::init($modelClass);
00232 }
00233 }
00234 return true;
00235 }
00236 return false;
00237 }
00238
00239
00240
00241
00242
00243
00244 function loadTasks() {
00245 if ($this->tasks === null || $this->tasks === false) {
00246 return;
00247 }
00248
00249 if ($this->tasks !== true && !empty($this->tasks)) {
00250
00251 $tasks = $this->tasks;
00252 if (!is_array($tasks)) {
00253 $tasks = array($tasks);
00254 }
00255
00256 foreach ($tasks as $taskName) {
00257 $task = Inflector::underscore($taskName);
00258 $taskClass = Inflector::camelize($taskName.'Task');
00259 $taskKey = Inflector::underscore($taskClass);
00260
00261 if (!class_exists($taskClass)) {
00262 foreach ($this->Dispatch->shellPaths as $path) {
00263 $taskPath = $path . 'tasks' . DS . $task.'.php';
00264 if (file_exists($taskPath)) {
00265 require_once $taskPath;
00266 break;
00267 }
00268 }
00269 }
00270 if (ClassRegistry::isKeySet($taskKey)) {
00271 $this->taskNames[] = $taskName;
00272 if (!PHP5) {
00273 $this->{$taskName} =& ClassRegistry::getObject($taskKey);
00274 ClassRegistry::map($taskName, $taskKey);
00275 } else {
00276 $this->{$taskName} = ClassRegistry::getObject($taskKey);
00277 ClassRegistry::map($taskName, $taskKey);
00278 }
00279 } else {
00280
00281 $this->taskNames[] = $taskName;
00282 if (!PHP5) {
00283 $this->{$taskName} =& new $taskClass($this->Dispatch);
00284 } else {
00285 $this->{$taskName} = new $taskClass($this->Dispatch);
00286 }
00287 }
00288
00289 if (!isset($this->{$taskName})) {
00290 $this->err("Task '".$taskName."' could not be loaded");
00291 $this->_stop();
00292 }
00293 }
00294 }
00295
00296 return false;
00297 }
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 function in($prompt, $options = null, $default = null) {
00308 if (!$this->interactive) {
00309 return $default;
00310 }
00311 $in = $this->Dispatch->getInput($prompt, $options, $default);
00312
00313 if ($options && is_string($options)) {
00314 if (strpos($options, ',')) {
00315 $options = explode(',', $options);
00316 } elseif (strpos($options, '/')) {
00317 $options = explode('/', $options);
00318 } else {
00319 $options = array($options);
00320 }
00321 }
00322 if (is_array($options)) {
00323 while ($in == '' || ($in && (!in_array(low($in), $options) && !in_array(up($in), $options)) && !in_array($in, $options))) {
00324 $in = $this->Dispatch->getInput($prompt, $options, $default);
00325 }
00326 }
00327 if ($in) {
00328 return $in;
00329 }
00330 }
00331
00332
00333
00334
00335
00336
00337
00338 function out($string, $newline = true) {
00339 if (is_array($string)) {
00340 $str = '';
00341 foreach($string as $message) {
00342 $str .= $message ."\n";
00343 }
00344 $string = $str;
00345 }
00346 return $this->Dispatch->stdout($string, $newline);
00347 }
00348
00349
00350
00351
00352
00353
00354 function err($string) {
00355 if (is_array($string)) {
00356 $str = '';
00357 foreach($string as $message) {
00358 $str .= $message ."\n";
00359 }
00360 $string = $str;
00361 }
00362 return $this->Dispatch->stderr($string."\n");
00363 }
00364
00365
00366
00367
00368
00369
00370 function hr($newline = false) {
00371 if ($newline) {
00372 $this->out("\n");
00373 }
00374 $this->out('---------------------------------------------------------------');
00375 if ($newline) {
00376 $this->out("\n");
00377 }
00378 }
00379
00380
00381
00382
00383
00384
00385
00386 function error($title, $msg) {
00387 $out = "$title\n";
00388 $out .= "$msg\n";
00389 $out .= "\n";
00390 $this->err($out);
00391 $this->_stop();
00392 }
00393
00394
00395
00396
00397
00398
00399
00400 function _checkArgs($expectedNum, $command = null) {
00401 if (!$command) {
00402 $command = $this->command;
00403 }
00404 if (count($this->args) < $expectedNum) {
00405 $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}");
00406 }
00407 }
00408
00409
00410
00411
00412
00413
00414
00415
00416 function createFile ($path, $contents) {
00417 $path = str_replace(DS . DS, DS, $path);
00418 $this->out("\n" . sprintf(__("Creating file %s", true), $path));
00419 if (is_file($path) && $this->interactive === true) {
00420 $key = $this->in(__("File exists, overwrite?", true). " {$path}", array('y', 'n', 'q'), 'n');
00421 if (low($key) == 'q') {
00422 $this->out(__("Quitting.", true) ."\n");
00423 exit;
00424 } elseif (low($key) != 'y') {
00425 $this->out(__("Skip", true) ." {$path}\n");
00426 return false;
00427 }
00428 }
00429 if (!class_exists('File')) {
00430 uses('file');
00431 }
00432
00433 if ($File = new File($path, true)) {
00434 $data = $File->prepare($contents);
00435 $File->write($data);
00436 $this->out(__("Wrote", true) ." {$path}");
00437 return true;
00438 } else {
00439 $this->err(__("Error! Could not write to", true)." {$path}.\n");
00440 return false;
00441 }
00442 }
00443
00444
00445
00446
00447
00448 function help() {
00449 if ($this->command != null) {
00450 $this->err("Unknown {$this->name} command '$this->command'.\nFor usage, try 'cake {$this->shell} help'.\n\n");
00451 } else {
00452 $this->Dispatch->help();
00453 }
00454 }
00455
00456
00457
00458
00459
00460
00461 function _checkUnitTest() {
00462 if (is_dir(VENDORS.'simpletest') || is_dir(ROOT.DS.APP_DIR.DS.'vendors'.DS.'simpletest')) {
00463 return true;
00464 }
00465 $unitTest = $this->in('Cake test suite not installed. Do you want to bake unit test files anyway?', array('y','n'), 'y');
00466 $result = low($unitTest) == 'y' || low($unitTest) == 'yes';
00467
00468 if ($result) {
00469 $this->out("\nYou can download the Cake test suite from http://cakeforge.org/projects/testsuite/", true);
00470 }
00471 return $result;
00472 }
00473
00474
00475
00476
00477
00478
00479
00480 function shortPath($file) {
00481 $shortPath = str_replace(ROOT, null, $file);
00482 $shortPath = str_replace('..'.DS, '', $shortPath);
00483 return r(DS.DS, DS, $shortPath);
00484 }
00485
00486
00487
00488
00489
00490
00491 function getAdmin() {
00492 $admin = '';
00493 $cakeAdmin = null;
00494 $adminRoute = Configure::read('Routing.admin');
00495 if (!empty($adminRoute)) {
00496 $cakeAdmin = $adminRoute . '_';
00497 } else {
00498 $this->out('You need to enable Configure::write(\'Routing.admin\',\'admin\') in /app/config/core.php to use admin routing.');
00499 $this->out('What would you like the admin route to be?');
00500 $this->out('Example: www.example.com/admin/controller');
00501 while ($admin == '') {
00502 $admin = $this->in("What would you like the admin route to be?", null, 'admin');
00503 }
00504 if ($this->Project->cakeAdmin($admin) !== true) {
00505 $this->out('Unable to write to /app/config/core.php.');
00506 $this->out('You need to enable Configure::write(\'Routing.admin\',\'admin\') in /app/config/core.php to use admin routing.');
00507 $this->_stop();
00508 } else {
00509 $cakeAdmin = $admin . '_';
00510 }
00511 }
00512 return $cakeAdmin;
00513 }
00514
00515
00516
00517
00518
00519
00520
00521 function _controllerPath($name) {
00522 return low(Inflector::underscore($name));
00523 }
00524
00525
00526
00527
00528
00529
00530
00531 function _controllerName($name) {
00532 return Inflector::pluralize(Inflector::camelize($name));
00533 }
00534
00535
00536
00537
00538
00539
00540
00541 function _modelName($name) {
00542 return Inflector::camelize(Inflector::singularize($name));
00543 }
00544
00545
00546
00547
00548
00549
00550
00551 function _modelKey($name) {
00552 return Inflector::underscore(Inflector::singularize($name)).'_id';
00553 }
00554
00555
00556
00557
00558
00559
00560
00561 function _modelNameFromKey($key) {
00562 $name = str_replace('_id', '',$key);
00563 return Inflector::camelize($name);
00564 }
00565
00566
00567
00568
00569
00570
00571
00572 function _singularName($name) {
00573 return Inflector::variable(Inflector::singularize($name));
00574 }
00575
00576
00577
00578
00579
00580
00581
00582 function _pluralName($name) {
00583 return Inflector::variable(Inflector::pluralize($name));
00584 }
00585
00586
00587
00588
00589
00590
00591
00592 function _singularHumanName($name) {
00593 return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
00594 }
00595
00596
00597
00598
00599
00600
00601
00602 function _pluralHumanName($name) {
00603 return Inflector::humanize(Inflector::underscore(Inflector::pluralize($name)));
00604 }
00605 }
00606 ?>