我正在使用令人敬畏的Faker库生成大量的数据装置,也使用lorempixel.com在我的Symfony2项目中有一些随机图像。这需要一些时间(目前~ 10分钟),所以我想知道是否有可能通过容器接口以某种方式访问命令输出接口并以这种方式打印进度,而不是echo'ing
一切。
也可能有一个很好的输出与ProgressBar
看起来ConsoleOutput不需要任何特殊的东西,可以直接实例化。
use Symfony'Component'Console'Output'ConsoleOutput;
// ...
public function load(ObjectManager $manager)
{
$output = new ConsoleOutput();
$output->writeln('<info>this works... </info>');
}
如果您使用ConsoleEvents::COMMAND
事件获得"原始" $output
对象,可能会有更好的解决方案。
namespace App'DoctrineFixtures;
use Doctrine'Common'DataFixtures'AbstractFixture;
use Doctrine'Common'Persistence'ObjectManager;
use Symfony'Component'Console'ConsoleEvents;
use Symfony'Component'Console'Event'ConsoleCommandEvent;
use Symfony'Component'Console'Helper'ProgressBar;
use Symfony'Component'Console'Output'OutputInterface;
use Symfony'Component'EventDispatcher'EventSubscriberInterface;
class CustomFixture extends AbstractFixture implements EventSubscriberInterface
{
/** @var OutputInterface */
private $output;
/** @var Command */
private $command;
public static function getSubscribedEvents()
{
return [
ConsoleEvents::COMMAND => 'init',
];
}
public function init(ConsoleCommandEvent $event): void
{
$this->output = $event->getOutput();
$this->command = $event->getCommand();
}
public function load(ObjectManager $manager)
{
// ...
$this->output->writeln('...');
// ...
$tableHelper = $this->command->getHelper('table');
// ...
$progressBar = new ProgressBar($this->output, 50);
// ...
}
}
In services.yml
:
services:
App'DoctrineFixtures'CustomFixture:
tags:
- 'doctrine.fixture.orm'
- 'kernel.event_subscriber'