gpt4 book ai didi

symfony - 如何在命令类之外获取命令参数?

转载 作者:行者123 更新时间:2023-12-05 07:57:35 24 4
gpt4 key购买 nike

我在 doctrine:fixtures:load 命令中添加了自定义选项。现在我想知道如何在自定义灯具类中获取此命令选项:

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 并设法添加了一个额外的参数,那么您可以将该值设置为容器参数。

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

然后在您的设备类中获取该容器参数。

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');

///....
}
}

关于symfony - 如何在命令类之外获取命令参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26596218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com