Neos\Media\Domain\Repository\AssetRepository::iterate PHP 메소드

iterate() 공개 메소드

This method is useful for batch processing huge result set as it clears the object manager and detaches the current object on each iteration.
public iterate ( Doctrine\ORM\Internal\Hydration\IterableResult $iterator, callable $callback = null ) : Generator
$iterator Doctrine\ORM\Internal\Hydration\IterableResult
$callback callable
리턴 Generator
    public function iterate(IterableResult $iterator, callable $callback = null)
    {
        $iteration = 0;
        foreach ($iterator as $object) {
            $object = current($object);
            (yield $object);
            if ($callback !== null) {
                call_user_func($callback, $iteration, $object);
            }
            $iteration++;
        }
    }

Usage Example

 /**
  * Create thumbnails
  *
  * Creates thumbnail images based on the configured thumbnail presets. Optional ``preset`` parameter to only create
  * thumbnails for a specific thumbnail preset configuration.
  *
  * Additionally accepts a ``async`` parameter determining if the created thumbnails are generated when created.
  *
  * @param string $preset Preset name, if not provided thumbnails are created for all presets
  * @param boolean $async Asynchronous generation, if not provided the setting ``Neos.Media.asyncThumbnails`` is used
  * @return void
  */
 public function createThumbnailsCommand($preset = null, $async = null)
 {
     $async = $async !== null ? $async : $this->asyncThumbnails;
     $presets = $preset !== null ? [$preset] : array_keys($this->thumbnailService->getPresets());
     $presetThumbnailConfigurations = [];
     foreach ($presets as $preset) {
         $presetThumbnailConfigurations[] = $this->thumbnailService->getThumbnailConfigurationForPreset($preset, $async);
     }
     $iterator = $this->assetRepository->findAllIterator();
     $imageCount = $this->assetRepository->countAll();
     $this->output->progressStart($imageCount * count($presetThumbnailConfigurations));
     foreach ($this->assetRepository->iterate($iterator) as $image) {
         foreach ($presetThumbnailConfigurations as $presetThumbnailConfiguration) {
             $this->thumbnailService->getThumbnail($image, $presetThumbnailConfiguration);
             $this->persistenceManager->persistAll();
             $this->output->progressAdvance(1);
         }
     }
 }