Pass parameter to command from controller Symfony2 - php

I have a command which executes some actions that depend on the entity passed in parameter.
checkAlertCommand.php:
<?php
namespace MDB\PlatformBundle\Command;
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;
class checkAlertCommand extends Command {
protected function configure() {
$this
->setName('platform:checkAlert')
->setDescription('Check the alert in in function of the current advert')
->addArgument(
'postedAdvert'
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
$postedAdvert = $input->getArgument('postedAdvert');
$output->writeln($postedAdvert->getTitre());
}
}
?>
So my questions are:
How to get an entity as argument in the checkAlertCommand.php?
How to call this command from a controller and pass the desired entity as argument?
Thanks.

You can't pass an entity directly to a console command. Instead of you should pass "id" of entity as argument, then use repository and pick up desired entity by its id.
<?php
namespace MDB\PlatformBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class checkAlertCommand extends ContainerAwareCommand {
protected function configure() {
$this
->setName('platform:checkAlert')
->setDescription('Check the alert in in function of the current advert')
->addArgument(
'postedAdvertId'
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
$postedAdvertId = $input->getArgument('postedAdvertId');
$em = $this->getContainer()->get('doctrine')->getManager();
$repo = $em->getRepository('MDBPlatformBundle:PostedAdvert');
$postedAdvert = $repo->find($postedAdvertId);
$output->writeln($postedAdvert->getTitre());
}
}
?>
You should use Process component to run the command inside a controller.
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use MDB\PlatformBundle\Command\checkAlertCommand;
class MyController extends Controller
{
public function indexAction()
{
// get post $postedAdvertId here
....
$command = new checkAlertCommand();
$command->setContainer($this->container);
$input = new ArrayInput(array('postedAdvertId' => $postedAdvertId));
$output = new NullOutput();
$result = $command->run($input, $output);
...
}
}
Update: Answer to your question
I'm not sure what exactly do you mean "asynchronous", but given example executes the command in synchronous way, so mean the controller will wait till command will be finished and only then will go to next operation. But if you need run it in asynchronous(in background) way,you should use Process component http://symfony.com/doc/current/components/process.html

Related

Testing Symfony custom maker (maker bundle)

I'm trying to make a custom maker with the Symfony make bundle.
The maker command looks like this:
<?php
namespace App\Maker;
use Doctrine\Common\Annotations\Annotation;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
final class MakeCustomEntity extends AbstractMaker
{
public static function getCommandName(): string
{
return 'make:custom-entity';
}
public static function getCommandDescription(): string
{
return 'Creates a new entity';
}
public function configureCommand(Command $command, InputConfiguration $inputConf)
{
$command
->addArgument('entity-class', InputArgument::OPTIONAL, sprintf('Choose a name for your entity class (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())));
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
{
}
public function configureDependencies(DependencyBuilder $dependencies)
{
$dependencies->addClassDependency(
Annotation::class,
'doctrine/annotations'
);
}
}
So far so good, the custom maker shows up when listing all commands.
However I would like to write a test for this maker (inspired from the tests I have found on the bundles github):
<?php
namespace Tests\Maker;
use App\Maker\MakeCustomEntity;
use Symfony\Bundle\MakerBundle\Test\MakerTestCase;
use Symfony\Bundle\MakerBundle\Test\MakerTestDetails;
class MakeCustomEntityTest extends MakerTestCase
{
public function getTestDetails()
{
yield 'entity_full_custom_namespace' => [
MakerTestDetails::createTest(
$this->getMakerInstance(MakeCustomEntity::class),
[
// entity class name
'\App\Domain\Entity\Test\Test',
]
)
->assert(function (string $output, string $directory) {
$this->assertStringContainsString('created: src/Domain/Entity/Test/Test.php', $output);
}),
];
}
}
When I try to run this test I get the following warning and test doesn't fail even though it should:
The data provider specified for Tests\Maker\MakeCustomEntityTest::testExecute is invalid.
You have requested a non-existent service "maker.maker.make_custom_entity". Did you mean one of these: "maker.maker.make_authenticator",...
Is this the correct way to testing custom makers? What should I do to avoid this?

Doctrine Persist Data into Database with Console Command

I created the console command but during the execution I get the following error:
In ControllerTrait.php line 338:
Call to a member function has() on null
DatabaseHelper.php
<?php
namespace App\Controller;
use App\Entity\Currency;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Controller\CurrencyComparison;
class DatabaseHelper extends AbstractController
{
public function insertValues()
{
$curFromAPI = new CurrencyComparison();
$curFromAPI = $curFromAPI->compareCurrencies();
$date = new \DateTime('#'.strtotime('now'));
$entityManager = $this->getDoctrine()->getManager();
$currency = new Currency();
$currency->setName('USD');
$currency->setAsk($curFromAPI->getUSD());
$currency->setLastUpdated($date);
$entityManager->persist($currency);
$entityManager->flush();
}
}
CurrencyComparison.php
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Service\DatabaseHelper;
class CurrencyComparison extends Command
{
private $databaseHelper;
public function __construct(DatabaseHelper $databaseHelper){
$this->$databaseHelper = $databaseHelper;
parent::__construct();
}
protected function configure()
{
$this
->setName('app:currency-comparison')
->setDescription('Inserts currencies into the database.')
->setHelp('Compares currencies from APIs and inserts the lower rates into the database.')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->databaseHelper->insertValues();
$output->writeln([
'Currencies are being inserted into the database',
'============',
'',
]);
$output->writeln('Done.');
}
}
When debugging, noticed that I'm getting this error after the following line in DatabaseHelper.php:
$entityManager = $this->getDoctrine()->getManager();
What should I change or should be looking for?
Thanks.
--UPDATE--
I created a Service and tried to inject EntityManager as construct.
App/Service/DatabaseHelper.php
<?php
namespace App\Service;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class DatabaseHelper extends AbstractController
{
private $entityManager;
public function __construct(EntityManager $entityManager){
$this->entityManager = $entityManager;
}
public function insertValues()
{
$curFromAPI = new CurrencyComparison();
$curFromAPI = $curFromAPI->compareCurrencies();
$date = new \DateTime('#'.strtotime('now'));
$this->entityManager = $this->getDoctrine()->getManager();
return dd($curFromAPI);
$currency = new Currency();
$currency->setName('USD');
$currency->setAsk($curFromAPI->getUSD());
$currency->setLastUpdated($date);
$entityManager->persist($currency);
// actually executes the queries (i.e. the INSERT query)
$entityManager->flush();
}
}
and updated Command\CurrencyComparison.php too. But when I'm trying to call execute function on CurrencyComparison.php, I cannot reach its' $this->databaseHelper->insertValues(); function. Would you mind to give any suggestions?
--UPDATE & Solution--
Removed extends AbstractController from class DatabaseHelper.
Changed __construct of DatabaseHelper and injected as follows: (EntityManagerInterface $entityManager)
removed the following line from insertValues function in DatabaseHelper.php:
$this->entityManager = $this->getDoctrine()->getManager(); and just used persist and flush functions as like above.
As your command has a dependency on DatabaseHelper that has a dependency on the Entity Manager, create the DatabaseHelper class as a service, with the Entity manager injected in via its constructor (autowire it, or specify in its service definition).
then, as per the docs here you can inject your DatabaseHelper service into the command via the constructor as it'll it be autowired by default.
I would also consider not extending the AbstractController as your DatabaseHelper is very tight in scope and wont need the entire container (assuming its only what youve posted). Just inject what you need.

Getting clone method called on non-object when specing Symfony command that uses SymfonyStyle for styled output

I'm trying to spec Symfony command and I want to have formated output with SymfonyStyle
<?php
namespace Acme\AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class SomeCommand extends ContainerAwareCommand
{
//....
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->title('Feed import initiated');
}
}
and spec file:
<?php
namespace spec\Acme\AppBundle\Command;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SomeCommandSpec extends ObjectBehavior
{
//...
function it_fetches_social_feeds(
ContainerInterface $container,
InputInterface $input,
OutputInterface $output,
SymfonyStyle $symfonyStyle
) {
// With options
$input->bind(Argument::any())->shouldBeCalled();
$input->hasArgument('command')->shouldBeCalled();
$input->isInteractive()->shouldBeCalled();
$input->validate()->shouldBeCalled();
$symfonyStyle->title(Argument::any())->shouldBeCalled();
$this->setContainer($container);
$this->run($input, $output);
}
}
but I'm getting this error:
exception [err:Error("__clone method called on non-object")] has been thrown.
0 vendor/symfony/symfony/src/Symfony/Component/Console/Style/SymfonyStyle.php:50
throw new PhpSpec\Exception\ErrorException("__clone method called on ...")
1 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:866
Symfony\Component\Console\Command\Command->run([obj:Symfony\Component\Console\Input\ArgvInput], [obj:Symfony\Component\Console\Output\ConsoleOutput])
2 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:193
Symfony\Component\Console\Application->doRunCommand([obj:PhpSpec\Console\Command\RunCommand], [obj:Symfony\Component\Console\Input\ArgvInput], [obj:Symfony\Component\Console\Output\ConsoleOutput])
3 vendor/phpspec/phpspec/src/PhpSpec/Console/Application.php:102
Symfony\Component\Console\Application->doRun([obj:Symfony\Component\Console\Input\ArgvInput], [obj:Symfony\Component\Console\Output\ConsoleOutput])
4 vendor/phpspec/phpspec/bin/phpspec:26
Symfony\Component\Console\Application->run()
5 vendor/phpspec/phpspec/bin/phpspec:28
{closure}("3.2.2")
on line 50 of SymfonyStyle is:
public function __construct(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
/* line 50 */ $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
// Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
$this->lineLength = min($this->getTerminalWidth() - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
parent::__construct($output);
}
and phpspec is complainig about
clone $output->getFormatter()
Am I doing something wrong, or am I missing something?
Update
this is my let method:
function let(SymfonyStyle $symfonyStyle, InputInterface $input, OutputInterface $output)
{
$symfonyStyle->beConstructedWith([$input->getWrappedObject(), $output->getWrappedObject()]);
}
You are missing this
function it_fetches_social_feeds(
ContainerInterface $container,
InputInterface $input,
OutputInterface $output,
SymfonyStyle $symfonyStyle
) {
// .... other code
$prophet = new Prophet();
$formatter = $prophet->prophesize(OutputFormatterInterface::class);
$output->getFormatter()->willReturn($formatter);
// .... more code
}
You can place it where you want but before the call is done.
In that way you have created a Stub that is basically a Double with behavior and without expectations. You can see it as a "proxy" that will intercept method calls and returns what you "teach" it to return.
In your example things get broken as your double OutputInterface would return null as it's not a "real" object.
I also suggest to stub the getVerbosity behavior if you need to do different kind of specs.
BTW you can read more about doubles in phpspec guide and prophecy guide
Looking at an example, SymfonyStyle never seems to be passed in the spec class.
The execute function of the command doesn't pass it either, just the input and output classes (the style class it created as a local variable).

Symfony Console ContainerAwareCommand $this->getContainer() produces Fatal error

TL;DR
I'm creating console Garbage collector, which should be able to get services from container.
It's basic, almost straight from the manual:
<?php
namespace SomeBundle\Console\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand,
Symfony\Component\Console\Input\InputArgument,
Symfony\Component\Console\Input\InputInterface,
Symfony\Component\Console\Input\InputOption,
Symfony\Component\Console\Output\OutputInterface;
class GarbageCollector extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('garbage:collect')
->setDescription('Collect garbage')
->addArgument(
'task',
InputArgument::REQUIRED,
'What task to execute?'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$task = $input->getArgument('task');
//...
$_container = $this->getContainer();
}
}
Then I'm trying to call it from console via application.php:
#!/usr/bin/env php
<?php
// application.php
require_once __DIR__.'/../vendor/autoload.php';
use SomeBundle\Console\Command\GarbageCollector;
use Symfony\Bundle\FrameworkBundle\Console\Application;
$application = new Application();
$application->add(new GarbageCollector);
$application->run();
Which produces fatal error:
Argument 1 passed to
Symfony\Bundle\FrameworkBundle\Console\Application::__construct() must
implement interface Symfony\Component\HttpKernel\Kernel Interface,
none given
Manual says that only thing I need to do is to extend my class with ContainerAwareCommand, but something missing. I've wrote some crap code to pass Kernel to the Application():
#!/usr/bin/env php
<?php
// application.php
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/AppKernel.php';
use SomeBundle\Console\Command\GarbageCollector;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->add(new GarbageCollector);
$application->run();
And it works, but feels disgusting.
What do I need to make ContainerAwareCommand implementation of console app? Thanks in advance.
Symfony2 provides via its console runner the ability to call your custom commands and it will handle the injection of the service container. All you need to do is create your command in a registered bundle and it will automatically become available to you. You are initializing your command outside of the framework that is why the container is not available to you unless you manually inject it.
You can reference the cookbook here:
http://symfony.com/doc/current/cookbook/console/console_command.html
You can get a list of all the commands available by running:
php app/console
On a side note, why are you creating a garbage collector in/for PHP? Just curious because to my understanding memory is freed for you after script execution has ended.
As by version 4.1 you can inject the container service at the constructor.
namespace App\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyCommand extends Command {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
parent::__construct();
}
protected function configure() {
// ...
}
protected function execute(InputInterface $input, OutputInterface $output) {
// You must make Some\Service\Class public.
// Documentation at https://symfony.com/doc/current/service_container.html#public-versus-private-services
$some_service = $this->container->get('Some\Service\Class');
}
}

Send email from command line in symfony2

I need to render a twig template from a command class in symfony2.
namespace IT\bBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class CronCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('send:emails')
->setDescription('Envio programado de emails');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$message = \Swift_Message::newInstance()
->setSubject('bla bla')
->setFrom('x#x.com')
->setTo('x#gmail.com')
->setCharset('UTF-8')
->setContentType('text/html')
->setBody($this->renderView('mainBundle:Email:default.html.twig'));
$this->getContainer()->get('mailer')->send($message);
$output->writeln('Enviado!');
}
}
But when I execute the command php app/console send:emails I get the following error:
Fatal error: Call to undefined method IT\bBundle\Command\CronCommand::renderView()
How can I render the view?
It's because renderView is method of class Controller. Instead of that try:
$this->getContainer()->get('templating')->render(...);
Change
$this->renderView()
to
$this->getContainer()->get('templating')->render()
Maybe, not exactly the question you ask, but for sure - important.
Please, do remember that if you want to send emails via Command call, you need to flushQueue.
$mailer = $container->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');
$spool->flushQueue($transport);

Categories