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