- md5
- 33e85b619612a0b57838dd66342b4eb7
- sha1
- 1c60bf3205ce77e10c24a05833a2fa9d1d5c9350
- sha256
- 0e10b1fb7d44ef86064d303dbf7f3ef68d09320ef5f1a0a472831a39c6a2f602
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Command;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Represents an invokable command.
*
* @author Yonel Ceruto <[email protected]>
*
* @internal
*/
class InvokableCommand implements SignalableCommandInterface
{
private readonly \Closure $code;
private readonly ?SignalableCommandInterface $signalableCommand;
private readonly \ReflectionFunction $reflection;
private bool $triggerDeprecations = false;
public function __construct(
private readonly Command $command,
callable $code,
) {
$this->code = $this->getClosure($code);
$this->signalableCommand = $code instanceof SignalableCommandInterface ? $code : null;
$this->reflection = new \ReflectionFunction($this->code);
}
/**
* Invokes a callable with parameters generated from the input interface.
*/
public function __invoke(InputInterface $input, OutputInterface $output): int
{
$statusCode = ($this->code)(...$this->getParameters($input, $output));
if (!\is_int($statusCode)) {
if ($this->triggerDeprecations) {
trigger_deprecation('symfony/console', '7.3', \sprintf('Returning a non-integer value from the command "%s" is deprecated and will throw an exception in Symfony 8.0.', $this->command->getName()));
return 0;
}
throw new \TypeError(\sprintf('The command "%s" must return an integer value in the "%s" method, but "%s" was returned.', $this->command->getName(), $this->reflection->getName(), get_debug_type($statusCode)));
}
return $statusCode;
}
/**
* Configures the input definition from an invokable-defined function.
*
* Processes the parameters of the reflection function to extract and
* add arguments or options to the provided input definition.
*/
public function configure(InputDefinition $definition): void
{
foreach ($this->reflection->getParameters() as $parameter) {
if ($argument = Argument::tryFrom($parameter)) {
$definition->addArgument($argument->toInputArgument());
} elseif ($option = Option::tryFrom($parameter)) {
$definition->addOption($option->toInputOption());
}
}
}
private function getClosure(callable $code): \Closure
{
if (!$code instanceof \Closure) {
return $code(...);
}
$this->triggerDeprecations = true;
if (null !== (new \ReflectionFunction($code))->getClosureThis()) {
return $code;
}
set_error_handler(static function () {});
try {
if ($c = \Closure::bind($code, $this->command)) {
$code = $c;
}
} finally {
restore_error_handler();
}
return $code;
}
private function getParameters(InputInterface $input, OutputInterface $output): array
{
$parameters = [];
foreach ($this->reflection->getParameters() as $parameter) {
if ($argument = Argument::tryFrom($parameter)) {
$parameters[] = $argument->resolveValue($input);
continue;
}
if ($option = Option::tryFrom($parameter)) {
$parameters[] = $option->resolveValue($input);
continue;
}
$type = $parameter->getType();
if (!$type instanceof \ReflectionNamedType) {
if ($this->triggerDeprecations) {
trigger_deprecation('symfony/console', '7.3', \sprintf('Omitting the type declaration for the parameter "$%s" is deprecated and will throw an exception in Symfony 8.0.', $parameter->getName()));
continue;
}
throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped, Union or Intersection types are not supported.', $parameter->getName()));
}
$parameters[] = match ($type->getName()) {
InputInterface::class => $input,
OutputInterface::class => $output,
SymfonyStyle::class => new SymfonyStyle($input, $output),
Application::class => $this->command->getApplication(),
default => throw new RuntimeException(\sprintf('Unsupported type "%s" for parameter "$%s".', $type->getName(), $parameter->getName())),
};
}
return $parameters ?: [$input, $output];
}
public function getSubscribedSignals(): array
{
return $this->signalableCommand?->getSubscribedSignals() ?? [];
}
public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
{
return $this->signalableCommand?->handleSignal($signal, $previousExitCode) ?? false;
}
}
The same content is in 17 releases, Joomla 6.0.0-beta1 to 6.1.0-alpha1. See them all, or every version of this file.