Application.php 5.09 KB
Newer Older
Alexander Makarov committed
1 2 3 4 5
<?php
/**
 * Console Application class file.
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright (c) 2008 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\InvalidRouteException;
Qiang Xue committed
13

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

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

89 90 91 92 93
	/**
	 * Handles the specified request.
	 * @param Request $request the request to be handled
	 * @return Response the resulting response
	 */
94
	public function handleRequest($request)
95 96
	{
		list ($route, $params) = $request->resolve();
Qiang Xue committed
97
		$this->requestedRoute = $route;
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
		$result = $this->runAction($route, $params);
		if ($result instanceof Response) {
			return $result;
		} else {
			$response = $this->getResponse();
			$response->exitStatus = (int)$result;
			return $response;
		}
	}

	/**
	 * Returns the response component.
	 * @return Response the response component
	 */
	public function getResponse()
	{
		return $this->getComponent('response');
	}

	/**
	 * Runs a controller action specified by a route.
	 * This method parses the specified route and creates the corresponding child module(s), controller and action
	 * instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
	 * If the route is empty, the method will use [[defaultRoute]].
	 * @param string $route the route that specifies the action.
	 * @param array $params the parameters to be passed to the action
	 * @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
	 * @throws Exception if the route is invalid
	 */
	public function runAction($route, $params = array())
	{
Qiang Xue committed
129
		try {
130
			return parent::runAction($route, $params);
Qiang Xue committed
131
		} catch (InvalidRouteException $e) {
132
			throw new Exception(\Yii::t('yii', 'Unknown command "{command}".', array('{command}' => $route)), 0, $e);
Qiang Xue committed
133
		}
Qiang Xue committed
134 135
	}

136 137 138 139
	/**
	 * Returns the configuration of the built-in commands.
	 * @return array the configuration of the built-in commands.
	 */
Qiang Xue committed
140
	public function coreCommands()
Alexander Makarov committed
141
	{
Qiang Xue committed
142
		return array(
143 144 145
			'message' => 'yii\console\controllers\MessageController',
			'help' => 'yii\console\controllers\HelpController',
			'migrate' => 'yii\console\controllers\MigrateController',
146
			'cache' => 'yii\console\controllers\CacheController',
147
			'asset' => 'yii\console\controllers\AssetController',
Qiang Xue committed
148
		);
Alexander Makarov committed
149
	}
Qiang Xue committed
150 151 152 153 154 155 156 157 158 159 160 161

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