更新tp5内核

This commit is contained in:
2018-01-02 23:03:31 +08:00
parent 590696a06b
commit 3818619504
99 changed files with 3362 additions and 2006 deletions
+243 -118
View File
@@ -20,20 +20,49 @@ use think\console\output\driver\Buffer;
class Console
{
/**
* @var string 命令名称
*/
private $name;
/**
* @var string 命令版本
*/
private $version;
/** @var Command[] */
/**
* @var Command[] 命令
*/
private $commands = [];
/**
* @var bool 是否需要帮助信息
*/
private $wantHelps = false;
/**
* @var bool 是否捕获异常
*/
private $catchExceptions = true;
private $autoExit = true;
/**
* @var bool 是否自动退出执行
*/
private $autoExit = true;
/**
* @var InputDefinition 输入定义
*/
private $definition;
/**
* @var string 默认执行的命令
*/
private $defaultCommand;
/**
* @var array 默认提供的命令
*/
private static $defaultCommands = [
"think\\console\\command\\Help",
"think\\console\\command\\Lists",
@@ -47,6 +76,12 @@ class Console
"think\\console\\command\\optimize\\Schema",
];
/**
* Console constructor.
* @access public
* @param string $name 名称
* @param string $version 版本
*/
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
{
$this->name = $name;
@@ -60,38 +95,44 @@ class Console
}
}
/**
* 初始化 Console
* @access public
* @param bool $run 是否运行 Console
* @return int|Console
*/
public static function init($run = true)
{
static $console;
if (!$console) {
// 实例化console
// 实例化 console
$console = new self('Think Console', '0.1');
// 读取指令集
if (is_file(CONF_PATH . 'command' . EXT)) {
$commands = include CONF_PATH . 'command' . EXT;
if (is_array($commands)) {
foreach ($commands as $command) {
if (class_exists($command) && is_subclass_of($command, "\\think\\console\\Command")) {
// 注册指令
$console->add(new $command());
}
class_exists($command) &&
is_subclass_of($command, "\\think\\console\\Command") &&
$console->add(new $command()); // 注册指令
}
}
}
}
if ($run) {
// 运行
return $console->run();
} else {
return $console;
}
return $run ? $console->run() : $console;
}
/**
* @param $command
* @param array $parameters
* @param string $driver
* @return Output|Buffer
* 调用命令
* @access public
* @param string $command
* @param array $parameters
* @param string $driver
* @return Output
*/
public static function call($command, array $parameters = [], $driver = 'buffer')
{
@@ -110,9 +151,9 @@ class Console
/**
* 执行当前的指令
* @access public
* @return int
* @throws \Exception
* @api
*/
public function run()
{
@@ -124,27 +165,21 @@ class Console
try {
$exitCode = $this->doRun($input, $output);
} catch (\Exception $e) {
if (!$this->catchExceptions) {
throw $e;
}
if (!$this->catchExceptions) throw $e;
$output->renderException($e);
$exitCode = $e->getCode();
if (is_numeric($exitCode)) {
$exitCode = (int) $exitCode;
if (0 === $exitCode) {
$exitCode = 1;
}
$exitCode = ((int) $exitCode) ?: 1;
} else {
$exitCode = 1;
}
}
if ($this->autoExit) {
if ($exitCode > 255) {
$exitCode = 255;
}
if ($exitCode > 255) $exitCode = 255;
exit($exitCode);
}
@@ -154,12 +189,14 @@ class Console
/**
* 执行指令
* @param Input $input
* @param Output $output
* @access public
* @param Input $input 输入
* @param Output $output 输出
* @return int
*/
public function doRun(Input $input, Output $output)
{
// 获取版本信息
if (true === $input->hasParameterOption(['--version', '-V'])) {
$output->writeln($this->getLongVersion());
@@ -168,6 +205,7 @@ class Console
$name = $this->getCommandName($input);
// 获取帮助信息
if (true === $input->hasParameterOption(['--help', '-h'])) {
if (!$name) {
$name = 'help';
@@ -182,25 +220,26 @@ class Console
$input = new Input([$this->defaultCommand]);
}
$command = $this->find($name);
$exitCode = $this->doRunCommand($command, $input, $output);
return $exitCode;
return $this->doRunCommand($this->find($name), $input, $output);
}
/**
* 设置输入参数定义
* @param InputDefinition $definition
* @access public
* @param InputDefinition $definition 输入定义
* @return $this;
*/
public function setDefinition(InputDefinition $definition)
{
$this->definition = $definition;
return $this;
}
/**
* 获取输入参数定义
* @return InputDefinition The InputDefinition instance
* @access public
* @return InputDefinition
*/
public function getDefinition()
{
@@ -208,8 +247,9 @@ class Console
}
/**
* Gets the help message.
* @return string A help message.
* 获取帮助信息
* @access public
* @return string
*/
public function getHelp()
{
@@ -217,27 +257,34 @@ class Console
}
/**
* 是否捕获异常
* @param bool $boolean
* @api
* 设置是否捕获异常
* @access public
* @param bool $boolean 是否捕获
* @return $this
*/
public function setCatchExceptions($boolean)
{
$this->catchExceptions = (bool) $boolean;
return $this;
}
/**
* 是否自动退出
* @param bool $boolean
* @api
* 设置是否自动退出
* @access public
* @param bool $boolean 是否自动退出
* @return $this
*/
public function setAutoExit($boolean)
{
$this->autoExit = (bool) $boolean;
return $this;
}
/**
* 获取名称
* @access public
* @return string
*/
public function getName()
@@ -247,17 +294,21 @@ class Console
/**
* 设置名称
* @param string $name
* @access public
* @param string $name 名称
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* 获取版本
* @access public
* @return string
* @api
*/
public function getVersion()
{
@@ -266,21 +317,30 @@ class Console
/**
* 设置版本
* @param string $version
* @access public
* @param string $version 版本信息
* @return $this
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* 获取完整的版本号
* @access public
* @return string
*/
public function getLongVersion()
{
if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) {
return sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
return sprintf(
'<info>%s</info> version <comment>%s</comment>',
$this->getName(),
$this->getVersion()
);
}
return '<info>Console Tool</info>';
@@ -288,7 +348,8 @@ class Console
/**
* 注册一个指令
* @param string $name
* @access public
* @param string $name 指令名称
* @return Command
*/
public function register($name)
@@ -297,32 +358,37 @@ class Console
}
/**
* 添加指令
* @param Command[] $commands
* 批量添加指令
* @access public
* @param Command[] $commands 指令实例
* @return $this
*/
public function addCommands(array $commands)
{
foreach ($commands as $command) {
$this->add($command);
}
foreach ($commands as $command) $this->add($command);
return $this;
}
/**
* 添加一个指令
* @param Command $command
* @return Command
* @access public
* @param Command $command 命令实例
* @return Command|bool
*/
public function add(Command $command)
{
$command->setConsole($this);
if (!$command->isEnabled()) {
$command->setConsole(null);
return;
return false;
}
$command->setConsole($this);
if (null === $command->getDefinition()) {
throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
throw new \LogicException(
sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command))
);
}
$this->commands[$command->getName()] = $command;
@@ -336,14 +402,17 @@ class Console
/**
* 获取指令
* @param string $name 指令名称
* @access public
* @param string $name 指令名称
* @return Command
* @throws \InvalidArgumentException
*/
public function get($name)
{
if (!isset($this->commands[$name])) {
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
throw new \InvalidArgumentException(
sprintf('The command "%s" does not exist.', $name)
);
}
$command = $this->commands[$name];
@@ -363,7 +432,8 @@ class Console
/**
* 某个指令是否存在
* @param string $name 指令名称
* @access public
* @param string $name 指令名称
* @return bool
*/
public function has($name)
@@ -373,16 +443,22 @@ class Console
/**
* 获取所有的命名空间
* @access public
* @return array
*/
public function getNamespaces()
{
$namespaces = [];
foreach ($this->commands as $command) {
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
$namespaces = array_merge(
$namespaces, $this->extractAllNamespaces($command->getName())
);
foreach ($command->getAliases() as $alias) {
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias));
$namespaces = array_merge(
$namespaces, $this->extractAllNamespaces($alias)
);
}
}
@@ -390,21 +466,25 @@ class Console
}
/**
* 查找注册命名空间中的名称或缩写
* 查找注册命名空间中的名称或缩写
* @access public
* @param string $namespace
* @return string
* @throws \InvalidArgumentException
*/
public function findNamespace($namespace)
{
$allNamespaces = $this->getNamespaces();
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*';
}, $namespace);
$allNamespaces = $this->getNamespaces();
$namespaces = preg_grep('{^' . $expr . '}', $allNamespaces);
if (empty($namespaces)) {
$message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
$message = sprintf(
'There are no commands defined in the "%s" namespace.', $namespace
);
if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) {
if (1 == count($alternatives)) {
@@ -420,8 +500,14 @@ class Console
}
$exact = in_array($namespace, $namespaces, true);
if (count($namespaces) > 1 && !$exact) {
throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))));
throw new \InvalidArgumentException(
sprintf(
'The namespace "%s" is ambiguous (%s).',
$namespace,
$this->getAbbreviationSuggestions(array_values($namespaces)))
);
}
return $exact ? $namespace : reset($namespaces);
@@ -429,20 +515,22 @@ class Console
/**
* 查找指令
* @param string $name 名称或者别名
* @access public
* @param string $name 名称或者别名
* @return Command
* @throws \InvalidArgumentException
*/
public function find($name)
{
$allCommands = array_keys($this->commands);
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*';
}, $name);
$allCommands = array_keys($this->commands);
$commands = preg_grep('{^' . $expr . '}', $allCommands);
if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) {
if (false !== $pos = strrpos($name, ':')) {
if (false !== ($pos = strrpos($name, ':'))) {
$this->findNamespace(substr($name, 0, $pos));
}
@@ -462,7 +550,7 @@ class Console
if (count($commands) > 1) {
$commandList = $this->commands;
$commands = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands) {
$commands = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands) {
$commandName = $commandList[$nameOrAlias]->getName();
return $commandName === $nameOrAlias || !in_array($commandName, $commands);
@@ -473,7 +561,9 @@ class Console
if (count($commands) > 1 && !$exact) {
$suggestions = $this->getAbbreviationSuggestions(array_values($commands));
throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions));
throw new \InvalidArgumentException(
sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions)
);
}
return $this->get($exact ? $name : reset($commands));
@@ -481,21 +571,20 @@ class Console
/**
* 获取所有的指令
* @param string $namespace 命名空间
* @access public
* @param string $namespace 命名空间
* @return Command[]
* @api
*/
public function all($namespace = null)
{
if (null === $namespace) {
return $this->commands;
}
if (null === $namespace) return $this->commands;
$commands = [];
foreach ($this->commands as $name => $command) {
if ($this->extractNamespace($name, substr_count($namespace, ':') + 1) === $namespace) {
$commands[$name] = $command;
}
$ext = $this->extractNamespace($name, substr_count($namespace, ':') + 1);
if ($ext === $namespace) $commands[$name] = $command;
}
return $commands;
@@ -503,7 +592,8 @@ class Console
/**
* 获取可能的指令名
* @param array $names
* @access public
* @param array $names 指令名
* @return array
*/
public static function getAbbreviations($names)
@@ -511,7 +601,7 @@ class Console
$abbrevs = [];
foreach ($names as $name) {
for ($len = strlen($name); $len > 0; --$len) {
$abbrev = substr($name, 0, $len);
$abbrev = substr($name, 0, $len);
$abbrevs[$abbrev][] = $name;
}
}
@@ -520,9 +610,11 @@ class Console
}
/**
* 配置基于用户的参数和选项的输入和输出实例
* @param Input $input 输入实例
* @param Output $output 输出实例
* 配置基于用户的参数和选项的输入和输出实例
* @access protected
* @param Input $input 输入实例
* @param Output $output 输出实例
* @return void
*/
protected function configureIO(Input $input, Output $output)
{
@@ -551,9 +643,10 @@ class Console
/**
* 执行指令
* @param Command $command 指令实例
* @param Input $input 输入实例
* @param Output $output 输出实例
* @access protected
* @param Command $command 指令实例
* @param Input $input 输入实例
* @param Output $output 输出实例
* @return int
* @throws \Exception
*/
@@ -563,8 +656,9 @@ class Console
}
/**
* 获取指令的基础名称
* @param Input $input
* 获取指令的名称
* @access protected
* @param Input $input 输入实例
* @return string
*/
protected function getCommandName(Input $input)
@@ -574,6 +668,7 @@ class Console
/**
* 获取默认输入定义
* @access protected
* @return InputDefinition
*/
protected function getDefaultInputDefinition()
@@ -591,41 +686,55 @@ class Console
}
/**
* 设置默认命令
* @return Command[] An array of default Command instances
* 获取默认命令
* @access protected
* @return Command[]
*/
protected function getDefaultCommands()
{
$defaultCommands = [];
foreach (self::$defaultCommands as $classname) {
if (class_exists($classname) && is_subclass_of($classname, "think\\console\\Command")) {
$defaultCommands[] = new $classname();
foreach (self::$defaultCommands as $class) {
if (class_exists($class) && is_subclass_of($class, "think\\console\\Command")) {
$defaultCommands[] = new $class();
}
}
return $defaultCommands;
}
public static function addDefaultCommands(array $classnames)
/**
* 添加默认指令
* @access public
* @param array $classes 指令
* @return void
*/
public static function addDefaultCommands(array $classes)
{
self::$defaultCommands = array_merge(self::$defaultCommands, $classnames);
self::$defaultCommands = array_merge(self::$defaultCommands, $classes);
}
/**
* 获取可能的建议
* @param array $abbrevs
* @access private
* @param array $abbrevs
* @return string
*/
private function getAbbreviationSuggestions($abbrevs)
{
return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : '');
return sprintf(
'%s, %s%s',
$abbrevs[0],
$abbrevs[1],
count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : ''
);
}
/**
* 返回命名空间部分
* @param string $name 指令
* @param string $limit 部分的命名空间的最大数量
* 返回指令的命名空间部分
* @access public
* @param string $name 指令名称
* @param string $limit 部分的命名空间的最大数量
* @return string
*/
public function extractNamespace($name, $limit = null)
@@ -638,16 +747,17 @@ class Console
/**
* 查找可替代的建议
* @param string $name
* @param array|\Traversable $collection
* @access private
* @param string $name 指令名称
* @param array|\Traversable $collection 建议集合
* @return array
*/
private function findAlternatives($name, $collection)
{
$threshold = 1e3;
$alternatives = [];
$collectionParts = [];
foreach ($collection as $item) {
$collectionParts[$item] = explode(':', $item);
}
@@ -655,6 +765,7 @@ class Console
foreach (explode(':', $name) as $i => $subname) {
foreach ($collectionParts as $collectionName => $parts) {
$exists = isset($alternatives[$collectionName]);
if (!isset($parts[$i]) && $exists) {
$alternatives[$collectionName] += $threshold;
continue;
@@ -663,8 +774,14 @@ class Console
}
$lev = levenshtein($subname, $parts[$i]);
if ($lev <= strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) {
$alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;
if ($lev <= strlen($subname) / 3 ||
'' !== $subname &&
false !== strpos($parts[$i], $subname)
) {
$alternatives[$collectionName] = $exists ?
$alternatives[$collectionName] + $lev :
$lev;
} elseif ($exists) {
$alternatives[$collectionName] += $threshold;
}
@@ -673,14 +790,18 @@ class Console
foreach ($collection as $item) {
$lev = levenshtein($name, $item);
if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) {
$alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev;
$alternatives[$item] = isset($alternatives[$item]) ?
$alternatives[$item] - $lev :
$lev;
}
}
$alternatives = array_filter($alternatives, function ($lev) use ($threshold) {
return $lev < 2 * $threshold;
});
asort($alternatives);
return array_keys($alternatives);
@@ -688,24 +809,28 @@ class Console
/**
* 设置默认的指令
* @param string $commandName The Command name
* @access public
* @param string $commandName 指令名称
* @return $this
*/
public function setDefaultCommand($commandName)
{
$this->defaultCommand = $commandName;
return $this;
}
/**
* 返回所有的命名空间
* @param string $name
* @access private
* @param string $name 指令名称
* @return array
*/
private function extractAllNamespaces($name)
{
$parts = explode(':', $name, -1);
$namespaces = [];
foreach ($parts as $part) {
foreach (explode(':', $name, -1) as $part) {
if (count($namespaces)) {
$namespaces[] = end($namespaces) . ':' . $part;
} else {