Skip to content

Commit

Permalink
Merge pull request #98 from AydinHassan/console-redraw
Browse files Browse the repository at this point in the history
Provide option for setting redraw frequency.
  • Loading branch information
ddeboer committed Jul 22, 2014
2 parents 0fe6fc1 + 91e0b60 commit 4b97b3e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,32 @@ $progressWriter = new ConsoleProgressWriter($output, $reader);

// Most useful when added to a workflow
$workflow->addWriter($progressWriter);

```

There are various optional arguments you can pass to the `ConsoleProgressWriter`. These include the output format and
the redraw frequency. You can read more about the options [here](http://symfony.com/doc/current/components/console/helpers/progressbar.html).

You might want to set the redraw rate higher than the default as it can slow down the import/export process quite a bit
as it will update the console text after every record has been processed by the `Workflow`.

```php
$output = new ConsoleOutput(...);
$progressWriter = new ConsoleProgressWriter($output, $reader, 'debug', 100);
```

Above we set the output format to 'debug' and the redraw rate to 100. This will only re-draw the console progress text
after every 100 records.

The `debug` format is default as it displays ETA's and Memory Usage. You can use a more simple formatter if you wish:

```php
$output = new ConsoleOutput(...);
$progressWriter = new ConsoleProgressWriter($output, $reader, 'normal', 100);
```



#### CallbackWriter

Instead of implementing your own writer, you can use the quick solution the
Expand Down
66 changes: 60 additions & 6 deletions src/Ddeboer/DataImport/Writer/ConsoleProgressWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,96 @@
*/
class ConsoleProgressWriter extends AbstractWriter
{
/**
* @var OutputInterface
*/
protected $output;

/**
* @var ProgressBar
*/
protected $progress;
protected $workflow;

public function __construct(OutputInterface $output, ReaderInterface $reader, $verbosity = 'debug')
{
$this->output = $output;
$this->reader = $reader;
$this->verbosity = $verbosity;
/**
* @var string
*/
protected $verbosity;

/**
* @var ReaderInterface
*/
protected $reader;

/**
* @var int
*/
protected $redrawFrequency;

/**
* @param OutputInterface $output
* @param ReaderInterface $reader
* @param string $verbosity
* @param int $redrawFrequency
*/
public function __construct(
OutputInterface $output,
ReaderInterface $reader,
$verbosity = 'debug',
$redrawFrequency = 1
) {
$this->output = $output;
$this->reader = $reader;
$this->verbosity = $verbosity;
$this->redrawFrequency = $redrawFrequency;
}

/**
* @return $this
*/
public function prepare()
{
$this->progress = new ProgressBar($this->output, $this->reader->count());
$this->progress->setFormat($this->verbosity);
$this->progress->setRedrawFrequency($this->redrawFrequency);
$this->progress->start();

return $this;
}

/**
* @param array $item
* @return $this
*/
public function writeItem(array $item)
{
$this->progress->advance();

return $this;
}

/**
* @return $this
*/
public function finish()
{
$this->progress->finish();

return $this;
}

/**
* @return string
*/
public function getVerbosity()
{
return $this->verbosity;
}

/**
* @return int
*/
public function getRedrawFrequency()
{
return $this->redrawFrequency;
}
}

0 comments on commit 4b97b3e

Please sign in to comment.