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