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

namespace yii\console;

Qiang Xue committed
12
use yii\base\Action;
Qiang Xue committed
13 14
use yii\base\Exception;

Alexander Makarov committed
15
/**
Qiang Xue committed
16
 * Controller is the base class of console command classes.
Alexander Makarov committed
17
 *
Qiang Xue committed
18 19 20
 * A controller consists of one or several actions known as sub-commands.
 * Users call a console command by specifying the corresponding route which identifies a controller action.
 * The `yiic` program is used when calling a console command, like the following:
Alexander Makarov committed
21
 *
Qiang Xue committed
22
 * ~~~
Qiang Xue committed
23
 * yiic <route> [--param1=value1 --param2 ...]
Qiang Xue committed
24
 * ~~~
Alexander Makarov committed
25 26
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
27
 *
Alexander Makarov committed
28 29
 * @since 2.0
 */
Qiang Xue committed
30
class Controller extends \yii\base\Controller
Alexander Makarov committed
31
{
Qiang Xue committed
32 33 34 35 36 37 38 39
	/**
	 * This method is invoked when the request parameters do not satisfy the requirement of the specified action.
	 * The default implementation will throw an exception.
	 * @param Action $action the action being executed
	 * @param Exception $exception the exception about the invalid parameters
	 */
	public function invalidActionParams($action, $exception)
	{
Qiang Xue committed
40 41 42
		echo \Yii::t('yii', 'Error: {message}', array(
			'{message}' => $exception->getMessage(),
		));
Qiang Xue committed
43 44 45
		\Yii::$application->end(1);
	}

Qiang Xue committed
46
	/**
Qiang Xue committed
47
	 * This method is invoked when extra parameters are provided to an action while it is executed.
Qiang Xue committed
48 49 50 51 52 53
	 * @param Action $action the action being executed
	 * @param array $expected the expected action parameters (name => value)
	 * @param array $actual the actual action parameters (name => value)
	 */
	public function extraActionParams($action, $expected, $actual)
	{
Qiang Xue committed
54
		unset($expected['args'], $actual['args']);
55

Qiang Xue committed
56 57
		$keys = array_diff(array_keys($actual), array_keys($expected));
		if (!empty($keys)) {
Qiang Xue committed
58
			echo \Yii::t('yii', 'Error: Unknown parameter(s): {params}', array(
Qiang Xue committed
59
				'{params}' => implode(', ', $keys),
Qiang Xue committed
60 61
			)) . "\n";
			\Yii::$application->end(1);
Alexander Makarov committed
62 63
		}
	}
Alexander Makarov committed
64 65 66 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

	/**
	 * Reads input via the readline PHP extension if that's available, or fgets() if readline is not installed.
	 *
	 * @param string $message to echo out before waiting for user input
	 * @param string $default the default string to be returned when user does not write anything.
	 * Defaults to null, means that default string is disabled.
	 * @return mixed line read as a string, or false if input has been closed
	 */
	public function prompt($message, $default = null)
	{
		if($default !== null) {
			$message .= " [$default] ";
		}
		else {
			$message .= ' ';
		}

		if(extension_loaded('readline')) {
			$input = readline($message);
			if($input !== false) {
				readline_add_history($input);
			}
		}
		else {
			echo $message;
			$input = fgets(STDIN);
		}

		if($input === false) {
			return false;
		}
		else {
			$input = trim($input);
			return ($input === '' && $default !== null) ? $default : $input;
		}
	}

	/**
	 * Asks user to confirm by typing y or n.
	 *
	 * @param string $message to echo out before waiting for user input
	 * @param boolean $default this value is returned if no selection is made.
	 * @return boolean whether user confirmed
	 */
	public function confirm($message, $default = false)
	{
		echo $message . ' (yes|no) [' . ($default ? 'yes' : 'no') . ']:';

		$input = trim(fgets(STDIN));
		return empty($input) ? $default : !strncasecmp($input, 'y', 1);
	}
Alexander Makarov committed
116
}