public function testTransformOnce()
 {
     $count = 0;
     $transformer = new CallbackTransformer(function () use($count) {
         return new StringResource('bar' . ++$count);
     });
     $resource = new StringResource('foo');
     $collection = new ResourceCollection([$resource]);
     $collection->addTransformer($transformer);
     $transformed = $collection->pop();
     $this->assertSame('bar1', $transformed->getFile()->fgets());
     // add the same resource again
     $collection->push($resource);
     $transformed = $collection->pop();
     $this->assertSame('foo', $transformed->getFile()->fgets(), 'A resource only gets transformed once');
 }
Exemplo n.º 2
0
 /**
  * @inheritdoc
  */
 public function transform(ResourceInterface $resource, ResourceCollection $collection)
 {
     // break up again
     $files = $this->breakup($resource);
     $resources = [];
     foreach ($files as $file) {
         $transport = FileTransport::create($file);
         $transport->setDestination($file);
         $resources[] = new FileResource($transport);
         if ($this->maxParts > 0 && sizeof($resources) >= $this->maxParts) {
             break;
         }
     }
     $collection->unshiftAll($resources);
     return $collection->shift();
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 public function transform(ResourceInterface $resource, ResourceCollection $collection)
 {
     if ($this->needsUnzipping($resource)) {
         $this->unzip($resource);
     }
     $resources = [];
     foreach ($this->files as $file) {
         $targetFile = $this->getTargetFile($resource, $file);
         if (!file_exists($targetFile)) {
             throw new TransportException(sprintf('File "%s" was not found in the archive', $targetFile));
         }
         $transport = FileTransport::create($targetFile);
         $transport->setDestinationDir($this->getTargetDir($resource));
         $resources[] = new FileResource($transport);
     }
     $collection->unshiftAll($resources);
     return $collection->shift();
 }
Exemplo n.º 4
0
 /**
  *
  */
 protected function initialize()
 {
     if ($this->initialized) {
         return;
     }
     // mark initialized first, to prevent recursive calls
     $this->initialized = true;
     $this->resources->rewind();
     $this->createNextReader();
 }
Exemplo n.º 5
0
 /**
  * Tests that unzipping is not skipped when target exists but is older than zip.
  */
 public function testDontSkipUnzip()
 {
     $transformer = $this->getMockBuilder(UnzipTransformer::class)->setMethods(['unzip'])->setConstructorArgs([[$this->filename], __DIR__])->getMock();
     $this->collection->addTransformer($transformer);
     // put the to be extracted file in the target dir
     file_put_contents(__DIR__ . '/' . $this->filename, $this->fileContents);
     // wait 1 second and touch the zip file
     sleep(1);
     touch($this->zipFilename);
     // unzip should be called when transforming
     $transformer->expects($this->once())->method('unzip');
     $this->collection->shift();
 }
Exemplo n.º 6
0
 /**
  * @inheritdoc
  */
 public function build(ReaderTypeInterface $type, array $transportConfig, $resourceType, array $options)
 {
     $resolver = $this->getOptionsResolver($type);
     $transport = $this->createTransport($transportConfig, $this->destinationDir, $this->eventDispatcher);
     $resources = new ResourceCollection([new FileResource($transport)]);
     $type->build($this, $resolver->resolve($options));
     if (isset($this->transformers[$resourceType])) {
         foreach ($this->transformers[$resourceType] as $transformer) {
             $resources->addTransformer($transformer);
         }
     }
     switch ($this->readerType) {
         case self::READER_TYPE_JSONLINES:
             return new JsonLinesReader($resources, $this->eventDispatcher);
             break;
         case self::READER_TYPE_XML:
             // xml is the default, let it fall through
         // xml is the default, let it fall through
         default:
             return new XmlReader($resources, $this->eventDispatcher);
     }
 }