2015-08-27 11 views
5

Bir bitiş noktası yürütmek için bir symfony konsol komutu oluşturmaya çalışıyorum.Symfony 2 - Bir denetleyici yöntemini konsol komutundan çağırılıyor

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

Ben

BillingBundle/Hizmetler/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 
} 
arayıp uç nokta

tanımlanan çalışıyorum

POSTman kullanarak son nokta/reddetme satırları aradığımda bu iyi çalışır. Ancak, ben bu yüzden

php app/konsol cron çağırdığınızda o konsol komutundan diyorsunuz nasıl emin değilim: rejectLines

çalışıyor.

Bu benim elde etmek istediğim şeydir. SalesOrderService sınıf __construct geçirilen bazı argümanları çünkü

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

Ancak, ben komut üzerinden arama yaparken bunu geçebilir nasıl emin değilim. Herhangi bir fikir ?

+0

Eğer CLI çalıştırmak için denediniz bu yardım? Çıktı nedir? –

cevap

2

Komut satırından bağımsız değişkenleri iletmeniz gerekmez. DI kabı sayesinde parametrelerini enjekte etmek için servisinize ihtiyacınız var.

Denetleyicinizde InjectParams ek açıklamasını görüyorum, böylece JMSDiExtraBundle kullanıyorsunuz. Bu durumda servis/denetleyici üzerinde parametreleri enjekte edebilir (senin yaptığın gibi) ve

<?php 

use JMS\DiExtraBundle\Annotation\Service; 

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

ile bir hizmet olarak çok maruz Artık Komutanlığı ContainerAwareCommand yöntemi kullanmak ve sizin (almak için kap kullanabilirsiniz

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

Sizin SalesOrderService ile tam enjekte) servis yaşam döngüsü Bağımlılık enjeksiyon sistemi tarafından Symfony2 konteyner tarafından yönetilen bir hizmettir. Yani muhtemelen bu yüzden (eğer di-extra-bundle kullandığınız) hizmeti sınıfı tanımında bildirildi adını bulmak:

  1. sınıf adı açıklama hizmet tanımının adını kontrol edin. Örnek olarak, böyle bir şey: konsol komutu kaba

    /** 
    * @Service("salesorder.service.id") 
    */ 
    class SalesOrderService 
    
  2. Request:

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

Umut

İlgili konular