2015-03-11 05:11:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is part of the FOSElasticaBundle project.
|
|
|
|
*
|
|
|
|
* (c) Tim Nagel <tim@nagel.com.au>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace FOS\ElasticaBundle\Command;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Helper\ProgressBar;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
class ProgressClosureBuilder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Builds a loggerClosure to be called from inside the Provider to update the command
|
|
|
|
* line.
|
|
|
|
*
|
|
|
|
* @param OutputInterface $output
|
|
|
|
* @param string $action
|
|
|
|
* @param string $index
|
|
|
|
* @param string $type
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
public function build(OutputInterface $output, $action, $index, $type)
|
|
|
|
{
|
2015-03-11 05:17:06 +01:00
|
|
|
if (!class_exists('Symfony\Component\Console\Helper\ProgressBar') ||
|
|
|
|
!is_callable(array('Symfony\Component\Console\Helper\ProgressBar', 'getProgress'))) {
|
2015-03-11 05:11:42 +01:00
|
|
|
return $this->buildLegacy($output, $action, $index, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$progress = null;
|
|
|
|
|
2015-03-11 11:31:23 +01:00
|
|
|
return function ($increment, $totalObjects, $message = null) use (&$progress, $output, $action, $index, $type) {
|
2015-03-11 05:11:42 +01:00
|
|
|
if (null === $progress) {
|
|
|
|
$progress = new ProgressBar($output, $totalObjects);
|
|
|
|
$progress->start();
|
|
|
|
}
|
|
|
|
|
2015-03-11 11:31:23 +01:00
|
|
|
if (null !== $message) {
|
|
|
|
$progress->clear();
|
|
|
|
$output->writeln(sprintf('<info>%s</info> <error>%s</error>', $action, $message));
|
|
|
|
$progress->display();
|
|
|
|
}
|
|
|
|
|
2015-03-11 05:11:42 +01:00
|
|
|
$progress->setMessage(sprintf('<info>%s</info> <comment>%s/%s</comment>', $action, $index, $type));
|
|
|
|
$progress->advance($increment);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a legacy closure that outputs lines for each step. Used in cases
|
|
|
|
* where the ProgressBar component doesnt exist or does not have the correct
|
|
|
|
* methods to support what we need.
|
|
|
|
*
|
|
|
|
* @param OutputInterface $output
|
|
|
|
* @param string $action
|
|
|
|
* @param string $index
|
|
|
|
* @param string $type
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
private function buildLegacy(OutputInterface $output, $action, $index, $type)
|
|
|
|
{
|
|
|
|
$lastStep = null;
|
|
|
|
$current = 0;
|
|
|
|
|
2015-03-11 11:31:23 +01:00
|
|
|
return function ($increment, $totalObjects, $message = null) use ($output, $action, $index, $type, &$lastStep, &$current) {
|
2015-03-11 05:11:42 +01:00
|
|
|
if ($current + $increment > $totalObjects) {
|
|
|
|
$increment = $totalObjects - $current;
|
|
|
|
}
|
|
|
|
|
2015-03-11 11:31:23 +01:00
|
|
|
if (null !== $message) {
|
|
|
|
$output->writeln(sprintf('<info>%s</info> <error>%s</error>', $action, $message));
|
|
|
|
}
|
|
|
|
|
2015-03-11 05:11:42 +01:00
|
|
|
$currentTime = microtime(true);
|
|
|
|
$timeDifference = $currentTime - $lastStep;
|
|
|
|
$objectsPerSecond = $lastStep ? ($increment / $timeDifference) : $increment;
|
|
|
|
$lastStep = $currentTime;
|
|
|
|
$current += $increment;
|
|
|
|
$percent = 100 * $current / $totalObjects;
|
|
|
|
|
|
|
|
$output->writeln(sprintf(
|
|
|
|
'<info>%s</info> <comment>%s/%s</comment> %0.1f%% (%d/%d), %d objects/s (RAM: current=%uMo peak=%uMo)',
|
|
|
|
$action,
|
|
|
|
$index,
|
|
|
|
$type,
|
|
|
|
$percent,
|
|
|
|
$current,
|
|
|
|
$totalObjects,
|
|
|
|
$objectsPerSecond,
|
|
|
|
round(memory_get_usage() / (1024 * 1024)),
|
|
|
|
round(memory_get_peak_usage() / (1024 * 1024))
|
|
|
|
));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|