$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: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\console\command\optimize;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Route extends Command
{
/** @var Output */
protected $output;
protected function configure()
{
$this->setName('optimize:route')
->setDescription('Build route cache.');
}
protected function execute(Input $input, Output $output)
{
if (!is_dir(RUNTIME_PATH)) {
@mkdir(RUNTIME_PATH, 0755, true);
}
file_put_contents(RUNTIME_PATH . 'route.php', $this->buildRouteCache());
$output->writeln('<info>Succeed!</info>');
}
protected function buildRouteCache()
{
$files = \think\Config::get('route_config_file');
foreach ($files as $file) {
if (is_file(CONF_PATH . $file . CONF_EXT)) {
$config = include CONF_PATH . $file . CONF_EXT;
if (is_array($config)) {
\think\Route::import($config);
}
}
}
$rules = \think\Route::rules(true);
array_walk_recursive($rules, [$this, 'buildClosure']);
$content = '<?php ' . PHP_EOL . 'return ';
$content .= var_export($rules, true) . ';';
$content = str_replace(['\'[__start__', '__end__]\''], '', stripcslashes($content));
return $content;
}
protected function buildClosure(&$value)
{
if ($value instanceof \Closure) {
$reflection = new \ReflectionFunction($value);
$startLine = $reflection->getStartLine();
$endLine = $reflection->getEndLine();
$file = $reflection->getFileName();
$item = file($file);
$content = '';
for ($i = $startLine - 1; $i <= $endLine - 1; $i++) {
$content .= $item[$i];
}
$start = strpos($content, 'function');
$end = strrpos($content, '}');
$value = '[__start__' . substr($content, $start, $end - $start + 1) . '__end__]';
}
}
}