You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.9 KiB
152 lines
4.9 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
|
* Console Application class file.
|
||
|
*
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
13 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\console;
|
||
|
|
||
12 years ago
|
use yii\base\InvalidRouteException;
|
||
13 years ago
|
|
||
13 years ago
|
/**
|
||
13 years ago
|
* Application represents a console application.
|
||
13 years ago
|
*
|
||
13 years ago
|
* Application extends from [[yii\base\Application]] by providing functionalities that are
|
||
13 years ago
|
* specific to console requests. In particular, it deals with console requests
|
||
|
* through a command-based approach:
|
||
|
*
|
||
13 years ago
|
* - A console application consists of one or several possible user commands;
|
||
13 years ago
|
* - Each user command is implemented as a class extending [[\yii\console\Controller]];
|
||
13 years ago
|
* - User specifies which command to run on the command line;
|
||
|
* - The command processes the user request with the specified parameters.
|
||
|
*
|
||
13 years ago
|
* The command classes reside in the directory specified by [[controllerPath]].
|
||
13 years ago
|
* Their naming should follow the same naming convention as controllers. For example, the `help` command
|
||
13 years ago
|
* is implemented using the `HelpController` class.
|
||
13 years ago
|
*
|
||
|
* To run the console application, enter the following on the command line:
|
||
|
*
|
||
13 years ago
|
* ~~~
|
||
12 years ago
|
* yii <route> [--param1=value1 --param2 ...]
|
||
13 years ago
|
* ~~~
|
||
|
*
|
||
13 years ago
|
* where `<route>` refers to a controller route in the form of `ModuleID/ControllerID/ActionID`
|
||
13 years ago
|
* (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).
|
||
13 years ago
|
*
|
||
13 years ago
|
* A `help` command is provided by default, which lists available commands and shows their usage.
|
||
13 years ago
|
* To use this command, simply type:
|
||
13 years ago
|
*
|
||
|
* ~~~
|
||
12 years ago
|
* yii help
|
||
13 years ago
|
* ~~~
|
||
13 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Application extends \yii\base\Application
|
||
|
{
|
||
|
/**
|
||
13 years ago
|
* @var string the default route of this application. Defaults to 'help',
|
||
|
* meaning the `help` command.
|
||
13 years ago
|
*/
|
||
13 years ago
|
public $defaultRoute = 'help';
|
||
13 years ago
|
/**
|
||
13 years ago
|
* @var boolean whether to enable the commands provided by the core framework.
|
||
|
* Defaults to true.
|
||
13 years ago
|
*/
|
||
13 years ago
|
public $enableCoreCommands = true;
|
||
13 years ago
|
|
||
13 years ago
|
/**
|
||
|
* Initialize the application.
|
||
|
*/
|
||
13 years ago
|
public function init()
|
||
|
{
|
||
13 years ago
|
parent::init();
|
||
|
if ($this->enableCoreCommands) {
|
||
|
foreach ($this->coreCommands() as $id => $command) {
|
||
12 years ago
|
if (!isset($this->controllerMap[$id])) {
|
||
|
$this->controllerMap[$id] = $command;
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
// ensure we have the 'help' command so that we can list the available commands
|
||
12 years ago
|
if (!isset($this->controllerMap['help'])) {
|
||
|
$this->controllerMap['help'] = 'yii\console\controllers\HelpController';
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* 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)
|
||
12 years ago
|
* @throws Exception if the script is not running from the command line
|
||
13 years ago
|
*/
|
||
13 years ago
|
public function processRequest()
|
||
13 years ago
|
{
|
||
13 years ago
|
/** @var $request Request */
|
||
|
$request = $this->getRequest();
|
||
|
if ($request->getIsConsoleRequest()) {
|
||
12 years ago
|
list ($route, $params) = $request->resolve();
|
||
|
return $this->runAction($route, $params);
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new Exception(\Yii::t('yii', 'This script must be run from the command line.'));
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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.
|
||
12 years ago
|
* @throws Exception if the route is invalid
|
||
12 years ago
|
*/
|
||
|
public function runAction($route, $params = array())
|
||
|
{
|
||
|
try {
|
||
|
return parent::runAction($route, $params);
|
||
|
} catch (InvalidRouteException $e) {
|
||
12 years ago
|
throw new Exception(\Yii::t('yii', 'Unknown command "{command}".', array('{command}' => $route)));
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Returns the configuration of the built-in commands.
|
||
|
* @return array the configuration of the built-in commands.
|
||
|
*/
|
||
13 years ago
|
public function coreCommands()
|
||
13 years ago
|
{
|
||
13 years ago
|
return array(
|
||
13 years ago
|
'message' => 'yii\console\controllers\MessageController',
|
||
|
'help' => 'yii\console\controllers\HelpController',
|
||
|
'migrate' => 'yii\console\controllers\MigrateController',
|
||
12 years ago
|
'cache' => 'yii\console\controllers\CacheController',
|
||
12 years ago
|
'asset' => 'yii\console\controllers\AssetController',
|
||
13 years ago
|
);
|
||
13 years ago
|
}
|
||
12 years ago
|
|
||
|
/**
|
||
|
* 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',
|
||
|
),
|
||
|
));
|
||
|
}
|
||
13 years ago
|
}
|