3 回答

TA貢獻1875條經驗 獲得超5個贊
private $router
您似乎遇到的問題不是由于您的服務聲明,而是您缺少中變量的聲明MyCommand。
因此,您可以保留services.yaml
要點中顯示的內容,無需更改自動裝配變量,也不必手動聲明命令
此外,您不需要$context
從路由器中獲取,您也可以在您的 中設置基本 URLframework.yaml
,在這里您可以找到我找到它的位置。
請注意,我從執行中刪除了一些代碼,這是因為我無法訪問您的其他文件。你可以重新添加這個。

TA貢獻1840條經驗 獲得超5個贊
好吧,解決這個問題并不是那么簡單。許多文檔已過時或未完全解決此問題。這是我到目前為止得到的:
服務.yaml:
Symfony\Component\Routing\RouterInterface:
? ? arguments: ['@router']
應用程序.php:
#!/usr/bin/env php
<?php
// application.php
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/src/Kernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.local');
$kernel = new App\Kernel(getenv('APP_ENV'), getenv('APP_DEBUG'));
$kernel->boot();
$container = $kernel->getContainer();
$application = new Application($kernel);
$application->add(new App\Command\MyCommand($container->get('router')));
$application->run();
注意:我將應用程序導入更改為 Symfony\Bundle\FrameworkBundle\Console\Application
我的命令.php:
<?php
// src/Command/MyCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpClient\HttpClient;
use App\SQSHelper;
class MyCommand extends Command
{
? ? use LockableTrait;
? ? // the name of the command (the part after "bin/console")
? ? protected static $defaultName = 'app:my-command';
? ? protected $router;
? ? public function __construct(RouterInterface $router)
? ? {
? ? ? ? parent::__construct();
? ? ? ? $this->router = $router;
? ? }
? ? protected function configure()
? ? {
? ? }
? ? protected function execute(InputInterface $input, OutputInterface $output)
? ? {
? ? ? ? if($this->lock()) { // Prevent running more than one instance
? ? ? ? ? ? $endpoint =?
? ? ? ? ? ? $queueName = 'Queue';
? ? ? ? ? ? $queue = new SQSHelper();
? ? ? ? ? ? while($queue->getApproxNumberOfMessages($queueName)) {
? ? ? ? ? ? ? ? $message = $queue->receiveMessage($queueName);
? ? ? ? ? ? ? ? if($message) {
? ? ? ? ? ? ? ? ? ? if($message['__EOQ__'] ?? FALSE) // End-of-Queue marker received
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? $context = $this->router->getContext();
? ? ? ? ? ? ? ? ? ? $context->setHost('localhost');
? ? ? ? ? ? ? ? ? ? $context->setHttpPort('49100');
? ? ? ? ? ? ? ? ? ? $context->setHttpsPort('49100');
? ? ? ? ? ? ? ? ? ? $context->setScheme('https');
? ? ? ? ? ? ? ? ? ? $context->setBaseUrl('');
? ? ? ? ? ? ? ? ? ? $url = $this->router->generate('ep', ['MessageId' => $message['MessageId']], UrlGeneratorInterface::ABSOLUTE_URL);
? ? ? ? ? ? ? ? ? ? $client = HttpClient::create();
? ? ? ? ? ? ? ? ? ? $response = $client->request('POST', $url, [
? ? ? ? ? ? ? ? ? ? ? ? 'headers' => ['Content-Type' => 'application/json'],
? ? ? ? ? ? ? ? ? ? ? ? 'body' => $message['Body'] // Already JSON encoded
? ? ? ? ? ? ? ? ? ? ]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? $this->release(); // Release lock
? ? ? ? ? ? // this method must return an integer number with the "exit status code"
? ? ? ? ? ? // of the command. You can also use these constants to make code more readable
? ? ? ? ? ? // return this if there was no problem running the command
? ? ? ? ? ? // (it's equivalent to returning int(0))
? ? ? ? ? ? return Command::SUCCESS;
? ? ? ? ? ? // or return this if some error happened during the execution
? ? ? ? ? ? // (it's equivalent to returning int(1))
? ? ? ? ? ? // return Command::FAILURE;
? ? ? ? }
? ? }
}
如果有什么感覺不對,或者如果您可以提供更好的解決方案或改進,請貢獻...
- 3 回答
- 0 關注
- 195 瀏覽
添加回答
舉報