Application.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Console Application class file.
  4. *
  5. * @link http://www.yiiframework.com/
  6. * @copyright Copyright (c) 2008 Yii Software LLC
  7. * @license http://www.yiiframework.com/license/
  8. */
  9. namespace yii\console;
  10. use Yii;
  11. use yii\base\InvalidRouteException;
  12. /**
  13. * Application represents a console application.
  14. *
  15. * Application extends from [[yii\base\Application]] by providing functionalities that are
  16. * specific to console requests. In particular, it deals with console requests
  17. * through a command-based approach:
  18. *
  19. * - A console application consists of one or several possible user commands;
  20. * - Each user command is implemented as a class extending [[\yii\console\Controller]];
  21. * - User specifies which command to run on the command line;
  22. * - The command processes the user request with the specified parameters.
  23. *
  24. * The command classes reside in the directory specified by [[controllerPath]].
  25. * Their naming should follow the same naming convention as controllers. For example, the `help` command
  26. * is implemented using the `HelpController` class.
  27. *
  28. * To run the console application, enter the following on the command line:
  29. *
  30. * ~~~
  31. * yii <route> [--param1=value1 --param2 ...]
  32. * ~~~
  33. *
  34. * where `<route>` refers to a controller route in the form of `ModuleID/ControllerID/ActionID`
  35. * (e.g. `sitemap/create`), and `param1`, `param2` refers to a set of named parameters that
  36. * will be used to initialize the controller action (e.g. `--since=0` specifies a `since` parameter
  37. * whose value is 0 and a corresponding `$since` parameter is passed to the action method).
  38. *
  39. * A `help` command is provided by default, which lists available commands and shows their usage.
  40. * To use this command, simply type:
  41. *
  42. * ~~~
  43. * yii help
  44. * ~~~
  45. *
  46. * @property Response $response The response component. This property is read-only.
  47. *
  48. * @author Qiang Xue <[email protected]>
  49. * @since 2.0
  50. */
  51. class Application extends \yii\base\Application
  52. {
  53. /**
  54. * @var string the default route of this application. Defaults to 'help',
  55. * meaning the `help` command.
  56. */
  57. public $defaultRoute = 'help';
  58. /**
  59. * @var boolean whether to enable the commands provided by the core framework.
  60. * Defaults to true.
  61. */
  62. public $enableCoreCommands = true;
  63. /**
  64. * @var Controller the currently active controller instance
  65. */
  66. public $controller;
  67. /**
  68. * Initialize the application.
  69. */
  70. public function init()
  71. {
  72. parent::init();
  73. if ($this->enableCoreCommands) {
  74. foreach ($this->coreCommands() as $id => $command) {
  75. if (!isset($this->controllerMap[$id])) {
  76. $this->controllerMap[$id] = $command;
  77. }
  78. }
  79. }
  80. // ensure we have the 'help' command so that we can list the available commands
  81. if (!isset($this->controllerMap['help'])) {
  82. $this->controllerMap['help'] = 'yii\console\controllers\HelpController';
  83. }
  84. }
  85. /**
  86. * Handles the specified request.
  87. * @param Request $request the request to be handled
  88. * @return Response the resulting response
  89. */
  90. public function handleRequest($request)
  91. {
  92. list ($route, $params) = $request->resolve();
  93. $this->requestedRoute = $route;
  94. $result = $this->runAction($route, $params);
  95. if ($result instanceof Response) {
  96. return $result;
  97. } else {
  98. $response = $this->getResponse();
  99. $response->exitStatus = (int)$result;
  100. return $response;
  101. }
  102. }
  103. /**
  104. * Returns the response component.
  105. * @return Response the response component
  106. */
  107. public function getResponse()
  108. {
  109. return $this->getComponent('response');
  110. }
  111. /**
  112. * Runs a controller action specified by a route.
  113. * This method parses the specified route and creates the corresponding child module(s), controller and action
  114. * instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
  115. * If the route is empty, the method will use [[defaultRoute]].
  116. * @param string $route the route that specifies the action.
  117. * @param array $params the parameters to be passed to the action
  118. * @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
  119. * @throws Exception if the route is invalid
  120. */
  121. public function runAction($route, $params = [])
  122. {
  123. try {
  124. return parent::runAction($route, $params);
  125. } catch (InvalidRouteException $e) {
  126. throw new Exception(Yii::t('yii', 'Unknown command "{command}".', ['command' => $route]), 0, $e);
  127. }
  128. }
  129. /**
  130. * Returns the configuration of the built-in commands.
  131. * @return array the configuration of the built-in commands.
  132. */
  133. public function coreCommands()
  134. {
  135. return [
  136. 'message' => 'yii\console\controllers\MessageController',
  137. 'help' => 'yii\console\controllers\HelpController',
  138. 'migrate' => 'yii\console\controllers\MigrateController',
  139. 'cache' => 'yii\console\controllers\CacheController',
  140. 'asset' => 'yii\console\controllers\AssetController',
  141. 'fixture' => 'yii\console\controllers\FixtureController',
  142. ];
  143. }
  144. /**
  145. * Registers the core application components.
  146. * @see setComponents
  147. */
  148. public function registerCoreComponents()
  149. {
  150. parent::registerCoreComponents();
  151. $this->setComponents([
  152. 'request' => ['class' => 'yii\console\Request'],
  153. 'response' => ['class' => 'yii\console\Response'],
  154. ]);
  155. }
  156. }