$input
$input : \think\console\Input
$input : \think\console\Input
$output : \think\console\Output
$console : \think\Console
setConsole(\think\Console $console = null)
设置控制台
\think\Console | $console |
getConsole() : \think\Console
获取控制台
run(\think\console\Input $input, \think\console\Output $output) : integer
执行
\think\console\Input | $input | |
\think\console\Output | $output |
setCode(callable $code) : \think\console\Command
设置执行代码
callable | $code | callable(InputInterface $input, OutputInterface $output) |
setDefinition(array|\think\console\input\Definition $definition) : \think\console\Command
设置参数定义
array|\think\console\input\Definition | $definition |
getDefinition() : \think\console\input\Definition
获取参数定义
getNativeDefinition() : \think\console\input\Definition
获取当前指令的参数定义
addArgument(string $name, integer $mode = null, string $description = '', mixed $default = null) : \think\console\Command
添加参数
string | $name | 名称 |
integer | $mode | 类型 |
string | $description | 描述 |
mixed | $default | 默认值 |
addOption(string $name, string $shortcut = null, integer $mode = null, string $description = '', mixed $default = null) : \think\console\Command
添加选项
string | $name | 选项名称 |
string | $shortcut | 别名 |
integer | $mode | 类型 |
string | $description | 描述 |
mixed | $default | 默认值 |
setName(string $name) : \think\console\Command
设置指令名称
string | $name |
setDescription(string $description) : \think\console\Command
设置描述
string | $description |
setHelp(string $help) : \think\console\Command
设置帮助信息
string | $help |
setAliases(array<mixed,string> $aliases) : \think\console\Command
设置别名
array<mixed,string> | $aliases |
execute(\think\console\Input $input, \think\console\Output $output) : null|integer
执行指令
\think\console\Input | $input | |
\think\console\Output | $output |
interact(\think\console\Input $input, \think\console\Output $output)
用户验证
\think\console\Input | $input | |
\think\console\Output | $output |
initialize(\think\console\Input $input, \think\console\Output $output)
初始化
\think\console\Input | $input | An InputInterface instance |
\think\console\Output | $output | An OutputInterface instance |
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\console\command\optimize;
use think\Config as ThinkConfig;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
class Config extends Command
{
/** @var Output */
protected $output;
protected function configure()
{
$this->setName('optimize:config')
->addArgument('module', Argument::OPTIONAL, 'Build module config cache .')
->setDescription('Build config and common file cache.');
}
protected function execute(Input $input, Output $output)
{
if ($input->getArgument('module')) {
$module = $input->getArgument('module') . DS;
} else {
$module = '';
}
$content = '<?php ' . PHP_EOL . $this->buildCacheContent($module);
if (!is_dir(RUNTIME_PATH . $module)) {
@mkdir(RUNTIME_PATH . $module, 0755, true);
}
file_put_contents(RUNTIME_PATH . $module . 'init' . EXT, $content);
$output->writeln('<info>Succeed!</info>');
}
protected function buildCacheContent($module)
{
$content = '';
$path = realpath(APP_PATH . $module) . DS;
if ($module) {
// 加载模块配置
$config = ThinkConfig::load(CONF_PATH . $module . 'config' . CONF_EXT);
// 读取数据库配置文件
$filename = CONF_PATH . $module . 'database' . CONF_EXT;
ThinkConfig::load($filename, 'database');
// 加载应用状态配置
if ($config['app_status']) {
$config = ThinkConfig::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT);
}
// 读取扩展配置文件
if (is_dir(CONF_PATH . $module . 'extra')) {
$dir = CONF_PATH . $module . 'extra';
$files = scandir($dir);
foreach ($files as $file) {
if (strpos($file, CONF_EXT)) {
$filename = $dir . DS . $file;
ThinkConfig::load($filename, pathinfo($file, PATHINFO_FILENAME));
}
}
}
}
// 加载行为扩展文件
if (is_file(CONF_PATH . $module . 'tags' . EXT)) {
$content .= '\think\Hook::import(' . (var_export(include CONF_PATH . $module . 'tags' . EXT, true)) . ');' . PHP_EOL;
}
// 加载公共文件
if (is_file($path . 'common' . EXT)) {
$content .= substr(php_strip_whitespace($path . 'common' . EXT), 5) . PHP_EOL;
}
$content .= '\think\Config::set(' . var_export(ThinkConfig::get(), true) . ');';
return $content;
}
}