Symfony 2 - 从控制台命令调用控制器方法

Symfony 2 - Calling a controller method from console command

我正在尝试创建一个 symfony 控制台命令来执行一个端点。

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");
    }
}
?>

我正在尝试调用

中定义的端点

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
}

当我使用 POSTman 调用终点 /rejectLines 时,这工作正常。但是,我不太确定如何从控制台命令调用,以便在调用

php app/console cron:rejectLines

有效。

这就是我想要实现的。

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

但是,因为 SalesOrderService class 有一些参数传递给 __construct,我不太确定在通过命令行调用时如何传递它。有什么想法吗?

您不需要从命令行传递参数。多亏了 DI 容器,你需要你的服务来注入它的参数。

我在你的控制器上看到 InjectParams 注释,所以我认为你正在使用 JMSDiExtraBundle。在这种情况下,您可以在 service/controller 上注入参数(就像您所做的那样)并将其作为服务公开

<?php

use JMS\DiExtraBundle\Annotation\Service;

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

现在您可以使用您的命令的 ContainerAwareCommand 方法并使用容器来获取您的(完全注入的)服务

$yourService = $this->getContainer()->get('some.service.id');

您的 SalesOrderService 是一个服务,其生命周期由依赖注入系统的 symfony2 容器管理。 所以你可能会在 class 定义中找到服务声明的名称(你正在使用 di-extra-bundle)所以:

  1. 检查 class 名称注释中的服务定义名称。例如,类似于:

    /**
    *  @Service("salesorder.service.id")
    */
    class SalesOrderService
    
  2. 在控制台命令中向容器请求:

    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");
    }
    

希望对您有所帮助