Application extends from Application by providing functionalities that are specific to console requests. In particular, it deals with console requests through a command-based approach: - A console application consists of one or several possible user commands; - Each user command is implemented as a class extending Controller; - User specifies which command to run on the command line; - The command processes the user request with the specified parameters. The command classes should be under the namespace specified by [[controllerNamespace]]. Their naming should follow the same naming convention as controllers. For example, the help command is implemented using the HelpController class. To run the console application, enter the following on the command line: ~~~ yii [--param1=value1 --param2 ...] ~~~ where refers to a controller route in the form of ModuleID/ControllerID/ActionID (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). A help command is provided by default, which lists available commands and shows their usage. To use this command, simply type: ~~~ yii help ~~~
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Application
示例#1
0
 /**
  * Suggest alternative commands for [[$command]] based on string similarity.
  *
  * Alternatives are searched using the following steps:
  *
  * - suggest alternatives that begin with `$command`
  * - find typos by calculating the Levenshtein distance between the unknown command and all
  *   available commands. The Levenshtein distance is defined as the minimal number of
  *   characters you have to replace, insert or delete to transform str1 into str2.
  *
  * @see http://php.net/manual/en/function.levenshtein.php
  * @return array a list of suggested alternatives sorted by similarity.
  */
 public function getSuggestedAlternatives()
 {
     $help = $this->application->createController('help');
     if ($help === false) {
         return [];
     }
     /** @var $helpController HelpController */
     list($helpController, $actionID) = $help;
     $availableActions = [];
     $commands = $helpController->getCommands();
     foreach ($commands as $command) {
         $result = $this->application->createController($command);
         if ($result === false) {
             continue;
         }
         // add the command itself (default action)
         $availableActions[] = $command;
         // add all actions of this controller
         /** @var $controller Controller */
         list($controller, $actionID) = $result;
         $actions = $helpController->getActions($controller);
         if (!empty($actions)) {
             $prefix = $controller->getUniqueId();
             foreach ($actions as $action) {
                 $availableActions[] = $prefix . '/' . $action;
             }
         }
     }
     return $this->filterBySimilarity($availableActions, $this->command);
 }
示例#2
0
 /**
  * Bootstraps the module for the console application.
  *
  * @param \yii\console\Application $app application instance.
  */
 protected function bootstrapConsoleApplication($app)
 {
     /** @var Module $module */
     $module = $app->getModule(Module::MODULE_ID);
     $module->controllerNamespace = 'nord\\yii\\account\\commands';
     $module->defaultController = Module::COMMAND_ACCOUNT;
     $app->controllerMap[$module->id] = ['class' => AccountCommand::className(), 'module' => $module];
 }
示例#3
0
 public function runPharCommand()
 {
     $configuration = (require \Yii::getAlias('@tests/unit/_config.php'));
     $configuration['modules']['phar'] = $this->moduleConfiguration;
     $application = new Application($configuration);
     $application->requestedRoute = 'phar/build';
     $application->runAction('phar/build', []);
 }
示例#4
0
 /**
  * Bootstrap method to be called during application bootstrap stage.
  *
  * @param Application $app the application currently running
  */
 public function bootstrap($app)
 {
     if ($app instanceof Application) {
         $app->controllerMap['audit'] = 'bedezign\\yii2\\audit\\commands\\AuditController';
     }
     if ($app->has('i18n')) {
         $app->i18n->translations['audit'] = ['class' => 'yii\\i18n\\PhpMessageSource', 'sourceLanguage' => 'en', 'basePath' => '@bedezign/yii2/audit/messages'];
     }
 }
 public static function responseAfterPrepare($event)
 {
     if (get_class(Yii::$app) == Application::className()) {
         return true;
     }
     $event->sender->content = ViewAccess::denyLinks($event->sender->content);
 }
示例#6
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     $this->_modulesConfigDependency = new ExpressionDependency(['expression' => '\\Yii::$app->getModulesHash()']);
     DbState::bootstrap();
     Yii::$container->set('gromver\\modulequery\\ModuleQuery', ['cache' => $this->cache, 'cacheDependency' => new ExpressionDependency(['expression' => '\\Yii::$app->getModulesHash()'])]);
 }
示例#7
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     $this->trigger(self::EVENT_ON_INIT);
     if ($this->isDatabaseInstalled()) {
         $baseUrl = Setting::get('baseUrl');
         Yii::setAlias("@web", $baseUrl);
         $this->urlManager->scriptUrl = $baseUrl;
     }
 }
 public function __construct(array $config = [])
 {
     Yii::setAlias('system', __DIR__);
     Yii::setAlias('admin', dirname(__DIR__) . '/admin');
     $this->initPluginManager($config);
     $haloConfig = (require __DIR__ . '/config/haloconfig-console.php');
     $cfg = ArrayHelper::merge($haloConfig, $this->loadPluginConfigs('/config-console.php'), $config);
     $file = $cfg['vendorPath'] . '/yiisoft/extensions.php';
     $extensions = is_file($file) ? include $file : [];
     if (isset($cfg['extensions']) && is_array($cfg['extensions'])) {
         $cfg['extensions'] = ArrayHelper::merge($extensions, $cfg['extensions']);
     } else {
         $cfg['extensions'] = $extensions;
     }
     parent::__construct($cfg);
 }
示例#9
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     $this->trigger(self::EVENT_ON_INIT);
     if ($this->isDatabaseInstalled()) {
         $baseUrl = Yii::$app->settings->get('baseUrl');
         if ($baseUrl !== null) {
             Yii::setAlias("@web", $baseUrl);
             $this->urlManager->scriptUrl = $baseUrl;
             $this->urlManager->baseUrl = $baseUrl;
             // Set hostInfo based on given baseUrl
             $urlParts = parse_url($baseUrl);
             $hostInfo = $urlParts['scheme'] . '://' . $urlParts['host'];
             if (isset($urlParts['port'])) {
                 $hostInfo .= ':' . $urlParts['port'];
             }
             $this->urlManager->hostInfo = $hostInfo;
         }
     }
 }
示例#10
0
 public function __construct($config)
 {
     parent::__construct($config);
     // If database if unavailable skip settings (initital install).
     try {
         // Set name from database if not set by config file.
         if (!isset($config['name'])) {
             if ($name = Config::setting('application.name')) {
                 $this->name = $name;
             }
         }
         // Set email from database if not set by config file.
         if (!isset(Yii::$app->params['adminEmail'])) {
             Yii::$app->params['adminEmail'] = null;
             if ($email = Config::setting('application.admin.email')) {
                 Yii::$app->params['adminEmail'] = $email;
             }
         }
     } catch (DBException $e) {
         // Do nothing.
     }
 }
示例#11
0
 /**
  * In addition to the default behavior of runAction, the console command
  * will strip the first element of the route and threat it as a module
  * changed the controller namespace to run the commands.
  *
  * ```
  * ./vendor/bin/luya <module>/<commandController>/<commandAction>
  * ```
  *
  * Will run all controllers located in the `commands` folder of a module.
  *
  * {@inheritDoc}
  *
  * @see \yii\console\Application::runAction()
  * @since 1.0.0-beta6
  */
 public function runAction($route, $params = [])
 {
     // In addition to the default behavior of runAction, the console command
     // will strip the first element of the route and threat it as a module
     // changed the controller namespace to run the commands
     if (!empty($route)) {
         $partial = explode("/", $route);
         // if there is a first key in the splitted array
         if (isset($partial[0]) && count($partial) > 1 && ($module = Yii::$app->getModule($partial[0]))) {
             try {
                 // change the controller namespace of this module to make usage of `commands`.
                 $module->controllerNamespace = $module->namespace . '\\commands';
                 unset($partial[0]);
                 // action response
                 return $module->runAction(implode("/", $partial), $params);
             } catch (\Exception $e) {
                 throw new Exception("Exception in route \"{$route}\": \"{$e->getMessage()}\" in file \"{$e->getFile()}\" on line {$e->getLine()}.", 0, $e);
             }
         }
     }
     // call parent action
     return parent::runAction($route, $params);
 }
 /**
  * @inheritdoc
  */
 public function coreComponents()
 {
     return array_merge(parent::coreComponents(), ['router' => ['class' => '\\sergebezborodov\\beanstalk\\Router'], 'beanstalk' => ['class' => '\\sergebezborodov\\beanstalk\\Beanstalk']]);
 }
示例#13
0
 /**
  * @inheritdoc
  */
 public function coreComponents()
 {
     return array_merge(parent::coreComponents(), ['request' => ['class' => 'yii\\console\\Request'], 'response' => ['class' => 'yii\\console\\Response'], 'errorHandler' => ['class' => 'yii\\console\\ErrorHandler']]);
 }
示例#14
0
 public function coreComponents()
 {
     return array_merge(parent::coreComponents(), ['user' => ['class' => 'yii\\web\\User']]);
 }
示例#15
0
 protected function _before()
 {
     $this->appConfig = array('id' => 'yiistrap-test', 'basePath' => dirname(__DIR__), 'class' => \yii\console\Application::className());
     $this->mockApplication();
 }
示例#16
0
 /**
  * Returns the log context for a console application
  * @return array
  */
 public function getConsoleContext(\yii\console\Application $app)
 {
     return [['title' => 'Params', 'value' => implode(' ', $app->getRequest()->getParams()), 'short' => true]];
 }
示例#17
0
 public function createControllerByID($id)
 {
     /// skip start for init goal
     if ($this->_first) {
         $this->_first = false;
         static $skips = ['init' => 1, 'clone' => 1, '--version' => 1];
         if (!isset($skips[$id])) {
             $this->runRequest('start');
         }
     }
     if ($this->get('config')->hasGoal($id)) {
         return $this->get('config')->get($id);
     }
     $controller = parent::createControllerByID($id);
     $this->get('config')->set($id, $controller);
     return $controller;
 }
示例#18
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->bootstrap = array_merge($this->bootstrap, ['grom']);
     parent::init();
 }
示例#19
0
 /**
  * @inheritdoc
  */
 public function coreCommands()
 {
     return array_merge(parent::coreCommands(), ['migrate' => 'yii\\platform\\console\\controllers\\MigrateController', 'runner' => 'yii\\platform\\console\\controllers\\RunnerController', 'message' => 'yii\\platform\\console\\controllers\\MessageController', 'platform' => 'yii\\platform\\console\\controllers\\PlatformController']);
 }