/** * @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 needsTransforming(ResourceInterface $resource) { $resourceFile = (string) $resource->getTransport(); // don't transform unzipped files foreach ($this->files as $file) { if ($resourceFile === $this->getTargetFile($resource, $file)) { return false; } } // check if file type is actually zip return $this->isExtractable($resource); }
/** * @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); }