Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://cakephp.org CakePHP Project
12
 * @package       Cake.Console.Command
13
 * @since         CakePHP v 2.0
14
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
15
 */
16
 
17
App::uses('AppShell', 'Console/Command');
18
App::uses('Inflector', 'Utility');
19
 
20
/**
21
 * Shows a list of commands available from the console.
22
 *
23
 * @package       Cake.Console.Command
24
 */
25
class CommandListShell extends AppShell {
26
 
27
/**
28
 * startup
29
 *
30
 * @return void
31
 */
32
	public function startup() {
33
		if (empty($this->params['xml'])) {
34
			parent::startup();
35
		}
36
	}
37
 
38
/**
39
 * Main function Prints out the list of shells.
40
 *
41
 * @return void
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
 * Gets the shell command listing.
72
 *
73
 * @return array
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
 * Scan the provided paths for shells, and append them into $shellList
100
 *
101
 * @param string $type
102
 * @param array $shells
103
 * @param array $shellList
104
 * @return void
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
 * Output text.
114
 *
115
 * @param array $shellList
116
 * @return void
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
 * Output as XML
132
 *
133
 * @param array $shellList
134
 * @return void
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
 * get the option parser
159
 *
160
 * @return void
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
}