dongmo20030416 2014-11-20 19:05
浏览 107
已采纳

Symfony控制台ContainerAwareCommand $ this-> getContainer()产生致命错误

TL;DR

I'm creating console Garbage collector, which should be able to get services from container. It's basic, almost straight from the manual:

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

Then I'm trying to call it from console via 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();

Which produces fatal error:

Argument 1 passed to Symfony\Bundle\FrameworkBundle\Console\Application::__construct() must implement interface Symfony\Component\HttpKernel\Kernel Interface, none given

Manual says that only thing I need to do is to extend my class with ContainerAwareCommand, but something missing. I've wrote some crap code to pass Kernel to the 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();

And it works, but feels disgusting.

What do I need to make ContainerAwareCommand implementation of console app? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • doushizhou4477 2014-11-20 19:23
    关注

    Symfony2 provides via its console runner the ability to call your custom commands and it will handle the injection of the service container. All you need to do is create your command in a registered bundle and it will automatically become available to you. You are initializing your command outside of the framework that is why the container is not available to you unless you manually inject it.

    You can reference the cookbook here:

    http://symfony.com/doc/current/cookbook/console/console_command.html

    You can get a list of all the commands available by running:

    php app/console 
    

    On a side note, why are you creating a garbage collector in/for PHP? Just curious because to my understanding memory is freed for you after script execution has ended.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。