我向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');
///....
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)