This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Symfony\Component\Console\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Command\LazyCommand;
|
||||
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
|
||||
@@ -38,22 +39,39 @@ class AddConsoleCommandPass implements CompilerPassInterface
|
||||
|
||||
foreach ($commandServices as $id => $tags) {
|
||||
$definition = $container->getDefinition($id);
|
||||
$definition->addTag('container.no_preload');
|
||||
$class = $container->getParameterBag()->resolveValue($definition->getClass());
|
||||
|
||||
if (isset($tags[0]['command'])) {
|
||||
$aliases = $tags[0]['command'];
|
||||
} else {
|
||||
if (!$r = $container->getReflectionClass($class)) {
|
||||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
}
|
||||
if (!$r->isSubclassOf(Command::class)) {
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, 'console.command', Command::class));
|
||||
}
|
||||
$aliases = str_replace('%', '%%', $class::getDefaultName() ?? '');
|
||||
if (!$r = $container->getReflectionClass($class)) {
|
||||
throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
}
|
||||
|
||||
$aliases = explode('|', $aliases ?? '');
|
||||
if (!$r->isSubclassOf(Command::class)) {
|
||||
if (!$r->hasMethod('__invoke')) {
|
||||
throw new InvalidArgumentException(\sprintf('The service "%s" tagged "%s" must either be a subclass of "%s" or have an "__invoke()" method.', $id, 'console.command', Command::class));
|
||||
}
|
||||
|
||||
$invokableRef = new Reference($id);
|
||||
$definition = $container->register($id .= '.command', $class = Command::class)
|
||||
->addMethodCall('setCode', [$invokableRef]);
|
||||
} else {
|
||||
$invokableRef = null;
|
||||
}
|
||||
|
||||
$definition->addTag('container.no_preload');
|
||||
|
||||
/** @var AsCommand|null $attribute */
|
||||
$attribute = ($r->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
|
||||
|
||||
if (Command::class !== (new \ReflectionMethod($class, 'getDefaultName'))->class) {
|
||||
trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultName()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);
|
||||
|
||||
$defaultName = $class::getDefaultName();
|
||||
} else {
|
||||
$defaultName = $attribute?->name;
|
||||
}
|
||||
|
||||
$aliases = str_replace('%', '%%', $tags[0]['command'] ?? $defaultName ?? '');
|
||||
$aliases = explode('|', $aliases);
|
||||
$commandName = array_shift($aliases);
|
||||
|
||||
if ($isHidden = '' === $commandName) {
|
||||
@@ -61,7 +79,7 @@ class AddConsoleCommandPass implements CompilerPassInterface
|
||||
}
|
||||
|
||||
if (null === $commandName) {
|
||||
if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag('container.private')) {
|
||||
if ($definition->isPrivate() || $definition->hasTag('container.private')) {
|
||||
$commandId = 'console.command.public_alias.'.$id;
|
||||
$container->setAlias($commandId, $id)->setPublic(true);
|
||||
$id = $commandId;
|
||||
@@ -72,6 +90,8 @@ class AddConsoleCommandPass implements CompilerPassInterface
|
||||
}
|
||||
|
||||
$description = $tags[0]['description'] ?? null;
|
||||
$help = $tags[0]['help'] ?? null;
|
||||
$usages = $tags[0]['usages'] ?? null;
|
||||
|
||||
unset($tags[0]);
|
||||
$lazyCommandMap[$commandName] = $id;
|
||||
@@ -88,6 +108,8 @@ class AddConsoleCommandPass implements CompilerPassInterface
|
||||
}
|
||||
|
||||
$description ??= $tag['description'] ?? null;
|
||||
$help ??= $tag['help'] ?? null;
|
||||
$usages ??= $tag['usages'] ?? null;
|
||||
}
|
||||
|
||||
$definition->addMethodCall('setName', [$commandName]);
|
||||
@@ -100,21 +122,32 @@ class AddConsoleCommandPass implements CompilerPassInterface
|
||||
$definition->addMethodCall('setHidden', [true]);
|
||||
}
|
||||
|
||||
if ($help && $invokableRef) {
|
||||
$definition->addMethodCall('setHelp', [str_replace('%', '%%', $help)]);
|
||||
}
|
||||
|
||||
if ($usages) {
|
||||
foreach ($usages as $usage) {
|
||||
$definition->addMethodCall('addUsage', [$usage]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$description) {
|
||||
if (!$r = $container->getReflectionClass($class)) {
|
||||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
if (Command::class !== (new \ReflectionMethod($class, 'getDefaultDescription'))->class) {
|
||||
trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultDescription()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);
|
||||
|
||||
$description = $class::getDefaultDescription();
|
||||
} else {
|
||||
$description = $attribute?->description;
|
||||
}
|
||||
if (!$r->isSubclassOf(Command::class)) {
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, 'console.command', Command::class));
|
||||
}
|
||||
$description = str_replace('%', '%%', $class::getDefaultDescription() ?? '');
|
||||
}
|
||||
|
||||
if ($description) {
|
||||
$definition->addMethodCall('setDescription', [$description]);
|
||||
$escapedDescription = str_replace('%', '%%', $description);
|
||||
$definition->addMethodCall('setDescription', [$escapedDescription]);
|
||||
|
||||
$container->register('.'.$id.'.lazy', LazyCommand::class)
|
||||
->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
|
||||
->setArguments([$commandName, $aliases, $escapedDescription, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
|
||||
|
||||
$lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user