如何在命令类之外获取命令参数? [英] How to get command argument outside command class?

查看:53
本文介绍了如何在命令类之外获取命令参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在doctrine:fixtures:load命令中添加了自定义选项.现在,我想知道如何在自定义装置类中获取此命令选项:

I added custom option to doctrine:fixtures:load command. Now I am wonder how to get this command option in custom fixtures class:

class LoadUserData implements FixtureInterface, ContainerAwareInterface {

  private $container;
  /**
   * {@inheritDoc}
   */
  public function load(ObjectManager $manager) {

  }

  public function setContainer(ContainerInterface $container = null) {
    $this->container = $container;
  }
}

有什么建议吗?

推荐答案

因此,如果您扩展了 Doctrine \ Bundle \ FixturesBundle \ Command \ LoadDataFixturesDoctrineCommand 并成功添加了其他参数,则可以进行设置该值作为容器参数传递.

So if you have extended Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand and managed to add a additional argument, then you can set that value passed as a container parameter.

<?php

namespace Your\Bundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand;

class LoadDataFixturesCommand extends LoadDataFixturesDoctrineCommand
{
    /**
     * {@inheritDoc}
     */
    protected function configure()
    {
        parent::configure();

        $this->addOption('custom-option', null, InputOption::VALUE_OPTIONAL, 'Your Custom Option');
    }

    /**
     * {@inheritDoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->getContainer()->setParameter('custom-option', $input->getOption('custom-option'));

        parent::execute($input, $output);
    }
}

然后在您的装置类中获取该容器参数.

Then get that container parameter in your fixtures class.

class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
    /**
     * If you have PHP 5.4 or greater, you can use this trait to implement ContainerAwareInterface
     *
     * @link https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/ContainerAwareTrait.php
     */
    use \Symfony\Component\DependencyInjection\ContainerAwareTrait;

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $customOption = $this->container->getParameter('custom-option');

        ///....
    }
}

这篇关于如何在命令类之外获取命令参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆