7

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 ?

1
  • Have you tried to run it in CLI? What is the output? Commented Aug 27, 2015 at 17:17

2 Answers 2

2

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');
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.