HelpController.php 9.36 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

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

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

Qiang Xue committed
66
			list($controller, $actionID) = $result;
Qiang Xue committed
67

Qiang Xue committed
68
			if ($actionID === '') {
69
				$this->getControllerHelp($controller);
Qiang Xue committed
70
			} else {
71
				$this->getActionHelp($controller, $actionID);
Qiang Xue committed
72 73 74 75 76 77 78 79 80 81
			}
		}
	}

	/**
	 * Returns all available command names.
	 * @return array all available command names
	 */
	public function getCommands()
	{
82
		$commands = $this->getModuleCommands(Yii::$application);
Qiang Xue committed
83 84 85 86 87 88 89 90 91 92 93
		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
94
		$actions = array_keys($controller->actions());
Qiang Xue committed
95 96 97
		$class = new \ReflectionClass($controller);
		foreach ($class->getMethods() as $method) {
			$name = $method->getName();
Qiang Xue committed
98 99
			if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') {
				$actions[] = StringHelper::camel2id(substr($name, 6));
Qiang Xue committed
100 101 102 103 104 105 106 107 108 109 110 111 112
			}
		}
		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
113
		$prefix = $module instanceof Application ? '' : $module->getUniqueID() . '/';
Qiang Xue committed
114 115

		$commands = array();
Qiang Xue committed
116
		foreach (array_keys($module->controllerMap) as $id) {
Qiang Xue committed
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
			$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.
	 */
Qiang Xue committed
142
	protected function getHelp()
Qiang Xue committed
143
	{
Qiang Xue committed
144 145
		$commands = $this->getCommands();
		if ($commands !== array()) {
Qiang Xue committed
146 147
			echo "\nUsage: yiic <command-name> [...options...]\n\n";
			echo "The following commands are available:\n\n";
Qiang Xue committed
148
			foreach ($commands as $command) {
Qiang Xue committed
149
				echo " * $command\n";
Qiang Xue committed
150
			}
Qiang Xue committed
151
			echo "\nTo see the help of each command, enter:\n";
Qiang Xue committed
152 153 154 155 156
			echo "\n    yiic help <command-name>\n";
		} else {
			echo "\nNo commands are found.\n";
		}
	}
Qiang Xue committed
157

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

		$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";
				}
183
				echo "\n";
Qiang Xue committed
184 185 186 187 188 189 190 191 192 193
			}
		}

		$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
194
					echo " * $prefix (default)\n";
Qiang Xue committed
195 196 197 198 199 200
				} else {
					echo " * $prefix/$action\n";
				}
			}
			echo "\n";
		}
Qiang Xue committed
201 202
	}

Alexander Makarov committed
203
	/**
Qiang Xue committed
204 205 206
	 * Displays the detailed information of a command action.
	 * @param Controller $controller the controller instance
	 * @param string $actionID action ID
207
	 * @throws BadUsageException if the action does not exist
Alexander Makarov committed
208
	 */
Qiang Xue committed
209
	protected function getActionHelp($controller, $actionID)
Alexander Makarov committed
210
	{
Qiang Xue committed
211 212
		$action = $controller->createAction($actionID);
		if ($action === null) {
213 214 215
			throw new BadUsageException(Yii::t('yii', 'No help for unknown sub-command "{command}".', array(
				'{command}' => $controller->getUniqueId() . "/$actionID",
			)));
Qiang Xue committed
216 217 218 219 220 221 222 223 224 225 226 227
		}
		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
228
		}
Qiang Xue committed
229

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

Qiang Xue committed
234 235 236 237 238 239 240 241 242
		$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
243
			}
Qiang Xue committed
244
			echo "\n";
Qiang Xue committed
245
		}
Qiang Xue committed
246 247 248 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
	}

	/**
	 * @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
292 293 294
			}
		}

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