[英]$this->getContainer() is returning null in Symfony 3.x for Command
[英]Symfony Console ContainerAwareCommand $this->getContainer() produces Fatal error
TL; DR
我正在創建控制台垃圾收集器,它應該能夠從容器中獲取服務。 它是基本的,幾乎與手冊完全一樣:
<?php
namespace SomeBundle\Console\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand,
Symfony\Component\Console\Input\InputArgument,
Symfony\Component\Console\Input\InputInterface,
Symfony\Component\Console\Input\InputOption,
Symfony\Component\Console\Output\OutputInterface;
class GarbageCollector extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('garbage:collect')
->setDescription('Collect garbage')
->addArgument(
'task',
InputArgument::REQUIRED,
'What task to execute?'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$task = $input->getArgument('task');
//...
$_container = $this->getContainer();
}
}
然后我試圖通過application.php
從控制台調用它:
#!/usr/bin/env php
<?php
// application.php
require_once __DIR__.'/../vendor/autoload.php';
use SomeBundle\Console\Command\GarbageCollector;
use Symfony\Bundle\FrameworkBundle\Console\Application;
$application = new Application();
$application->add(new GarbageCollector);
$application->run();
產生致命錯誤:
傳遞給Symfony \\ Bundle \\ FrameworkBundle \\ Console \\ Application :: __ construct()的參數1必須實現接口Symfony \\ Component \\ HttpKernel \\ Kernel接口,未給出
手冊說,我唯一需要做的就是用ContainerAwareCommand
擴展我的類,但是缺少一些東西。 我寫了一些廢話代碼,將Kernel
傳遞給Application()
:
#!/usr/bin/env php
<?php
// application.php
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/AppKernel.php';
use SomeBundle\Console\Command\GarbageCollector;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->add(new GarbageCollector);
$application->run();
它有效,但令人惡心。
我需要什么來使ContainerAwareCommand
實現控制台應用程序? 提前致謝。
Symfony2通過其控制台運行程序提供了調用自定義命令的功能,它將處理服務容器的注入。 您需要做的就是在已注冊的捆綁軟件中創建命令,該命令將自動變為可用狀態。 您正在框架外部初始化命令,這就是除非您手動注入容器,否則容器不可用的原因。
您可以在此處參考食譜:
http://symfony.com/doc/current/cookbook/console/console_command.html
您可以通過運行以下命令獲取所有可用命令的列表:
php app/console
附帶說明一下,為什么要在PHP中/為PHP創建垃圾收集器? 只是好奇,因為據我所知,腳本執行結束后會為您釋放內存。
與4.1版一樣,您可以在構造函數中注入容器服務。
namespace App\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyCommand extends Command {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
parent::__construct();
}
protected function configure() {
// ...
}
protected function execute(InputInterface $input, OutputInterface $output) {
// You must make Some\Service\Class public.
// Documentation at https://symfony.com/doc/current/service_container.html#public-versus-private-services
$some_service = $this->container->get('Some\Service\Class');
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.