2015-08-27 116 views
5

我正在尝试创建一个symfony控制台命令来执行一个端点。Symfony 2 - 从控制台命令调用控制器方法

BillingBundle /命令/ 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 /服务/ 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应用程序/控制台的cron这样:rejectLines

它的工作原理。

这就是我想要达到的。

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

然而,由于SalesOrderService类有一些参数传递给__construct,我不太清楚,通过命令行调用时,我如何能够通过。任何想法 ?

+0

您是否尝试过在命令行运行呢?什么是输出? –

回答

2

您不需要从命令行传递参数。感谢DI容器,您需要您的服务来获取其参数。

我看你的控制器上的InjectParams注释,所以我估计你正在使用JMSDiExtraBundle。在这种情况下,您可以将您的服务/控制器上注入参数(像你这样),太暴露它作为

<?php 

use JMS\DiExtraBundle\Annotation\Service; 

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

服务现在你可以使用你的命令的ContainerAwareCommand方法和使用的容器,让您(完全注入)与

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

SalesOrderService服务是其生命周期是由Symfony2的容器由依赖注入系统管理的服务。 所以,你可能会发现其服务类定义声明的名称(您正在使用的di-extra-bundle)这样:

  1. 检查类名称注记的服务定义的名称。作为例子,这样的:

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

希望这有助于