HelpController.php 9.25 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/
Alexander Makarov committed
6
 * @copyright Copyright &copy; 2008-2012 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 15
use yii\base\Application;
use yii\base\InlineAction;
use yii\console\Controller;

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

			list($controller, $action) = $result;

			if ($action === '') {
Qiang Xue committed
64
				$status = $this->getControllerHelp($controller);
Qiang Xue committed
65
			} else {
Qiang Xue committed
66
				$status = $this->getActionHelp($controller, $action);
Qiang Xue committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
			}
		}
		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)
	{
		$actions = array_keys($controller->actions);
		$class = new \ReflectionClass($controller);
		foreach ($class->getMethods() as $method) {
Qiang Xue committed
93
			/** @var $method \ReflectionMethod */
Qiang Xue committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 141 142 143
			$name = $method->getName();
			if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0) {
				$actions[] = lcfirst(substr($name, 6));
			}
		}
		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)
	{
		if ($module instanceof Application) {
			$prefix = '';
		} else {
			$prefix = $module->getUniqueId() . '/';
		}

		$commands = array();
		foreach (array_keys($module->controllers) as $id) {
			$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
144
	protected function getHelp()
Qiang Xue committed
145
	{
Qiang Xue committed
146 147 148 149 150 151 152 153 154 155 156 157
		$commands = $this->getCommands();
		if ($commands !== array()) {
			echo "\n    Usage: yiic <command-name> [...options...]\n\n";
			echo "The following commands are available:\n";
			foreach ($commands as $command) {
				echo " - $command\n";
			}
			echo "\nTo see individual command help, enter:\n";
			echo "\n    yiic help <command-name>\n";
		} else {
			echo "\nNo commands are found.\n";
		}
Qiang Xue committed
158
		return 0;
Qiang Xue committed
159
	}
Qiang Xue committed
160

Qiang Xue committed
161 162 163 164 165
	/**
	 * Displays the overall information of the command.
	 * @param Controller $controller the controller instance
	 * @return integer the exit status
	 */
Qiang Xue committed
166
	protected function getControllerHelp($controller)
Qiang Xue committed
167
	{
Qiang Xue committed
168 169 170 171 172 173 174
		$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 !== '') {
175
			echo "\n" . $comment . "\n";
Qiang Xue committed
176 177 178 179 180 181 182 183 184 185 186
		}

		$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";
				}
187
				echo "\n";
Qiang Xue committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
			}
		}

		$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) {
					echo " * $prefix/$action (default)\n";
				} else {
					echo " * $prefix/$action\n";
				}
			}
			echo "\n";
		}

		return 0;
Qiang Xue committed
207 208
	}

Alexander Makarov committed
209
	/**
Qiang Xue committed
210 211 212 213
	 * 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
214
	 */
Qiang Xue committed
215
	protected function getActionHelp($controller, $actionID)
Alexander Makarov committed
216
	{
Qiang Xue committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		$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
233
		}
Qiang Xue committed
234

Qiang Xue committed
235
		if ($comment !== '') {
236
			echo "\n" . $comment . "\n";
Qiang Xue committed
237
		}
Qiang Xue committed
238

Qiang Xue committed
239 240 241 242 243 244 245 246 247
		$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
248
			}
Qiang Xue committed
249
			echo "\n";
Qiang Xue committed
250
		}
Alexander Makarov committed
251

Qiang Xue committed
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 296 297 298
		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
299 300 301
			}
		}

Qiang Xue committed
302 303 304 305 306 307 308 309 310 311 312 313 314
		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
315
			if (!$property->isPublic() || $property->isStatic() || $property->getDeclaringClass()->getName() === 'yii\base\Controller') {
Qiang Xue committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
				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 = '';
					}
337
					$comment = $type === '' ? '' : ($type);
Qiang Xue committed
338
					if (trim($doc) !== '') {
339
						$comment .= ', ' . preg_replace("/^/m", "", $doc);
Qiang Xue committed
340 341 342 343 344 345 346 347 348 349 350
					}
					$options[$name] = $comment;
					break;
				}
			}
			if (!isset($options[$name])) {
				$options[$name] = '';
			}
		}
		ksort($options);
		return $options;
Alexander Makarov committed
351 352
	}
}