source: Dev/branches/cakephp/cake/console/cake.php @ 126

Last change on this file since 126 was 126, checked in by fpvanagthoven, 14 years ago

Cakephp branch.

File size: 16.5 KB
Line 
1#!/usr/bin/php -q
2<?php
3/**
4 * Command-line code generation utility to automate programmer chores.
5 *
6 * Shell dispatcher class
7 *
8 * PHP versions 4 and 5
9 *
10 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
11 * Copyright 2005-2011, Cake Software Foundation, Inc.
12 *
13 * Licensed under The MIT License
14 * Redistributions of files must retain the above copyright notice.
15 *
16 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
17 * @link          http://cakephp.org CakePHP(tm) Project
18 * @package       cake
19 * @subpackage    cake.cake.console
20 * @since         CakePHP(tm) v 1.2.0.5012
21 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
22 */
23if (!defined('E_DEPRECATED')) {
24        define('E_DEPRECATED', 8192);
25}
26/**
27 * Shell dispatcher
28 *
29 * @package       cake
30 * @subpackage    cake.cake.console
31 */
32class ShellDispatcher {
33
34/**
35 * Standard input stream.
36 *
37 * @var filehandle
38 * @access public
39 */
40        var $stdin;
41
42/**
43 * Standard output stream.
44 *
45 * @var filehandle
46 * @access public
47 */
48        var $stdout;
49
50/**
51 * Standard error stream.
52 *
53 * @var filehandle
54 * @access public
55 */
56        var $stderr;
57
58/**
59 * Contains command switches parsed from the command line.
60 *
61 * @var array
62 * @access public
63 */
64        var $params = array();
65
66/**
67 * Contains arguments parsed from the command line.
68 *
69 * @var array
70 * @access public
71 */
72        var $args = array();
73
74/**
75 * The file name of the shell that was invoked.
76 *
77 * @var string
78 * @access public
79 */
80        var $shell = null;
81
82/**
83 * The class name of the shell that was invoked.
84 *
85 * @var string
86 * @access public
87 */
88        var $shellClass = null;
89
90/**
91 * The command called if public methods are available.
92 *
93 * @var string
94 * @access public
95 */
96        var $shellCommand = null;
97
98/**
99 * The path locations of shells.
100 *
101 * @var array
102 * @access public
103 */
104        var $shellPaths = array();
105
106/**
107 * The path to the current shell location.
108 *
109 * @var string
110 * @access public
111 */
112        var $shellPath = null;
113
114/**
115 * The name of the shell in camelized.
116 *
117 * @var string
118 * @access public
119 */
120        var $shellName = null;
121
122/**
123 * Constructor
124 *
125 * The execution of the script is stopped after dispatching the request with
126 * a status code of either 0 or 1 according to the result of the dispatch.
127 *
128 * @param array $args the argv
129 * @return void
130 * @access public
131 */
132        function ShellDispatcher($args = array()) {
133                set_time_limit(0);
134
135                $this->__initConstants();
136                $this->parseParams($args);
137                $this->_initEnvironment();
138                $this->__buildPaths();
139                $this->_stop($this->dispatch() === false ? 1 : 0);
140        }
141
142/**
143 * Defines core configuration.
144 *
145 * @access private
146 */
147        function __initConstants() {
148                if (function_exists('ini_set')) {
149                        ini_set('display_errors', '1');
150                        ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
151                        ini_set('html_errors', false);
152                        ini_set('implicit_flush', true);
153                        ini_set('max_execution_time', 0);
154                }
155
156                if (!defined('CAKE_CORE_INCLUDE_PATH')) {
157                        define('PHP5', (PHP_VERSION >= 5));
158                        define('DS', DIRECTORY_SEPARATOR);
159                        define('CAKE_CORE_INCLUDE_PATH', dirname(dirname(dirname(__FILE__))));
160                        define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
161                        define('DISABLE_DEFAULT_ERROR_HANDLING', false);
162                        define('CAKEPHP_SHELL', true);
163                }
164                require_once(CORE_PATH . 'cake' . DS . 'basics.php');
165        }
166
167/**
168 * Defines current working environment.
169 *
170 * @access protected
171 */
172        function _initEnvironment() {
173                $this->stdin = fopen('php://stdin', 'r');
174                $this->stdout = fopen('php://stdout', 'w');
175                $this->stderr = fopen('php://stderr', 'w');
176
177                if (!$this->__bootstrap()) {
178                        $this->stderr("\nCakePHP Console: ");
179                        $this->stderr("\nUnable to load Cake core:");
180                        $this->stderr("\tMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH);
181                        $this->_stop();
182                }
183
184                if (!isset($this->args[0]) || !isset($this->params['working'])) {
185                        $this->stderr("\nCakePHP Console: ");
186                        $this->stderr('This file has been loaded incorrectly and cannot continue.');
187                        $this->stderr('Please make sure that ' . DIRECTORY_SEPARATOR . 'cake' . DIRECTORY_SEPARATOR . 'console is in your system path,');
188                        $this->stderr('and check the manual for the correct usage of this command.');
189                        $this->stderr('(http://manual.cakephp.org/)');
190                        $this->_stop();
191                }
192
193                if (basename(__FILE__) !=  basename($this->args[0])) {
194                        $this->stderr("\nCakePHP Console: ");
195                        $this->stderr('Warning: the dispatcher may have been loaded incorrectly, which could lead to unexpected results...');
196                        if ($this->getInput('Continue anyway?', array('y', 'n'), 'y') == 'n') {
197                                $this->_stop();
198                        }
199                }
200
201                $this->shiftArgs();
202        }
203
204/**
205 * Builds the shell paths.
206 *
207 * @access private
208 * @return void
209 */
210        function __buildPaths() {
211                $paths = array();
212                if (!class_exists('Folder')) {
213                        require LIBS . 'folder.php';
214                }
215                $plugins = App::objects('plugin', null, false);
216                foreach ((array)$plugins as $plugin) {
217                        $pluginPath = App::pluginPath($plugin);
218                        $path = $pluginPath . 'vendors' . DS . 'shells' . DS;
219                        if (file_exists($path)) {
220                                $paths[] = $path;
221                        }
222                }
223
224                $vendorPaths = array_values(App::path('vendors'));
225                foreach ($vendorPaths as $vendorPath) {
226                        $path = rtrim($vendorPath, DS) . DS . 'shells' . DS;
227                        if (file_exists($path)) {
228                                $paths[] = $path;
229                        }
230                }
231
232                $this->shellPaths = array_values(array_unique(array_merge($paths, App::path('shells'))));
233        }
234
235/**
236 * Initializes the environment and loads the Cake core.
237 *
238 * @return boolean Success.
239 * @access private
240 */
241        function __bootstrap() {
242
243                define('ROOT', $this->params['root']);
244                define('APP_DIR', $this->params['app']);
245                define('APP_PATH', $this->params['working'] . DS);
246                define('WWW_ROOT', APP_PATH . $this->params['webroot'] . DS);
247                if (!is_dir(ROOT . DS . APP_DIR . DS . 'tmp')) {
248                        define('TMP', CORE_PATH . 'cake' . DS . 'console' . DS . 'templates' . DS . 'skel' . DS . 'tmp' . DS);
249                }
250
251                $includes = array(
252                        CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php',
253                        CORE_PATH . 'cake' . DS . 'libs' . DS . 'object.php',
254                        CORE_PATH . 'cake' . DS . 'libs' . DS . 'inflector.php',
255                        CORE_PATH . 'cake' . DS . 'libs' . DS . 'configure.php',
256                        CORE_PATH . 'cake' . DS . 'libs' . DS . 'file.php',
257                        CORE_PATH . 'cake' . DS . 'libs' . DS . 'cache.php',
258                        CORE_PATH . 'cake' . DS . 'libs' . DS . 'string.php',
259                        CORE_PATH . 'cake' . DS . 'libs' . DS . 'class_registry.php',
260                        CORE_PATH . 'cake' . DS . 'console' . DS . 'error.php'
261                );
262
263                foreach ($includes as $inc) {
264                        if (!require($inc)) {
265                                $this->stderr("Failed to load Cake core file {$inc}");
266                                return false;
267                        }
268                }
269
270                Configure::getInstance(file_exists(CONFIGS . 'bootstrap.php'));
271
272                if (!file_exists(APP_PATH . 'config' . DS . 'core.php')) {
273                        include_once CORE_PATH . 'cake' . DS . 'console' . DS . 'templates' . DS . 'skel' . DS . 'config' . DS . 'core.php';
274                        App::build();
275                }
276
277                return true;
278        }
279
280/**
281 * Clear the console
282 *
283 * @return void
284 * @access public
285 */
286        function clear() {
287                if (empty($this->params['noclear'])) {
288                        if ( DS === '/') {
289                                passthru('clear');
290                        } else {
291                                passthru('cls');
292                        }
293                }
294        }
295
296/**
297 * Dispatches a CLI request
298 *
299 * @return boolean
300 * @access public
301 */
302        function dispatch() {
303                $arg = $this->shiftArgs();
304
305                if (!$arg) {
306                        $this->help();
307                        return false;
308                }
309                if ($arg == 'help') {
310                        $this->help();
311                        return true;
312                }
313               
314                list($plugin, $shell) = pluginSplit($arg);
315                $this->shell = $shell;
316                $this->shellName = Inflector::camelize($shell);
317                $this->shellClass = $this->shellName . 'Shell';
318
319                $arg = null;
320
321                if (isset($this->args[0])) {
322                        $arg = $this->args[0];
323                        $this->shellCommand = Inflector::variable($arg);
324                }
325
326                $Shell = $this->_getShell($plugin);
327
328                if (!$Shell) {
329                        $title = sprintf(__('Error: Class %s could not be loaded.', true), $this->shellClass);
330                        $this->stderr($title . "\n");
331                        return false;
332                }
333
334                $methods = array();
335
336                if (is_a($Shell, 'Shell')) {
337                        $Shell->initialize();
338                        $Shell->loadTasks();
339
340                        foreach ($Shell->taskNames as $task) {
341                                if (is_a($Shell->{$task}, 'Shell')) {
342                                        $Shell->{$task}->initialize();
343                                        $Shell->{$task}->loadTasks();
344                                }
345                        }
346
347                        $task = Inflector::camelize($arg);
348
349                        if (in_array($task, $Shell->taskNames)) {
350                                $this->shiftArgs();
351                                $Shell->{$task}->startup();
352
353                                if (isset($this->args[0]) && $this->args[0] == 'help') {
354                                        if (method_exists($Shell->{$task}, 'help')) {
355                                                $Shell->{$task}->help();
356                                        } else {
357                                                $this->help();
358                                        }
359                                        return true;
360                                }
361                                return $Shell->{$task}->execute();
362                        }
363                        $methods = array_diff(get_class_methods('Shell'), array('help'));
364                }
365                $methods = array_diff(get_class_methods($Shell), $methods);
366                $added = in_array(strtolower($arg), array_map('strtolower', $methods));
367                $private = $arg[0] == '_' && method_exists($Shell, $arg);
368
369                if (!$private) {
370                        if ($added) {
371                                $this->shiftArgs();
372                                $Shell->startup();
373                                return $Shell->{$arg}();
374                        }
375                        if (method_exists($Shell, 'main')) {
376                                $Shell->startup();
377                                return $Shell->main();
378                        }
379                }
380
381                $title = sprintf(__('Error: Unknown %1$s command %2$s.', true), $this->shellName, $arg);
382                $message = sprintf(__('For usage try `cake %s help`', true), $this->shell);
383                $this->stderr($title . "\n" . $message . "\n");
384                return false;
385        }
386
387/**
388 * Get shell to use, either plugin shell or application shell
389 *
390 * All paths in the shellPaths property are searched.
391 * shell, shellPath and shellClass properties are taken into account.
392 *
393 * @param string $plugin Optionally the name of a plugin
394 * @return mixed False if no shell could be found or an object on success
395 * @access protected
396 */
397        function _getShell($plugin = null) {
398                foreach ($this->shellPaths as $path) {
399                        $this->shellPath = $path . $this->shell . '.php';
400                        $pluginShellPath =  DS . $plugin . DS . 'vendors' . DS . 'shells' . DS;
401
402                        if ((strpos($path, $pluginShellPath) !== false || !$plugin) && file_exists($this->shellPath)) {
403                                $loaded = true;
404                                break;
405                        }
406                }
407                if (!isset($loaded)) {
408                        return false;
409                }
410
411                if (!class_exists('Shell')) {
412                        require CONSOLE_LIBS . 'shell.php';
413                }
414
415                if (!class_exists($this->shellClass)) {
416                        require $this->shellPath;
417                }
418                if (!class_exists($this->shellClass)) {
419                        return false;
420                }
421                $Shell = new $this->shellClass($this);
422                return $Shell;
423        }
424
425/**
426 * Prompts the user for input, and returns it.
427 *
428 * @param string $prompt Prompt text.
429 * @param mixed $options Array or string of options.
430 * @param string $default Default input value.
431 * @return Either the default value, or the user-provided input.
432 * @access public
433 */
434        function getInput($prompt, $options = null, $default = null) {
435                if (!is_array($options)) {
436                        $printOptions = '';
437                } else {
438                        $printOptions = '(' . implode('/', $options) . ')';
439                }
440
441                if ($default === null) {
442                        $this->stdout($prompt . " $printOptions \n" . '> ', false);
443                } else {
444                        $this->stdout($prompt . " $printOptions \n" . "[$default] > ", false);
445                }
446                $result = fgets($this->stdin);
447
448                if ($result === false) {
449                        exit;
450                }
451                $result = trim($result);
452
453                if ($default != null && empty($result)) {
454                        return $default;
455                }
456                return $result;
457        }
458
459/**
460 * Outputs to the stdout filehandle.
461 *
462 * @param string $string String to output.
463 * @param boolean $newline If true, the outputs gets an added newline.
464 * @return integer Returns the number of bytes output to stdout.
465 * @access public
466 */
467        function stdout($string, $newline = true) {
468                if ($newline) {
469                        return fwrite($this->stdout, $string . "\n");
470                } else {
471                        return fwrite($this->stdout, $string);
472                }
473        }
474
475/**
476 * Outputs to the stderr filehandle.
477 *
478 * @param string $string Error text to output.
479 * @access public
480 */
481        function stderr($string) {
482                fwrite($this->stderr, $string);
483        }
484
485/**
486 * Parses command line options
487 *
488 * @param array $params Parameters to parse
489 * @access public
490 */
491        function parseParams($params) {
492                $this->__parseParams($params);
493                $defaults = array('app' => 'app', 'root' => dirname(dirname(dirname(__FILE__))), 'working' => null, 'webroot' => 'webroot');
494                $params = array_merge($defaults, array_intersect_key($this->params, $defaults));
495                $isWin = false;
496                foreach ($defaults as $default => $value) {
497                        if (strpos($params[$default], '\\') !== false) {
498                                $isWin = true;
499                                break;
500                        }
501                }
502                $params = str_replace('\\', '/', $params);
503
504                if (isset($params['working'])) {
505                        $params['working'] = trim($params['working']);
506                }
507                if (!empty($params['working']) && (!isset($this->args[0]) || isset($this->args[0]) && $this->args[0]{0} !== '.')) {
508                        if (empty($this->params['app']) && $params['working'] != $params['root']) {
509                                $params['root'] = dirname($params['working']);
510                                $params['app'] = basename($params['working']);
511                        } else {
512                                $params['root'] = $params['working'];
513                        }
514                }
515
516                if ($params['app'][0] == '/' || preg_match('/([a-z])(:)/i', $params['app'], $matches)) {
517                        $params['root'] = dirname($params['app']);
518                } elseif (strpos($params['app'], '/')) {
519                        $params['root'] .= '/' . dirname($params['app']);
520                }
521
522                $params['app'] = basename($params['app']);
523                $params['working'] = rtrim($params['root'], '/');
524                if (!$isWin || !preg_match('/^[A-Z]:$/i', $params['app'])) {
525                        $params['working'] .= '/' . $params['app'];
526                }
527
528                if (!empty($matches[0]) || !empty($isWin)) {
529                        $params = str_replace('/', '\\', $params);
530                }
531
532                $this->params = array_merge($this->params, $params);
533        }
534
535/**
536 * Helper for recursively parsing params
537 *
538 * @return array params
539 * @access private
540 */
541        function __parseParams($params) {
542                $count = count($params);
543                for ($i = 0; $i < $count; $i++) {
544                        if (isset($params[$i])) {
545                                if ($params[$i]{0} === '-') {
546                                        $key = substr($params[$i], 1);
547                                        $this->params[$key] = true;
548                                        unset($params[$i]);
549                                        if (isset($params[++$i])) {
550                                                if ($params[$i]{0} !== '-') {
551                                                        $this->params[$key] = str_replace('"', '', $params[$i]);
552                                                        unset($params[$i]);
553                                                } else {
554                                                        $i--;
555                                                        $this->__parseParams($params);
556                                                }
557                                        }
558                                } else {
559                                        $this->args[] = $params[$i];
560                                        unset($params[$i]);
561                                }
562
563                        }
564                }
565        }
566
567/**
568 * Removes first argument and shifts other arguments up
569 *
570 * @return mixed Null if there are no arguments otherwise the shifted argument
571 * @access public
572 */
573        function shiftArgs() {
574                return array_shift($this->args);
575        }
576
577/**
578 * Shows console help
579 *
580 * @access public
581 */
582        function help() {
583                $this->clear();
584                $this->stdout("\nWelcome to CakePHP v" . Configure::version() . " Console");
585                $this->stdout("---------------------------------------------------------------");
586                $this->stdout("Current Paths:");
587                $this->stdout(" -app: ". $this->params['app']);
588                $this->stdout(" -working: " . rtrim($this->params['working'], DS));
589                $this->stdout(" -root: " . rtrim($this->params['root'], DS));
590                $this->stdout(" -core: " . rtrim(CORE_PATH, DS));
591                $this->stdout("");
592                $this->stdout("Changing Paths:");
593                $this->stdout("your working path should be the same as your application path");
594                $this->stdout("to change your path use the '-app' param.");
595                $this->stdout("Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp");
596
597                $this->stdout("\nAvailable Shells:");
598                $shellList = array();
599                foreach ($this->shellPaths as $path) {
600                        if (!is_dir($path)) {
601                                continue;
602                        }
603                        $shells = App::objects('file', $path);
604                        if (empty($shells)) {
605                                continue;
606                        }
607                        if (preg_match('@plugins[\\\/]([^\\\/]*)@', $path, $matches)) {
608                                $type = Inflector::camelize($matches[1]);
609                        } elseif (preg_match('@([^\\\/]*)[\\\/]vendors[\\\/]@', $path, $matches)) {
610                                $type = $matches[1];
611                        } elseif (strpos($path, CAKE_CORE_INCLUDE_PATH . DS . 'cake') === 0) {
612                                $type = 'CORE';
613                        } else {
614                                $type = 'app';
615                        }
616                        foreach ($shells as $shell) {
617                                if ($shell !== 'shell.php') {
618                                        $shell = str_replace('.php', '', $shell);
619                                        $shellList[$shell][$type] = $type;
620                                }
621                        }
622                }
623                if ($shellList) {
624                        ksort($shellList);
625                        if (DS === '/') {
626                                $width = exec('tput cols') - 2;
627                        }
628                        if (empty($width)) {
629                                $width = 80;
630                        }
631                        $columns = max(1, floor($width / 30));
632                        $rows = ceil(count($shellList) / $columns);
633
634                        foreach ($shellList as $shell => $types) {
635                                sort($types);
636                                $shellList[$shell] = str_pad($shell . ' [' . implode ($types, ', ') . ']', $width / $columns);
637                        }
638                        $out = array_chunk($shellList, $rows);
639                        for ($i = 0; $i < $rows; $i++) {
640                                $row = '';
641                                for ($j = 0; $j < $columns; $j++) {
642                                        if (!isset($out[$j][$i])) {
643                                                continue;
644                                        }
645                                        $row .= $out[$j][$i];
646                                }
647                                $this->stdout(" " . $row);
648                        }
649                }
650                $this->stdout("\nTo run a command, type 'cake shell_name [args]'");
651                $this->stdout("To get help on a specific command, type 'cake shell_name help'");
652        }
653
654/**
655 * Stop execution of the current script
656 *
657 * @param $status see http://php.net/exit for values
658 * @return void
659 * @access protected
660 */
661        function _stop($status = 0) {
662                exit($status);
663        }
664}
665if (!defined('DISABLE_AUTO_DISPATCH')) {
666        $dispatcher = new ShellDispatcher($argv);
667}
Note: See TracBrowser for help on using the repository browser.