HelpController.php 9.29 KB
Newer Older
Alexander Makarov committed
1 2
<?php
/**
Qiang Xue committed
3
 * HelpController class file.
Alexander Makarov committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
Alexander Makarov committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

10
namespace yii\console\controllers;
Alexander Makarov committed
11

Qiang Xue committed
12 13 14
use yii\base\Application;
use yii\base\InlineAction;
use yii\console\Controller;
Qiang Xue committed
15
use yii\util\StringHelper;
Qiang Xue committed
16

Alexander Makarov committed
17
/**
Qiang Xue committed
18
 * This command provides help information about console commands.
Alexander Makarov committed
19
 *
Qiang Xue committed
20 21 22
 * This command displays the available command list in
 * the application or the detailed instructions about using
 * a specific command.
Alexander Makarov committed
23
 *
Qiang Xue committed
24
 * This command can be used as follows on command line:
Qiang Xue committed
25 26 27 28 29
 *
 * ~~~
 * yiic help [command name]
 * ~~~
 *
Qiang Xue committed
30 31
 * In the above, if the command name is not provided, all
 * available commands will be displayed.
Alexander Makarov committed
32 33 34 35
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
36
class HelpController extends Controller
Alexander Makarov committed
37
{
Qiang Xue committed
38 39 40 41 42 43 44 45 46 47
	/**
	 * Displays available commands or the detailed information
	 * about a particular command. For example,
	 *
	 * ~~~
	 * yiic help          # list available commands
	 * yiic help message  # display help info about "message"
	 * ~~~
	 *
	 * @param array $args additional anonymous command line arguments.
Qiang Xue committed
48
	 * You may provide a command name to display its detailed information.
Qiang Xue committed
49 50
	 * @return integer the exit status
	 */
Qiang Xue committed
51 52
	public function actionIndex($args = array())
	{
Qiang Xue committed
53
		if (empty($args)) {
Qiang Xue committed
54
			$status = $this->getHelp();
Qiang Xue committed
55 56 57
		} else {
			$result = \Yii::$application->createController($args[0]);
			if ($result === false) {
Qiang Xue committed
58
				echo "\nError: no help for unknown command \"{$args[0]}\".\n";
Qiang Xue committed
59 60 61
				return 1;
			}

Qiang Xue committed
62
			list($controller, $actionID) = $result;
Qiang Xue committed
63

Qiang Xue committed
64
			if ($actionID === '') {
Qiang Xue committed
65
				$status = $this->getControllerHelp($controller);
Qiang Xue committed
66
			} else {
Qiang Xue committed
67
				$status = $this->getActionHelp($controller, $actionID);
Qiang Xue committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
			}
		}
		return $status;
	}

	/**
	 * Returns all available command names.
	 * @return array all available command names
	 */
	public function getCommands()
	{
		$commands = $this->getModuleCommands(\Yii::$application);
		sort($commands);
		return array_unique($commands);
	}

	/**
	 * Returns all available actions of the specified controller.
	 * @param Controller $controller the controller instance
	 * @return array all available action IDs.
	 */
	public function getActions($controller)
	{
Qiang Xue committed
91
		$actions = array_keys($controller->actions());
Qiang Xue committed
92 93
		$class = new \ReflectionClass($controller);
		foreach ($class->getMethods() as $method) {
Qiang Xue committed
94
			/** @var $method \ReflectionMethod */
Qiang Xue committed
95
			$name = $method->getName();
Qiang Xue committed
96 97
			if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') {
				$actions[] = StringHelper::camel2id(substr($name, 6));
Qiang Xue committed
98 99 100 101 102 103 104 105 106 107 108 109 110
			}
		}
		sort($actions);
		return array_unique($actions);
	}

	/**
	 * Returns available commands of a specified module.
	 * @param \yii\base\Module $module the module instance
	 * @return array the available command names
	 */
	protected function getModuleCommands($module)
	{
Qiang Xue committed
111
		$prefix = $module instanceof Application ? '' : $module->getUniqueID() . '/';
Qiang Xue committed
112 113

		$commands = array();
Qiang Xue committed
114
		foreach (array_keys($module->controllerMap) as $id) {
Qiang Xue committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
			$commands[] = $prefix . $id;
		}

		foreach ($module->getModules() as $id => $child) {
			if (($child = $module->getModule($id)) === null) {
				continue;
			}
			foreach ($this->getModuleCommands($child) as $command) {
				$commands[] = $prefix . $id . '/' . $command;
			}
		}

		$files = scandir($module->getControllerPath());
		foreach ($files as $file) {
			if(strcmp(substr($file,-14),'Controller.php') === 0 && is_file($file)) {
				$commands[] = $prefix . lcfirst(substr(basename($file), 0, -14));
			}
		}

		return $commands;
	}

	/**
	 * Displays all available commands.
	 * @return integer the exit status
	 */
Qiang Xue committed
141
	protected function getHelp()
Qiang Xue committed
142
	{
Qiang Xue committed
143 144
		$commands = $this->getCommands();
		if ($commands !== array()) {
Qiang Xue committed
145 146
			echo "\nUsage: yiic <command-name> [...options...]\n\n";
			echo "The following commands are available:\n\n";
Qiang Xue committed
147
			foreach ($commands as $command) {
Qiang Xue committed
148
				echo " * $command\n";
Qiang Xue committed
149
			}
Qiang Xue committed
150
			echo "\nTo see the help of each command, enter:\n";
Qiang Xue committed
151 152 153 154
			echo "\n    yiic help <command-name>\n";
		} else {
			echo "\nNo commands are found.\n";
		}
Qiang Xue committed
155
		return 0;
Qiang Xue committed
156
	}
Qiang Xue committed
157

Qiang Xue committed
158 159 160 161 162
	/**
	 * Displays the overall information of the command.
	 * @param Controller $controller the controller instance
	 * @return integer the exit status
	 */
Qiang Xue committed
163
	protected function getControllerHelp($controller)
Qiang Xue committed
164
	{
Qiang Xue committed
165 166 167 168 169 170 171
		$class = new \ReflectionClass($controller);
		$comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($class->getDocComment(), '/'))), "\r", '');
		if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
			$comment = trim(substr($comment, 0, $matches[0][1]));
		}

		if ($comment !== '') {
172
			echo "\n" . $comment . "\n";
Qiang Xue committed
173 174 175 176 177 178 179 180 181 182 183
		}

		$options = $this->getGlobalOptions($class, $controller);
		if ($options !== array()) {
			echo "\nGLOBAL OPTIONS";
			echo "\n--------------\n\n";
			foreach ($options as $name => $description) {
				echo " --$name";
				if ($description != '') {
					echo ": $description\n";
				}
184
				echo "\n";
Qiang Xue committed
185 186 187 188 189 190 191 192 193 194
			}
		}

		$actions = $this->getActions($controller);
		if ($actions !== array()) {
			echo "\nSUB-COMMANDS";
			echo "\n------------\n\n";
			$prefix = $controller->getUniqueId();
			foreach ($actions as $action) {
				if ($controller->defaultAction === $action) {
Qiang Xue committed
195
					echo " * $prefix (default)\n";
Qiang Xue committed
196 197 198 199 200 201 202 203
				} else {
					echo " * $prefix/$action\n";
				}
			}
			echo "\n";
		}

		return 0;
Qiang Xue committed
204 205
	}

Alexander Makarov committed
206
	/**
Qiang Xue committed
207 208 209 210
	 * Displays the detailed information of a command action.
	 * @param Controller $controller the controller instance
	 * @param string $actionID action ID
	 * @return integer the exit status
Alexander Makarov committed
211
	 */
Qiang Xue committed
212
	protected function getActionHelp($controller, $actionID)
Alexander Makarov committed
213
	{
Qiang Xue committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		$action = $controller->createAction($actionID);
		if ($action === null) {
			echo "Unknown sub-command: " . $controller->getUniqueId() . "/$actionID\n";
			return 1;
		}
		if ($action instanceof InlineAction) {
			$method = new \ReflectionMethod($controller, 'action' . $action->id);
		} else {
			$method = new \ReflectionMethod($action, 'run');
		}
		$comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($method->getDocComment(), '/'))), "\r", '');
		if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
			$meta = substr($comment, $matches[0][1]);
			$comment = trim(substr($comment, 0, $matches[0][1]));
		} else {
			$meta = '';
Alexander Makarov committed
230
		}
Qiang Xue committed
231

Qiang Xue committed
232
		if ($comment !== '') {
233
			echo "\n" . $comment . "\n";
Qiang Xue committed
234
		}
Qiang Xue committed
235

Qiang Xue committed
236 237 238 239 240 241 242 243 244
		$options = $this->getOptions($method, $meta);
		if ($options !== array()) {
			echo "\nOPTIONS";
			echo "\n-------\n\n";
			foreach ($options as $name => $description) {
				echo " --$name";
				if ($description != '') {
					echo ": $description\n";
				}
Alexander Makarov committed
245
			}
Qiang Xue committed
246
			echo "\n";
Qiang Xue committed
247
		}
Alexander Makarov committed
248

Qiang Xue committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
		return 0;
	}

	/**
	 * @param \ReflectionMethod $method
	 * @param string $meta
	 * @return array
	 */
	protected function getOptions($method, $meta)
	{
		$params = $method->getParameters();
		$tags = preg_split('/^\s*@/m', $meta, -1, PREG_SPLIT_NO_EMPTY);
		$options = array();
		$count = 0;
		foreach ($tags as $tag) {
			$parts = preg_split('/\s+/', trim($tag), 2);
			if ($parts[0] === 'param' && isset($params[$count])) {
				$param = $params[$count];
				$comment = isset($parts[1]) ? $parts[1] : '';
				if (preg_match('/^([^\s]+)\s+(\$\w+\s+)?(.*)/s', $comment, $matches)) {
					$type = $matches[1];
					$doc = $matches[3];
				} else {
					$type = $comment;
					$doc = '';
				}
				$comment = $type === '' ? '' : ($type . ', ');
				if ($param->isDefaultValueAvailable()) {
					$value = $param->getDefaultValue();
					if (!is_array($value)) {
						$comment .= 'optional (defaults to ' . var_export($value, true) . ').';
					} else {
						$comment .= 'optional.';
					}
				} else {
					$comment .= 'required.';
				}
				if (trim($doc) !== '') {
					$comment .= "\n" . preg_replace("/^/m", "     ", $doc);
				}
				$options[$param->getName()] = $comment;
				$count++;
			}
		}
		if ($count < count($params)) {
			for ($i = $count; $i < count($params); ++$i) {
				$options[$params[$i]->getName()] = '';
Qiang Xue committed
296 297 298
			}
		}

Qiang Xue committed
299 300 301 302 303 304 305 306 307 308 309 310 311
		ksort($options);
		return $options;
	}

	/**
	 * @param \ReflectionClass $class
	 * @param Controller $controller
	 * @return array
	 */
	protected function getGlobalOptions($class, $controller)
	{
		$options = array();
		foreach ($class->getProperties() as $property) {
Qiang Xue committed
312
			if (!$property->isPublic() || $property->isStatic() || $property->getDeclaringClass()->getName() !== get_class($controller)) {
Qiang Xue committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
				continue;
			}
			$name = $property->getName();
			$comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($property->getDocComment(), '/'))), "\r", '');
			if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
				$meta = substr($comment, $matches[0][1]);
			} else {
				$meta = '';
			}
			$tags = preg_split('/^\s*@/m', $meta, -1, PREG_SPLIT_NO_EMPTY);
			foreach ($tags as $tag) {
				$parts = preg_split('/\s+/', trim($tag), 2);
				$comment = isset($parts[1]) ? $parts[1] : '';
				if ($parts[0] === 'var' || $parts[0] === 'property') {
					if (preg_match('/^([^\s]+)(\s+.*)?/s', $comment, $matches)) {
						$type = $matches[1];
						$doc = trim($matches[2]);
					} else {
						$type = $comment;
						$doc = '';
					}
334
					$comment = $type === '' ? '' : ($type);
Qiang Xue committed
335
					if (trim($doc) !== '') {
336
						$comment .= ', ' . preg_replace("/^/m", "", $doc);
Qiang Xue committed
337 338 339 340 341 342 343 344 345 346 347
					}
					$options[$name] = $comment;
					break;
				}
			}
			if (!isset($options[$name])) {
				$options[$name] = '';
			}
		}
		ksort($options);
		return $options;
Alexander Makarov committed
348 349
	}
}