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