[英]Symfony2 console HelperSet
我正在使用Symfony2组件制作应用程序,但我陷入了symfony控制台的困境。 问题是我初始化控制台
$objectRepository = ObjectRepository::getInstance();
$console = $objectRepository->get('console');
if ( ! $console instanceof \Symfony\Component\Console\Application) {
echo 'Failed to initialize console.' . PHP_EOL;
}
$helperSet = $console->getHelperSet();
$helperSet->set(new EntityManagerHelper($objectRepository->get('entity_manager')), 'em');
$console->run();
我有教义创建命令别名是
namespace My\Console\Command;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand as BaseCommand;
class CreateCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('doctrine:schema:update');
}
}
Doctrine \\ ORM \\ Tools \\ Console \\ Command \\ SchemaTool \\ CreateCommand使用的是em帮助程序,问题出在Symfony \\ Component \\ Console \\ Application doRun()方法中
$command = $this->find($name);
$this->runningCommand = $command;
$statusCode = $command->run($input, $output);
应用程序在HelperSet中保留3个助手(对话框,格式,entityManager和em(em是EntityManager的别名))。 找到命令后,命令不会继承应用程序帮助程序集,并且仅具有默认对话框和格式帮助程序。
我有扩展symfony默认Application类并重写doRun()方法的解决方案,但这不是最好的方法。
看起来应用程序和命令可以具有不同的帮助程序集,所以我解决了
namespace My\Console\Command;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand as BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CreateCommand extends BaseCommand
{
protected function configure()
{
parent::configure();
$this->setName('doctrine:schema:create');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setHelperSet($this->getApplication()->getHelperSet());
parent::execute($input, $output);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.