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

namespace yii\console;

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

Alexander Makarov committed
15
/**
Qiang Xue committed
16
 * Application represents a console application.
Alexander Makarov committed
17
 *
Qiang Xue committed
18
 * Application extends from [[yii\base\Application]] by providing functionalities that are
Alexander Makarov committed
19 20 21
 * specific to console requests. In particular, it deals with console requests
 * through a command-based approach:
 *
Qiang Xue committed
22
 * - A console application consists of one or several possible user commands;
Qiang Xue committed
23
 * - Each user command is implemented as a class extending [[\yii\console\Controller]];
Qiang Xue committed
24 25 26
 * - User specifies which command to run on the command line;
 * - The command processes the user request with the specified parameters.
 *
Qiang Xue committed
27
 * The command classes reside in the directory specified by [[controllerPath]].
Qiang Xue committed
28
 * Their naming should follow the same naming convention as controllers. For example, the `help` command
Qiang Xue committed
29
 * is implemented using the `HelpController` class.
Alexander Makarov committed
30 31 32
 *
 * To run the console application, enter the following on the command line:
 *
Qiang Xue committed
33
 * ~~~
Qiang Xue committed
34
 * yiic <route> [--param1=value1 --param2 ...]
Qiang Xue committed
35 36
 * ~~~
 *
Qiang Xue committed
37
 * where `<route>` refers to a controller route in the form of `ModuleID/ControllerID/ActionID`
Qiang Xue committed
38 39 40
 * (e.g. `sitemap/create`), and `param1`, `param2` refers to a set of named parameters that
 * will be used to initialize the controller action (e.g. `--since=0` specifies a `since` parameter
 * whose value is 0 and a corresponding `$since` parameter is passed to the action method).
Qiang Xue committed
41
 *
Qiang Xue committed
42
 * A `help` command is provided by default, which lists available commands and shows their usage.
Qiang Xue committed
43
 * To use this command, simply type:
Qiang Xue committed
44 45
 *
 * ~~~
Qiang Xue committed
46
 * yiic help
Qiang Xue committed
47
 * ~~~
Alexander Makarov committed
48 49 50 51 52 53 54
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Application extends \yii\base\Application
{
	/**
Qiang Xue committed
55 56
	 * @var string the default route of this application. Defaults to 'help',
	 * meaning the `help` command.
Alexander Makarov committed
57
	 */
Qiang Xue committed
58
	public $defaultRoute = 'help';
Alexander Makarov committed
59
	/**
Qiang Xue committed
60 61
	 * @var boolean whether to enable the commands provided by the core framework.
	 * Defaults to true.
Alexander Makarov committed
62
	 */
Qiang Xue committed
63
	public $enableCoreCommands = true;
Alexander Makarov committed
64

65 66 67
	/**
	 * Initialize the application.
	 */
Alexander Makarov committed
68 69
	public function init()
	{
Qiang Xue committed
70 71 72 73 74 75 76 77 78
		parent::init();
		if ($this->enableCoreCommands) {
			foreach ($this->coreCommands() as $id => $command) {
				if (!isset($this->controllers[$id])) {
					$this->controllers[$id] = $command;
				}
			}
		}
		// ensure we have the 'help' command so that we can list the available commands
Qiang Xue committed
79
		if (!isset($this->controllers['help'])) {
80
			$this->controllers['help'] = 'yii\console\controllers\HelpController';
Alexander Makarov committed
81 82 83 84
		}
	}

	/**
Qiang Xue committed
85 86 87 88
	 * Processes the request.
	 * The request is represented in terms of a controller route and action parameters.
	 * @return integer the exit status of the controller action (0 means normal, non-zero values mean abnormal)
	 * @throws Exception if the route cannot be resolved into a controller
Alexander Makarov committed
89
	 */
Qiang Xue committed
90
	public function processRequest()
Alexander Makarov committed
91
	{
Qiang Xue committed
92 93 94 95 96
		/** @var $request Request */
		$request = $this->getRequest();
		if ($request->getIsConsoleRequest()) {
			return $this->runController($request->route, $request->params);
		} else {
Qiang Xue committed
97 98
			die('This script must be run from the command line.');
		}
Qiang Xue committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	}

	/**
	 * Runs a controller with the given route and parameters.
	 * @param string $route the route (e.g. `post/create`)
	 * @param array $params the parameters to be passed to the controller action
	 * @return integer the exit status (0 means normal, non-zero values mean abnormal)
	 * @throws Exception if the route cannot be resolved into a controller
	 */
	public function runController($route, $params = array())
	{
		$result = $this->createController($route);
		if ($result === false) {
			throw new Exception(\Yii::t('yii', 'Unable to resolve the request.'));
		}
114
		/** @var $controller \yii\console\Controller */
Qiang Xue committed
115 116 117 118 119 120
		list($controller, $action) = $result;
		$priorController = $this->controller;
		$this->controller = $controller;
		$params = ReflectionHelper::initObjectWithParams($controller, $params);
		$status = $controller->run($action, $params);
		$this->controller = $priorController;
Qiang Xue committed
121
		return $status;
Alexander Makarov committed
122 123
	}

124 125 126 127
	/**
	 * Returns the configuration of the built-in commands.
	 * @return array the configuration of the built-in commands.
	 */
Qiang Xue committed
128
	public function coreCommands()
Alexander Makarov committed
129
	{
Qiang Xue committed
130
		return array(
131 132 133 134
			'message' => 'yii\console\controllers\MessageController',
			'help' => 'yii\console\controllers\HelpController',
			'migrate' => 'yii\console\controllers\MigrateController',
			'shell' => 'yii\console\controllers\ShellController',
Alexander Makarov committed
135
			'create' => 'yii\console\controllers\CreateController',
Qiang Xue committed
136
		);
Alexander Makarov committed
137
	}
Qiang Xue committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

	/**
	 * Registers the core application components.
	 * @see setComponents
	 */
	public function registerCoreComponents()
	{
		parent::registerCoreComponents();
		$this->setComponents(array(
			'request' => array(
				'class' => 'yii\console\Request',
			),
			'response' => array(
				'class' => 'yii\console\Response',
			),
		));
	}
Alexander Makarov committed
155
}