Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

195 lines
6.6KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\console;
  8. use Yii;
  9. use yii\base\InvalidRouteException;
  10. /**
  11. * Application represents a console application.
  12. *
  13. * Application extends from [[\yii\base\Application]] by providing functionalities that are
  14. * specific to console requests. In particular, it deals with console requests
  15. * through a command-based approach:
  16. *
  17. * - A console application consists of one or several possible user commands;
  18. * - Each user command is implemented as a class extending [[\yii\console\Controller]];
  19. * - User specifies which command to run on the command line;
  20. * - The command processes the user request with the specified parameters.
  21. *
  22. * The command classes should be under the namespace specified by [[controllerNamespace]].
  23. * Their naming should follow the same naming convention as controllers. For example, the `help` command
  24. * is implemented using the `HelpController` class.
  25. *
  26. * To run the console application, enter the following on the command line:
  27. *
  28. * ~~~
  29. * yii <route> [--param1=value1 --param2 ...]
  30. * ~~~
  31. *
  32. * where `<route>` refers to a controller route in the form of `ModuleID/ControllerID/ActionID`
  33. * (e.g. `sitemap/create`), and `param1`, `param2` refers to a set of named parameters that
  34. * will be used to initialize the controller action (e.g. `--since=0` specifies a `since` parameter
  35. * whose value is 0 and a corresponding `$since` parameter is passed to the action method).
  36. *
  37. * A `help` command is provided by default, which lists available commands and shows their usage.
  38. * To use this command, simply type:
  39. *
  40. * ~~~
  41. * yii help
  42. * ~~~
  43. *
  44. * @author Qiang Xue <qiang.xue@gmail.com>
  45. * @since 2.0
  46. */
  47. class Application extends \yii\base\Application
  48. {
  49. /**
  50. * The option name for specifying the application configuration file path.
  51. */
  52. const OPTION_APPCONFIG = 'appconfig';
  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. * @inheritdoc
  69. */
  70. public function __construct($config = [])
  71. {
  72. $config = $this->loadConfig($config);
  73. parent::__construct($config);
  74. }
  75. /**
  76. * Loads the configuration.
  77. * This method will check if the command line option [[OPTION_APPCONFIG]] is specified.
  78. * If so, the corresponding file will be loaded as the application configuration.
  79. * Otherwise, the configuration provided as the parameter will be returned back.
  80. * @param array $config the configuration provided in the constructor.
  81. * @return array the actual configuration to be used by the application.
  82. */
  83. protected function loadConfig($config)
  84. {
  85. if (!empty($_SERVER['argv'])) {
  86. $option = '--' . self::OPTION_APPCONFIG . '=';
  87. foreach ($_SERVER['argv'] as $param) {
  88. if (strpos($param, $option) !== false) {
  89. $path = substr($param, strlen($option));
  90. if (!empty($path) && is_file($file = Yii::getAlias($path))) {
  91. return require($file);
  92. } else {
  93. die("The configuration file does not exist: $path\n");
  94. }
  95. }
  96. }
  97. }
  98. return $config;
  99. }
  100. /**
  101. * Initialize the application.
  102. */
  103. public function init()
  104. {
  105. parent::init();
  106. if ($this->enableCoreCommands) {
  107. foreach ($this->coreCommands() as $id => $command) {
  108. if (!isset($this->controllerMap[$id])) {
  109. $this->controllerMap[$id] = $command;
  110. }
  111. }
  112. }
  113. // ensure we have the 'help' command so that we can list the available commands
  114. if (!isset($this->controllerMap['help'])) {
  115. $this->controllerMap['help'] = 'yii\console\controllers\HelpController';
  116. }
  117. }
  118. /**
  119. * Handles the specified request.
  120. * @param Request $request the request to be handled
  121. * @return Response the resulting response
  122. */
  123. public function handleRequest($request)
  124. {
  125. list ($route, $params) = $request->resolve();
  126. $this->requestedRoute = $route;
  127. $result = $this->runAction($route, $params);
  128. if ($result instanceof Response) {
  129. return $result;
  130. } else {
  131. $response = $this->getResponse();
  132. $response->exitStatus = $result;
  133. return $response;
  134. }
  135. }
  136. /**
  137. * Runs a controller action specified by a route.
  138. * This method parses the specified route and creates the corresponding child module(s), controller and action
  139. * instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
  140. * If the route is empty, the method will use [[defaultRoute]].
  141. * @param string $route the route that specifies the action.
  142. * @param array $params the parameters to be passed to the action
  143. * @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
  144. * @throws Exception if the route is invalid
  145. */
  146. public function runAction($route, $params = [])
  147. {
  148. try {
  149. return (int)parent::runAction($route, $params);
  150. } catch (InvalidRouteException $e) {
  151. throw new Exception("Unknown command \"$route\".", 0, $e);
  152. }
  153. }
  154. /**
  155. * Returns the configuration of the built-in commands.
  156. * @return array the configuration of the built-in commands.
  157. */
  158. public function coreCommands()
  159. {
  160. return [
  161. 'message' => 'yii\console\controllers\MessageController',
  162. 'help' => 'yii\console\controllers\HelpController',
  163. 'migrate' => 'yii\console\controllers\MigrateController',
  164. 'cache' => 'yii\console\controllers\CacheController',
  165. 'asset' => 'yii\console\controllers\AssetController',
  166. 'fixture' => 'yii\console\controllers\FixtureController',
  167. ];
  168. }
  169. /**
  170. * @inheritdoc
  171. */
  172. public function coreComponents()
  173. {
  174. return array_merge(parent::coreComponents(), [
  175. 'request' => ['class' => 'yii\console\Request'],
  176. 'response' => ['class' => 'yii\console\Response'],
  177. 'errorHandler' => ['class' => 'yii\console\ErrorHandler'],
  178. ]);
  179. }
  180. }