Application.php 4.81 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 &copy; 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
 * ~~~
Qiang Xue committed
33
 * yiic <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
 *
 * ~~~
Qiang Xue committed
45
 * yiic help
Qiang Xue committed
46
 * ~~~
Alexander Makarov committed
47 48 49 50 51 52 53
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Application extends \yii\base\Application
{
	/**
Qiang Xue committed
54 55
	 * @var string the default route of this application. Defaults to 'help',
	 * meaning the `help` command.
Alexander Makarov committed
56
	 */
Qiang Xue committed
57
	public $defaultRoute = 'help';
Alexander Makarov committed
58
	/**
Qiang Xue committed
59 60
	 * @var boolean whether to enable the commands provided by the core framework.
	 * Defaults to true.
Alexander Makarov committed
61
	 */
Qiang Xue committed
62
	public $enableCoreCommands = true;
Alexander Makarov committed
63

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

	/**
Qiang Xue committed
84 85 86
	 * 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)
Qiang Xue committed
87
	 * @throws Exception if the script is not running from the command line
Alexander Makarov committed
88
	 */
Qiang Xue committed
89
	public function processRequest()
Alexander Makarov committed
90
	{
Qiang Xue committed
91 92 93
		/** @var $request Request */
		$request = $this->getRequest();
		if ($request->getIsConsoleRequest()) {
Qiang Xue committed
94
			return $this->runAction($request->route, $request->params);
Qiang Xue committed
95
		} else {
96
			throw new Exception(\Yii::t('yii|this script must be run from the command line.'));
Qiang Xue committed
97 98 99 100 101 102 103 104 105 106 107
		}
	}

	/**
	 * 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.
Qiang Xue committed
108
	 * @throws Exception if the route is invalid
Qiang Xue committed
109 110 111 112 113 114
	 */
	public function runAction($route, $params = array())
	{
		try {
			return parent::runAction($route, $params);
		} catch (InvalidRouteException $e) {
115
			throw new Exception(\Yii::t('yii|Unknown command "{command}".', array('{command}' => $route)));
Qiang Xue committed
116
		}
Qiang Xue committed
117 118
	}

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

	/**
	 * 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
149
}