CommandCall: fix command name when AutoPrix or AutoDiscovery

master 3.8.0
Julien Rosset 9 months ago
parent c40a14aba0
commit c1c36475de

@ -0,0 +1,41 @@
<?php
namespace jrosset\CliProgram;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Helper methods
*/
abstract class CliHelper {
/**
* Get the “error” output if exists, else the output itself
*
* @param OutputInterface $output The output
*
* @return OutputInterface The “error” output if exists, else the output itself
*/
public static final function getErrorOutput (OutputInterface $output): OutputInterface {
return $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
}
/**
* Get the name of a command class
*
* @param Application $application The application
* @param class-string<Command> $commandClass The command class
*
* @return string|null The command name ; Null if not found
*/
public static final function getCommandNameFromClass (Application $application, string $commandClass): ?string {
foreach ($application->all() as $possibleCommand) {
if ($possibleCommand::class == $commandClass) {
return $possibleCommand->getName();
}
}
return null;
}
}

@ -3,6 +3,7 @@
namespace jrosset\CliProgram\CommandCall;
use Arrayy\Arrayy;
use jrosset\CliProgram\CliHelper;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
@ -74,7 +75,9 @@ class CommandCall {
* @return $this
*/
public function setCommandName (string|Command $commandName): self {
$this->commandName = $commandName instanceof Command ? $commandName->getName() : $commandName;
$this->commandName = $commandName instanceof Command
? CliHelper::getCommandNameFromClass($commandName->getApplication(), $commandName::class)
: $commandName;
return $this;
}

@ -1,22 +0,0 @@
<?php
namespace jrosset\CliProgram;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Helper class for command/application output
*/
abstract class OutputHelper {
/**
* Get the “error” output if exists, else the output itself
*
* @param OutputInterface $output The output
*
* @return OutputInterface The “error” output if exists, else the output itself
*/
public static final function getErrorOutput (OutputInterface $output): OutputInterface {
return $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
}
}

@ -2,7 +2,7 @@
namespace jrosset\CliProgram\Requirements;
use jrosset\CliProgram\OutputHelper;
use jrosset\CliProgram\CliHelper;
use ReflectionAttribute;
use ReflectionClass;
use Symfony\Component\Console\ConsoleEvents;
@ -62,7 +62,7 @@ trait TRequirementsApplication {
//endregion
}
catch (Throwable $exception) {
$this->renderThrowable($exception, OutputHelper::getErrorOutput($commandOutput));
$this->renderThrowable($exception, CliHelper::getErrorOutput($commandOutput));
$event->disableCommand();
$event->stopPropagation();

Loading…
Cancel
Save