Hi there, I’m pretty new on PHP/Laminas. I joined a nice team and we try to migrate an old project to PHP 8.
We were using Laminas/Console and now we try to use Symfony.
We have a service to update currencies into DB. So we want to keep it and beeing able to run it with a command like this :
.\vendor\bin\laminas import-currency
To do it, we created a CurrencyCommand class, extending from Command. In this class we want to call our previous method to update currencies:
class CurrencyCommand extends Command implements AbstractCustomService {
/**
* @var CurrencyService
*/
protected $service;
/** @var string */
protected static $defaultName = 'import-currency';
public function __construct(CurrencyService $currencyService, string $name = null){
parent::__construct($name);
$this->serivce = $currencyService;
}
protected function configure() : void
{
$this->setName(self::$defaultName);
$this->addOption('name', null, InputOption::VALUE_REQUIRED, 'Module name');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->getService()->importData();
return 0;
}
/**
* @return CurrencyService
*/
public function getService(): CurrencyService {
return $this->service;
}
/**
* @param CurrencyService $service
* @return ConsoleController
*/
public function setService($service): ConsoleController {
$this->service = $service;
return $this;
}
public function getServiceName(): string {
return CurrencyService::class;
}
}
We were also using a factory :
class CurrencyServiceFactory implements FactoryInterface {
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return CurrencyService
* @throws ContainerException
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
$baseServiceFactory = new BaseServiceFactory();
/**
* @var $currencyService CurrencyService
*/
$currencyService = $baseServiceFactory($container, CurrencyService::class, $options);
$config = $container->get('Config');
if (!isset($config['fixerio_api_key'])) {
throw new ServiceNotCreatedException("Please provide Fixer IO api key in config -> fixerio_api_key");
}
$currencyService->setApiKey($config['fixerio_api_key']);
return $currencyService;
}
}
The config file looks like this :
<?php
namespace Currency;
use Currency\Command\CurrencyCommand;
use Currency\Service\Factory\CurrencyServiceFactory;
return [
'laminas-cli' => [
'commands' => [
'import-currency' => CurrencyCommand::class,
],
],
'service_manager' => [
'factories' => [
CurrencyCommand::class => CurrencyServiceFactory::class,
],
],
];
Our problem is, when we run the command, we get :
Expected an instance of Symfony\Component\Console\Command\Command. Got: Currency\Service\CurrencyService
If we remove the service_manager, we’re not able to call the “importData()” function anymore
Any idea ?
Thanks