1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112:
<?php
namespace Guzzle\Service\Resource;
use Guzzle\Common\AbstractHasDispatcher;
use Guzzle\Batch\BatchBuilder;
use Guzzle\Batch\BatchSizeDivisor;
use Guzzle\Batch\BatchClosureTransfer;
use Guzzle\Common\Version;
class ResourceIteratorApplyBatched extends AbstractHasDispatcher
{
protected $callback;
protected $iterator;
protected $batches = 0;
protected $iterated = 0;
public static function getAllEvents()
{
return array(
'iterator_batch.before_batch',
'iterator_batch.after_batch',
'iterator_batch.created_batch'
);
}
public function __construct(ResourceIteratorInterface $iterator, $callback)
{
$this->iterator = $iterator;
$this->callback = $callback;
Version::warn(__CLASS__ . ' is deprecated');
}
public function apply($perBatch = 50)
{
$this->iterated = $this->batches = $batches = 0;
$that = $this;
$it = $this->iterator;
$callback = $this->callback;
$batch = BatchBuilder::factory()
->createBatchesWith(new BatchSizeDivisor($perBatch))
->transferWith(new BatchClosureTransfer(function (array $batch) use ($that, $callback, &$batches, $it) {
$batches++;
$that->dispatch('iterator_batch.before_batch', array('iterator' => $it, 'batch' => $batch));
call_user_func_array($callback, array($it, $batch));
$that->dispatch('iterator_batch.after_batch', array('iterator' => $it, 'batch' => $batch));
}))
->autoFlushAt($perBatch)
->build();
$this->dispatch('iterator_batch.created_batch', array('batch' => $batch));
foreach ($this->iterator as $resource) {
$this->iterated++;
$batch->add($resource);
}
$batch->flush();
$this->batches = $batches;
return $this->iterated;
}
public function getBatchCount()
{
return $this->batches;
}
public function getIteratedCount()
{
return $this->iterated;
}
}