Created
June 9, 2020 19:14
-
-
Save tSixTM/86a29ee75dbd117c8f8571d458ed72db to your computer and use it in GitHub Desktop.
Symfony 5 URL Generation from Console Command not working...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:deliver'; | |
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 | |
$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('api/ep'); | |
$url = $context->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; | |
} | |
} | |
} | |
# services.yaml | |
# This file is the entry point to configure your own services. | |
# Files in the packages/ subdirectory configure your dependencies. | |
# Put parameters here that don't need to change on each machine where the app is deployed | |
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration | |
parameters: | |
services: | |
# default configuration for services in *this* file | |
_defaults: | |
autowire: true # Automatically injects dependencies in your services. | |
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. | |
# makes classes in src/ available to be used as services | |
# this creates a service per class whose id is the fully-qualified class name | |
App\: | |
resource: '../src/*' | |
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}' | |
# controllers are imported separately to make sure services can be injected | |
# as action arguments even if you don't extend any base controller class | |
App\Controller\: | |
resource: '../src/Controller' | |
tags: ['controller.service_arguments'] | |
# add more service definitions when explicit configuration is needed | |
# please note that last definitions always *replace* previous ones | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment