1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Shell;
16:
17: use Cake\Console\ConsoleOutput;
18: use Cake\Console\Shell;
19: use Cake\Core\Configure;
20: use Cake\Core\Plugin;
21: use Cake\Utility\Inflector;
22: use SimpleXMLElement;
23:
24: 25: 26: 27: 28: 29:
30: class CommandListShell extends Shell
31: {
32: 33: 34: 35: 36:
37: public $tasks = ['Command'];
38:
39: 40: 41: 42: 43:
44: protected function _welcome()
45: {
46: $this->out();
47: $this->out(sprintf('<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
48: $this->hr();
49: $this->out(sprintf('App : %s', APP_DIR));
50: $this->out(sprintf('Path: %s', APP));
51: $this->out(sprintf('PHP : %s', PHP_VERSION));
52: $this->hr();
53: }
54:
55: 56: 57: 58: 59:
60: public function startup()
61: {
62: if (!$this->param('xml') && !$this->param('version')) {
63: parent::startup();
64: }
65: }
66:
67: 68: 69: 70: 71:
72: public function main()
73: {
74: if (!$this->param('xml') && !$this->param('version')) {
75: $this->out('<info>Current Paths:</info>', 2);
76: $this->out('* app: ' . APP_DIR . DIRECTORY_SEPARATOR);
77: $this->out('* root: ' . ROOT . DIRECTORY_SEPARATOR);
78: $this->out('* core: ' . CORE_PATH);
79: $this->out('');
80:
81: $this->out('<info>Available Shells:</info>', 2);
82: }
83:
84: if ($this->param('version')) {
85: $this->out(Configure::version());
86:
87: return;
88: }
89:
90: $shellList = $this->Command->getShellList();
91: if (!$shellList) {
92: return;
93: }
94:
95: if (!$this->param('xml')) {
96: $this->_asText($shellList);
97: } else {
98: $this->_asXml($shellList);
99: }
100: }
101:
102: 103: 104: 105: 106: 107:
108: protected function _asText($shellList)
109: {
110: foreach ($shellList as $plugin => $commands) {
111: sort($commands);
112: $this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
113: $this->out();
114: }
115:
116: $this->out('To run an app or core command, type <info>`cake shell_name [args]`</info>');
117: $this->out('To run a plugin command, type <info>`cake Plugin.shell_name [args]`</info>');
118: $this->out('To get help on a specific command, type <info>`cake shell_name --help`</info>', 2);
119: }
120:
121: 122: 123: 124: 125: 126:
127: protected function _asXml($shellList)
128: {
129: $plugins = Plugin::loaded();
130: $shells = new SimpleXMLElement('<shells></shells>');
131: foreach ($shellList as $plugin => $commands) {
132: foreach ($commands as $command) {
133: $callable = $command;
134: if (in_array($plugin, $plugins)) {
135: $callable = Inflector::camelize($plugin) . '.' . $command;
136: }
137:
138: $shell = $shells->addChild('shell');
139: $shell->addAttribute('name', $command);
140: $shell->addAttribute('call_as', $callable);
141: $shell->addAttribute('provider', $plugin);
142: $shell->addAttribute('help', $callable . ' -h');
143: }
144: }
145: $this->_io->setOutputAs(ConsoleOutput::RAW);
146: $this->out($shells->saveXML());
147: }
148:
149: 150: 151: 152: 153:
154: public function getOptionParser()
155: {
156: $parser = parent::getOptionParser();
157:
158: $parser->setDescription(
159: 'Get the list of available shells for this CakePHP application.'
160: )->addOption('xml', [
161: 'help' => 'Get the listing as XML.',
162: 'boolean' => true
163: ])->addOption('version', [
164: 'help' => 'Prints the currently installed version of CakePHP. (deprecated - use `cake --version` instead)',
165: 'boolean' => true
166: ]);
167:
168: return $parser;
169: }
170: }
171: