public function has($name)
{
return isset(
$this->commands[
$name]);
}
public function getNamespaces()
{
$namespaces = [];
foreach (
$this->commands
as $command) {
$namespaces = array_merge(
$namespaces,
$this->extractAllNamespaces(
$command->getName()));
foreach (
$command->getAliases()
as $alias) {
$namespaces = array_merge(
$namespaces,
$this->extractAllNamespaces(
$alias));
}
}
return array_values(array_unique(array_filter(
$namespaces)));
}
public function findNamespace($namespace)
{
$allNamespaces =
$this->getNamespaces();
$expr = preg_replace_callback(
'{([^:]+|)}',
function ($matches) {
return preg_quote(
$matches[
1]) .
'[^:]*';
},
$namespace);
$namespaces = preg_grep(
'{^' .
$expr .
'}',
$allNamespaces);
if (
empty(
$namespaces)) {
$message = sprintf(
'There are no commands defined in the "%s" namespace.',
$namespace);
if (
$alternatives =
$this->findAlternatives(
$namespace,
$allNamespaces)) {
if (
1 == count(
$alternatives)) {
$message .=
"\n\nDid you mean this?\n ";
}
else {
$message .=
"\n\nDid you mean one of these?\n ";
}
$message .= implode(
"\n ",
$alternatives);
}
throw new \InvalidArgumentException(
$message);
}
$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))));
}
return $exact ?
$namespace : reset(
$namespaces);
}
public function find($name)
{
$allCommands = array_keys(
$this->commands);
$expr = preg_replace_callback(
'{([^:]+|)}',
function ($matches) {
return preg_quote(
$matches[
1]) .
'[^:]*';
},
$name);
$commands = preg_grep(
'{^' .
$expr .
'}',
$allCommands);
if (
empty(
$commands) || count(preg_grep(
'{^' .
$expr .
'$}',
$commands)) <
1) {
if (
false !==
$pos = strrpos(
$name,
':')) {
$this->findNamespace(substr(
$name,
0,
$pos));
}
$message = sprintf(
'Command "%s" is not defined.',
$name);
if (
$alternatives =
$this->findAlternatives(
$name,
$allCommands)) {
if (
1 == count(
$alternatives)) {
$message .=
"\n\nDid you mean this?\n ";
}
else {
$message .=
"\n\nDid you mean one of these?\n ";
}
$message .= implode(
"\n ",
$alternatives);
}
throw new \InvalidArgumentException(
$message);
}
if (count(
$commands) >
1) {
$commandList =
$this->commands;
$commands = array_filter(
$commands,
function ($nameOrAlias) use ($commandList, $commands) {
$commandName =
$commandList[
$nameOrAlias]->getName();
return $commandName ===
$nameOrAlias || !in_array(
$commandName,
$commands);
});
}
$exact = in_array(
$name,
$commands,
true);
if (count(
$commands) >
1 && !
$exact) {
$suggestions =
$this->getAbbreviationSuggestions(array_values(
$commands));
throw new \InvalidArgumentException(sprintf(
'Command "%s" is ambiguous (%s).',
$name,
$suggestions));
}
return $this->get(
$exact ?
$name : reset(
$commands));
}
转载请注明原文地址: https://ju.6miu.com/read-677867.html