I'm working on a command line start of an import process.
The process itself is already a custom plugin. It uses a cron job to run the import with some custom rules.
Here is my command class:
<?php
declare(strict_types = 1);
namespace Vendor\ProductImport\Api\Data;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Magento\Framework\App\State;
use Psr\Log\LoggerInterface;
use Vendor\ProductImport\Api\Data\ProductImportInterface;
use Vendor\ProductImport\Model\Provider\StateProvider;
class ProductImportCommand extends Command
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var State
*/
protected $state;
/**
* @var ProductImportInterface
*/
protected $productImport;
/**
* @var StateProvider
*/
protected $importStateProvider;
/**
* @param State $state
* @param LoggerInterface $logger
* @param ProductImportInterface $productImport
* @param StateProvider $importStateProvider
*/
public function __construct(
State $state,
LoggerInterface $logger,
ProductImportInterface $productImport,
StateProvider $importStateProvider
) {
parent::__construct();
$this->state = $state;
$this->logger = $logger;
$this->productImport = $productImport;
$this->importStateProvider = $importStateProvider;
$this->importStateProvider->setVendorImportState(true);
}
/**
* @inheritDoc
*/
protected function configure():void
{
$this
->setName('vendor:importproducts:db')
->setDescription('Start import process manually.')
->addOption(
'dry-run',
null,
InputOption::VALUE_NONE,
'Perform a dry-run without launching the import.'
);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|void|null
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->state->setAreaCode("adminhtml");
$isDryRun = $input->getOption('dry-run');
if (!$isDryRun) {
$output->writeln(
'Warning: this is not a dry run. If you want to do a dry-run, add --dry-run.'
);
$question = new ConfirmationQuestion('<info>Are you sure you want to continue? [No] </info>', false);
$questionHelper = $this->getHelper('question');
if (!$questionHelper->ask($input, $output, $question))
{
return;
}
}
if (!$isDryRun) {
try {
$this->productImport->loadData();
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}
}
}
}
When I try to compile, I get this:
Cannot instantiate interface Vendor\ProductImport\Api\Data\ProductImportInterface#0 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Vendor\\Pro...')
#1 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(170): Magento\Framework\ObjectManager\ObjectManager->get('Vendor\\Pro...')
#2 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Vendor\\Pro...', NULL, 'productImport', 'Vendor\\Pro...')
#3 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument('Vendor\\Pro...', Array, Array)
#4 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime('Vendor\\Pro...', Array, Array)
#5 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(59): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Vendor\\Pro...', Array, Array)
#6 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Vendor\\Pro...')
#7 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(206): Magento\Framework\ObjectManager\ObjectManager->get('Vendor\\Pro...')
#8 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(182): Magento\Framework\ObjectManager\Factory\AbstractFactory->parseArray(Array)
#9 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, NULL, Array, 'commands', 'Magento\\Framewo...')
#10 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument('Magento\\Framewo...', Array, Array)
#11 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime('Magento\\Framewo...', Array, Array)
#12 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(59): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Magento\\Framewo...', Array, Array)
#13 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Framewo...', Array)
#14 /var/www/php/vendor/magento/framework/Console/Cli.php(153): Magento\Framework\ObjectManager\ObjectManager->create('Magento\\Framewo...')
#15 /var/www/php/vendor/magento/framework/Console/Cli.php(134): Magento\Framework\Console\Cli->getApplicationCommands()
#16 /var/www/php/vendor/symfony/console/Application.php(1274): Magento\Framework\Console\Cli->getDefaultCommands()
#17 /var/www/php/vendor/symfony/console/Application.php(629): Symfony\Component\Console\Application->init()
#18 /var/www/php/vendor/symfony/console/Application.php(237): Symfony\Component\Console\Application->find('setup:di:compil...')
#19 /var/www/php/vendor/magento/framework/Console/Cli.php(115): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 /var/www/php/vendor/symfony/console/Application.php(149): Magento\Framework\Console\Cli->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 /var/www/php/bin/magento(23): Symfony\Component\Console\Application->run()
#22 {main}
My di.xml looks somewhat like this:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="vendor:importproducts:db" xsi:type="object">Vendor\ProductImport\Api\Data\ProductImportCommand</item>
</argument>
</arguments>
</type>
</config>
The weird part about it is that there are TONS of other classes in the plugin using this ProductImportInterface, but none of them have this issue.
Could it be because I'm using Syphony classes and it somehow doesnt see this ProductInterface?
I'm pretty much using the same code Adobe has outlined for a CLI command:
https://developer.adobe.com/commerce/php/development/cli-commands/custom/