I tried this solution but the arguments are empty when I debug the command class. It should be possible to create command classes by using a virtualType, no?
The constructor arguments are empty in TesterTestModelSyncCommandVirtual when I run the command.
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- Command List -->
<type name="MagentoFrameworkConsoleCommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="test_command" xsi:type="object">
TesterTestModelSyncCommandVirtual
</item>
</argument>
</arguments>
</type>
<virtualType name="TesterTestModelSyncCommandVirtual"
type="TestTesterCommandSyncCommand">
<arguments>
<item name="name" xsi:type="string">test:tester:sync</item>
<item name="description" xsi:type="string">Test sync cmd</item>
<item name="initiator" xsi:type="object">TestTesterModelSupplierASyncVirtual</item>
</arguments>
</virtualType>
SyncCommand.php
<?php
declare(strict_types=1);
namespace TestTesterCommand;
use LaminasConfigReaderIni;
use TestTesterModelInitiator;
use Exception;
use InvalidArgumentException;
use MagentoFrameworkConsoleCli;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;
class SyncCommand extends Command
{
protected string $name;
protected string $description;
protected Initiator $initiator;
public function __construct(
Initiator $initiator,
$description,
$name = ''
) {
$this->name = $name;
$this->description = $description;
$this->initiator = $initiator;
parent::__construct($name);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$output->writeln('hello');
$this->initiator->run();
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return Cli::RETURN_FAILURE;
}
return Cli::RETURN_SUCCESS;
}
}