Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 - Calling a controller method from console command

Tags:

php

cron

symfony

I am trying to create a symfony console command to execute an end point.

BillingBundle/Command/RejectCommand.php

<?php
namespace BillingBundle\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 RejectCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('cron:rejectLines')
            ->setDescription('Executes the RejectLines cron');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln("Starting the cron");

       // Call the method here

        $output->writeln("Cron completed");
    }
}
?>

I am trying to call and endpoint defined in

BillingBundle/Services/SalesOrderService.php

/**
     * @InjectParams({
     *      "repository" = @Inject("billing.repository.sales_order"),
     *      "sapInterface" = @Inject("external.sap_sales_order_interface"),
     *      "articleService" = @Inject("stream_one.product.interface.solution_store_product_service"),
     *      "logger" = @Inject("logger")
     * })
     */
    public function __construct(SalesOrderRepositoryInterface $repository, SapSalesOrderInterface $sapInterface, ArticleService $articleService, Logger $logger) {
        $this->repository = $repository;
        $this->sapInterface = $sapInterface;
        $this->articleService = $articleService;
        $this->logger = $logger;
    }


/**
     * CRON: Reject lines
     *
     * @Post("/rejectLines", name="reject_lines_post")
     */
    public function rejectSalesOrderLines() {

// do some stuff and quit silently
}

This works fine when I call the end point /rejectLines using POSTman. However, I am not quite sure how do I call from console command so that when I call

php app/console cron:rejectLines

it works.

This is what I wanna achieve.

$cron = new SalesOrderService();
$cron->rejectSalesOrderLines();

However, because SalesOrderService class has some arguments passed to the __construct, I am not quite sure how I can pass it when calling through the commandline. Any idea ?

like image 675
zeroweb Avatar asked Aug 27 '15 16:08

zeroweb


2 Answers

You do not need to pass arguments from command-line. You need your service to get its parameters injected thanks to the DI container.

I see InjectParams annotation on your controller so I reckon you are using JMSDiExtraBundle. In this case you can inject parameters on your service/controller (like you did) and expose it too as a service with

<?php

use JMS\DiExtraBundle\Annotation\Service;

/**
 * @Service("some.service.id")
 */
class SalesOrderService
{
    ....
}

Now you can use the ContainerAwareCommand method of your Command and use the container to get your (fully injected) Service with

$yourService = $this->getContainer()->get('some.service.id');
like image 118
Diego Ferri Avatar answered Nov 02 '22 01:11

Diego Ferri


Your SalesOrderService is a service which life-cycle is managed by the symfony2 container by the Dependency injection system. So you probably find the name which the service is declared in the class definition (you are using the di-extra-bundle) so:

  1. Check the name of the service definition in the class name annotation. As example, something like:

    /**
    *  @Service("salesorder.service.id")
    */
    class SalesOrderService
    
  2. Request to the container in the console command:

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln("Starting the cron");
    
       // Call the method here
       $service = $this->getContainer()->get('salesorder.service.id');
       $service-> rejectSalesOrderLines()
        $output->writeln("Cron completed");
    }
    

Hope this help

like image 23
Matteo Avatar answered Nov 02 '22 02:11

Matteo