/** * @param string $file * * @throws \RuntimeException When the file does not exist * * @return FileTransport */ public static function createTransportFromFile($file) { if (false === file_exists($file)) { throw new \RuntimeException(sprintf('File "%s" does not exist', $file)); } return FileTransport::create($file); }
protected function setUp() { if (!extension_loaded('zip')) { $this->markTestSkipped('The zip extension is not available'); } $this->zipFile = new \ZipArchive(); $this->zipFile->open($this->zipFilename, \ZipArchive::CREATE); $this->zipFile->addFromString($this->filename, $this->fileContents); $this->zipFile->close(); $this->collection = new ResourceCollection([new FileResource(FileTransport::create($this->zipFilename))]); }
/** * @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(); }
/** * @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(); }
/** * @inheritdoc */ public function transform(ResourceInterface $resource, ResourceCollection $collection) { $file = $resource->getFile()->getPathname(); // first, rename the original file $oldFile = $this->rename($file); $old = fopen($oldFile, 'r'); $new = fopen($file, 'w'); while (!feof($old)) { fwrite($new, mb_convert_encoding(fgets($old), $this->toEncoding, $this->fromEncoding)); } fclose($old); fclose($new); unlink($oldFile); $transport = FileTransport::create($file); if ($resource->getTransport()) { $transport->setDestinationDir($resource->getTransport()->getDestinationDir()); } return new FileResource($transport); }
/** * @inheritdoc */ public function transform(ResourceInterface $resource, ResourceCollection $collection) { $file = $resource->getFile()->getPathname(); // the file could be big, so just read the $tmpFile = tempnam(sys_get_temp_dir(), $file); $old = fopen($file, 'r'); $new = fopen($tmpFile, 'w'); // write the beginning with the xml declaration replaced fwrite($new, preg_replace($this->xmlDeclarationRegEx, $this->xmlDeclaration, fread($old, 96))); // now copy the rest of the file while (!feof($old)) { fwrite($new, fread($old, 8192)); } fclose($old); fclose($new); // atomic write $this->rename($tmpFile, $file); $transport = FileTransport::create($file); if ($resource->getTransport()) { $transport->setDestinationDir($resource->getTransport()->getDestinationDir()); } return new FileResource($transport); }
/** * @inheritdoc */ public function transform(ResourceInterface $resource, ResourceCollection $collection) { $file = $resource->getFile()->getPathname(); $tmpFile = tempnam(sys_get_temp_dir(), $file); // remove control characters $old = fopen($file, 'r'); $new = fopen($tmpFile, 'w'); // list control characters, but leave out \t\r\n $chars = array_map('chr', range(0, 31)); $chars[] = chr(127); unset($chars[9], $chars[10], $chars[13]); while (!feof($old)) { fwrite($new, str_replace($chars, '', fread($old, $this->length))); } fclose($old); fclose($new); // atomic write $this->rename($tmpFile, $file); $transport = FileTransport::create($file); if ($resource->getTransport()) { $transport->setDestinationDir($resource->getTransport()->getDestinationDir()); } return new FileResource($transport); }
/** * @return FileTransport */ protected function getTransport() { return FileTransport::create($this->getFilename()); }
/** * @expectedException \TreeHouse\Feeder\Exception\TransportException */ public function testNonExistingFileResource() { $transport = FileTransport::create('/foo'); $resource = new FileResource($transport); $resource->getFile(); }