我正在尝试创建一个symfony控制台命令来执行一个终点.
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");
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我正在尝试调用和端点定义
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
}
Run Code Online (Sandbox Code Playgroud)
当我使用POSTman调用终点/ rejectLines时,这很好.但是,我不太确定如何从控制台命令调用,以便在我打电话时
php app/console cron:rejectLines
有用.
这就是我想要实现的目标.
$cron = new SalesOrderService();
$cron->rejectSalesOrderLines();
Run Code Online (Sandbox Code Playgroud)
但是,因为SalesOrderService类有一些参数传递给__construct,所以我不太确定在通过命令行调用时如何传递它.任何的想法 ?
您不需要从命令行传递参数。由于 DI 容器,您需要您的服务来注入其参数。
我在你的控制器上看到InjectParams注释,所以我认为你正在使用JMSDiExtraBundle. 在这种情况下,您可以在服务/控制器上注入参数(就像您所做的那样)并将其也作为服务公开
<?php
use JMS\DiExtraBundle\Annotation\Service;
/**
* @Service("some.service.id")
*/
class SalesOrderService
{
....
}
Run Code Online (Sandbox Code Playgroud)
现在您可以使用ContainerAwareCommand命令的方法并使用容器来获取(完全注入的)服务
$yourService = $this->getContainer()->get('some.service.id');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4827 次 |
| 最近记录: |