|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
-
-
- namespace yii\console;
-
- use Yii;
- use yii\base\InvalidRouteException;
-
-
-
- defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
- defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w'));
- defined('STDERR') or define('STDERR', fopen('php://stderr', 'w'));
-
-
- class Application extends \yii\base\Application
- {
-
-
- const OPTION_APPCONFIG = 'appconfig';
-
-
-
- public $defaultRoute = 'help';
-
-
- public $enableCoreCommands = true;
-
-
- public $controller;
-
-
-
-
- public function __construct($config = [])
- {
- $config = $this->loadConfig($config);
- parent::__construct($config);
- }
-
-
-
- protected function loadConfig($config)
- {
- if (!empty($_SERVER['argv'])) {
- $option = '--' . self::OPTION_APPCONFIG . '=';
- foreach ($_SERVER['argv'] as $param) {
- if (strpos($param, $option) !== false) {
- $path = substr($param, strlen($option));
- if (!empty($path) && is_file($file = Yii::getAlias($path))) {
- return require($file);
- } else {
- exit("The configuration file does not exist: $path\n");
- }
- }
- }
- }
-
- return $config;
- }
-
-
-
- public function init()
- {
- parent::init();
- if ($this->enableCoreCommands) {
- foreach ($this->coreCommands() as $id => $command) {
- if (!isset($this->controllerMap[$id])) {
- $this->controllerMap[$id] = $command;
- }
- }
- }
-
- if (!isset($this->controllerMap['help'])) {
- $this->controllerMap['help'] = 'yii\console\controllers\HelpController';
- }
- }
-
-
-
- public function handleRequest($request)
- {
- list ($route, $params) = $request->resolve();
- $this->requestedRoute = $route;
- $result = $this->runAction($route, $params);
- if ($result instanceof Response) {
- return $result;
- } else {
- $response = $this->getResponse();
- $response->exitStatus = $result;
-
- return $response;
- }
- }
-
-
-
- public function runAction($route, $params = [])
- {
- try {
- $res = parent::runAction($route, $params);
- return is_object($res) ? $res : (int)$res;
- } catch (InvalidRouteException $e) {
- throw new Exception("Unknown command \"$route\".", 0, $e);
- }
- }
-
-
-
- public function coreCommands()
- {
- return [
- 'asset' => 'yii\console\controllers\AssetController',
- 'cache' => 'yii\console\controllers\CacheController',
- 'fixture' => 'yii\console\controllers\FixtureController',
- 'help' => 'yii\console\controllers\HelpController',
- 'message' => 'yii\console\controllers\MessageController',
- 'migrate' => 'yii\console\controllers\MigrateController',
- 'serve' => 'yii\console\controllers\ServeController',
- ];
- }
-
-
-
- public function getErrorHandler()
- {
- return $this->get('errorHandler');
- }
-
-
-
- public function getRequest()
- {
- return $this->get('request');
- }
-
-
-
- public function getResponse()
- {
- return $this->get('response');
- }
-
-
-
- public function coreComponents()
- {
- return array_merge(parent::coreComponents(), [
- 'request' => ['class' => 'yii\console\Request'],
- 'response' => ['class' => 'yii\console\Response'],
- 'errorHandler' => ['class' => 'yii\console\ErrorHandler'],
- ]);
- }
- }
|