1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: App::uses('AppShell', 'Console/Command');
18: App::uses('Inflector', 'Utility');
19:
20: 21: 22: 23: 24:
25: class CommandListShell extends AppShell {
26:
27: 28: 29: 30: 31:
32: public function startup() {
33: if (empty($this->params['xml'])) {
34: parent::startup();
35: }
36: }
37:
38: 39: 40: 41: 42:
43: public function main() {
44: if (empty($this->params['xml'])) {
45: $this->out(__d('cake_console', "<info>Current Paths:</info>"), 2);
46: $this->out(" -app: " . APP_DIR);
47: $this->out(" -working: " . rtrim(APP, DS));
48: $this->out(" -root: " . rtrim(ROOT, DS));
49: $this->out(" -core: " . rtrim(CORE_PATH, DS));
50: $this->out("");
51: $this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2);
52: $this->out(__d('cake_console', "Your working path should be the same as your application path. To change your path use the '-app' param."));
53: $this->out(__d('cake_console', "Example: %s or %s", '-app relative/path/to/myapp', '-app /absolute/path/to/myapp'), 2);
54:
55: $this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
56: }
57:
58: $shellList = $this->_getShellList();
59: if (empty($shellList)) {
60: return;
61: }
62:
63: if (empty($this->params['xml'])) {
64: $this->_asText($shellList);
65: } else {
66: $this->_asXml($shellList);
67: }
68: }
69:
70: 71: 72: 73: 74:
75: protected function _getShellList() {
76: $skipFiles = array('AppShell');
77:
78: $plugins = CakePlugin::loaded();
79: $shellList = array_fill_keys($plugins, null) + array('CORE' => null, 'app' => null);
80:
81: $corePath = App::core('Console/Command');
82: $shells = App::objects('file', $corePath[0]);
83: $shells = array_diff($shells, $skipFiles);
84: $this->_appendShells('CORE', $shells, $shellList);
85:
86: $appShells = App::objects('Console/Command', null, false);
87: $appShells = array_diff($appShells, $shells, $skipFiles);
88: $this->_appendShells('app', $appShells, $shellList);
89:
90: foreach ($plugins as $plugin) {
91: $pluginShells = App::objects($plugin . '.Console/Command');
92: $this->_appendShells($plugin, $pluginShells, $shellList);
93: }
94:
95: return array_filter($shellList);
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105:
106: protected function _appendShells($type, $shells, &$shellList) {
107: foreach ($shells as $shell) {
108: $shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
109: }
110: }
111:
112: 113: 114: 115: 116: 117:
118: protected function _asText($shellList) {
119: foreach ($shellList as $plugin => $commands) {
120: sort($commands);
121: $this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
122: $this->out();
123: }
124:
125: $this->out(__d('cake_console', "To run an app or core command, type <info>cake shell_name [args]</info>"));
126: $this->out(__d('cake_console', "To run a plugin command, type <info>cake Plugin.shell_name [args]</info>"));
127: $this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
128: }
129:
130: 131: 132: 133: 134: 135:
136: protected function _asXml($shellList) {
137: $plugins = CakePlugin::loaded();
138: $shells = new SimpleXmlElement('<shells></shells>');
139: foreach ($shellList as $plugin => $commands) {
140: foreach ($commands as $command) {
141: $callable = $command;
142: if (in_array($plugin, $plugins)) {
143: $callable = Inflector::camelize($plugin) . '.' . $command;
144: }
145:
146: $shell = $shells->addChild('shell');
147: $shell->addAttribute('name', $command);
148: $shell->addAttribute('call_as', $callable);
149: $shell->addAttribute('provider', $plugin);
150: $shell->addAttribute('help', $callable . ' -h');
151: }
152: }
153: $this->stdout->outputAs(ConsoleOutput::RAW);
154: $this->out($shells->saveXml());
155: }
156:
157: 158: 159: 160: 161:
162: public function getOptionParser() {
163: $parser = parent::getOptionParser();
164: return $parser->description(__d('cake_console', 'Get the list of available shells for this CakePHP application.'))
165: ->addOption('sort', array(
166: 'help' => __d('cake_console', 'Does nothing (deprecated)'),
167: 'boolean' => true
168: ))
169: ->addOption('xml', array(
170: 'help' => __d('cake_console', 'Get the listing as XML.'),
171: 'boolean' => true
172: ));
173: }
174:
175: }
176: