Как правильно вызвать команду в контроллере
Есть команда
<?php
namespace App\Command;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use App\Repository\ImageRepository;
use App\Repository\BattleCardRepository;
use App\Repository\CollectionCardRepository;
#[AsCommand(
name: 'app:cover:download',
description: 'Download image for cover',
)]
class CoverDownloadCommand extends Command
{
public function __construct(
private ImageRepository $imageRepository,
private BattleCardRepository $battleCardRepository,
private CollectionCardRepository $collectionCardRepository,
private EntityManagerInterface $entityManager
)
{
parent::__construct();
}
protected function configure(): void
{
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$battle_cards = $this->battleCardRepository->findAll();
$collection_cards = $this->collectionCardRepository->findAll();
$link = "https://api.telegram.org/bot{$_ENV['token']}/getFile?file_id=";
$file_link = "https://api.telegram.org/file/bot{$_ENV['token']}/";
$ch = curl_init();
foreach ($battle_cards as $card) {
$covers = [$card->getDefaultCover(), $card->getSilverCover(), $card->getGoldCover(), $card->getDiamondCover()];
foreach ($covers as $item) {
if($item) {
$img_db = $this->imageRepository->findOneBy(['cover' => $item]);
if(!$img_db) {
curl_setopt($ch, CURLOPT_URL, $link . $item);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$data = json_decode($response, true);
if ($data['ok']) {
$io->writeln($data['result']['file_path']);
$image_url = $file_link . $data['result']['file_path'];
$path_info = pathinfo($image_url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$image_data = curl_exec($ch);
curl_close($ch);
$save_path = "./public/images/{$item}.{$path_info['extension']}";
file_put_contents($save_path, $image_data);
$this->imageRepository->createImg($item, $path_info['extension']);
$this->entityManager->flush();
}
}
}
}
}
foreach ($collection_cards as $card) {
$img = $card->getPhoto();
$img_db = $this->imageRepository->findOneBy(['cover' => $img]);
if(!$img_db) {
curl_setopt($ch, CURLOPT_URL, $link . $img);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$data = json_decode($response, true);
if ($data['ok']) {
$io->writeln($data['result']['file_path']);
$image_url = $file_link . $data['result']['file_path'];
$path_info = pathinfo($image_url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$image_data = curl_exec($ch);
curl_close($ch);
$save_path = "./public/images/{$img}.{$path_info['extension']}";
file_put_contents($save_path, $image_data);
$this->imageRepository->createImg($img, $path_info['extension']);
$this->entityManager->flush();
}
}
}
curl_close($ch);
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
return Command::SUCCESS;
}
}
Если я ее просто запускаю из докер контейнера php bin/console app:cover:download так, то все работает, но мне нужно дать возможность запускать ее по api. Делаю класс контроллера
#[Route('/api')]
class ApiCardsController extends AbstractController
{
public function __construct(
private CommandExecutor $commandExecutor
){}
#[Route('/covers-download', name: 'coversDownload', methods:'GET')]
public function coversDownload(): Response
{
$output = $this->commandExecutor->execute('app:cover:download');
return $this->json(['status'=> true, 'message' => 'ok', 'output' => $output]);
}
}
И делаю сервис который вызывается
<?php
namespace App\Service;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Application;
use Symfony\Bundle\FrameworkBundle\Console\Application as SymfonyApplication;
use Symfony\Component\HttpKernel\KernelInterface;
class CommandExecutor
{
private $application;
public function __construct(KernelInterface $kernel)
{
$this->application = new SymfonyApplication($kernel);
$this->application->setAutoExit(false);
}
public function execute(string $commandName, array $arguments = []): string
{
$input = new ArrayInput(array_merge(['command' => $commandName], $arguments));
$output = new BufferedOutput();
$this->application->run($input, $output);
return $output->fetch();
}
}
Когда обращаюсь по api ссылке почему-то падает ошибка(при том что из консоли все ок)
Warning: file_put_contents(./public/images/AgACAgIAAxkBAAEBA4dmcbO89PLUSdj_ \n kpRr8XWPvwtuYwACdN4xG3QeiEpIk4hH5hObBgEAAwIAA3kAAzUE.jpg): Failed to open s \n tream: No such file or directory
\n
\n\nException trace:\n at /var/www/html/src/Command/CoverDownloadCommand.php:76\n App\Command\CoverDownloadCommand-\u003Eexecute() at /var/www/html/vendor/symfony/console/Command/Command.php:279\n Symfony\Component\Console\Command\Command-\u003Erun() at /var/www/html/vendor/symfony/console/Application.php:1047\n Symfony\Component\Console\Application-\u003EdoRunCommand() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:123\n Symfony\Bundle\FrameworkBundle\Console\Application-\u003EdoRunCommand() at /var/www/html/vendor/symfony/console/Application.php:316\n Symfony\Component\Console\Application-\u003EdoRun() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:77\n Symfony\Bundle\FrameworkBundle\Console\Application-\u003EdoRun() at /var/www/html/vendor/symfony/console/Application.php:167\n Symfony\Component\Console\Application-\u003Erun() at /var/www/html/src/Service/CommandExecutor.php:25\n App\Service\CommandExecutor-\u003Eexecute() at /var/www/html/src/Controller/Api/ApiCardsController.php:21\n App\Controller\Api\ApiCardsController-\u003EcoversDownload() at /var/www/html/vendor/symfony/http-kernel/HttpKernel.php:183\n Symfony\Component\HttpKernel\HttpKernel-\u003EhandleRaw() at /var/www/html/vendor/symfony/http-kernel/HttpKernel.php:76\n Symfony\Component\HttpKernel\HttpKernel-\u003Ehandle() at /var/www/html/vendor/symfony/http-kernel/Kernel.php:182\n Symfony\Component\HttpKernel\Kernel-\u003Ehandle() at /var/www/html/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35\n Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner-\u003Erun() at /var/www/html/vendor/autoload_runtime.php:29\n require_once() at /var/www/html/public/index.php:5\n\napp:cover:download\n\n
Так же попробовал выдать права из контейнера chmod -R 0777 public/images Не помогло