diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f0fe7..b0e515e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,3 +28,13 @@ ### Features - Added missing features for supporting PHPWord + +## 0.2.4 + +### Changes +- XMLWriter : Refactoring for improving performances + +## 0.2.5 + +### Features +- Added Zip Adapters (PclZip & ZipArchive) diff --git a/VERSION b/VERSION index 373f8c6..72f9fa8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.3 \ No newline at end of file +0.2.4 \ No newline at end of file diff --git a/composer.json b/composer.json index 551401d..eed7fad 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ } ], "require": { - "php": ">=5.3.0" + "php": ">=5.3.0", + "pclzip/pclzip": "^2.8" }, "require-dev": { "phpunit/phpunit": "3.7.*", diff --git a/src/Common/Adapter/Zip/PclZipAdapter.php b/src/Common/Adapter/Zip/PclZipAdapter.php new file mode 100644 index 0000000..92d73f1 --- /dev/null +++ b/src/Common/Adapter/Zip/PclZipAdapter.php @@ -0,0 +1,58 @@ +oPclZip = new PclZip($filename); + $this->tmpDir = sys_get_temp_dir(); + return $this; + } + + /** + * @return $this + */ + public function close() + { + return $this; + } + + /** + * @param $localname + * @param $contents + * @return $this + * @throws \Exception + */ + public function addFromString($localname, $contents) + { + $pathData = pathinfo($localname); + + $hFile = fopen($this->tmpDir.'/'.$pathData['basename'], "wb"); + fwrite($hFile, $contents); + fclose($hFile); + + $res = $this->oPclZip->add($this->tmpDir.'/'.$pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']); + if ($res == 0) { + throw new \Exception("Error zipping files : " . $this->oPclZip->errorInfo(true)); + } + unlink($this->tmpDir.'/'.$pathData['basename']); + return $this; + } +} diff --git a/src/Common/Adapter/Zip/ZipArchiveAdapter.php b/src/Common/Adapter/Zip/ZipArchiveAdapter.php new file mode 100644 index 0000000..b7d6787 --- /dev/null +++ b/src/Common/Adapter/Zip/ZipArchiveAdapter.php @@ -0,0 +1,59 @@ +filename for writing. + * @return mixed + */ + public function open($filename) + { + $this->filename = $filename; + $this->oZipArchive = new ZipArchive(); + + if ($this->oZipArchive->open($this->filename, ZipArchive::OVERWRITE) === true) { + return $this; + } + if ($this->oZipArchive->open($this->filename, ZipArchive::CREATE) === true) { + return $this; + } + throw new \Exception("Could not open $this->filename for writing."); + } + + /** + * @return $this + * @throws \Exception Could not close zip file $this->filename. + */ + public function close() + { + if ($this->oZipArchive->close() === false) { + throw new \Exception("Could not close zip file $this->filename."); + } + return $this; + } + + /** + * @param $localname + * @param $contents + * @return bool + */ + public function addFromString($localname, $contents) + { + return $this->oZipArchive->addFromString($localname, $contents); + } +} diff --git a/src/Common/Adapter/Zip/ZipInterface.php b/src/Common/Adapter/Zip/ZipInterface.php new file mode 100644 index 0000000..b830fb8 --- /dev/null +++ b/src/Common/Adapter/Zip/ZipInterface.php @@ -0,0 +1,10 @@ +xmlWriter = new \XMLWriter(); - // Open temporary storage if ($pTemporaryStorage == self::STORAGE_MEMORY) { - $this->xmlWriter->openMemory(); + $this->openMemory(); } else { + if (!is_dir($pTemporaryStorageDir)) { + $pTemporaryStorageDir = sys_get_temp_dir(); + } // Create temporary filename $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml'); // Open storage - $this->xmlWriter->openUri($this->tempFileName); + $this->openUri($this->tempFileName); } if ($compatibility) { - $this->xmlWriter->setIndent(false); - $this->xmlWriter->setIndentString(''); + $this->setIndent(false); + $this->setIndentString(''); } else { - $this->xmlWriter->setIndent(true); - $this->xmlWriter->setIndentString(' '); + $this->setIndent(true); + $this->setIndentString(' '); } } @@ -89,31 +82,12 @@ public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTempora */ public function __destruct() { - // Desctruct XMLWriter - unset($this->xmlWriter); - // Unlink temporary files - if ($this->tempFileName != '') { - if (@unlink($this->tempFileName) === false) { - throw new \Exception('The file '.$this->tempFileName.' could not be deleted.'); - } + if (empty($this->tempFileName)) { + return; } - } - - /** - * Catch function calls (and pass them to internal XMLWriter) - * - * @param mixed $function - * @param mixed $args - */ - public function __call($function, $args) - { - try { - if (@call_user_func_array(array($this->xmlWriter, $function), $args) === false) { - throw new \Exception('The method '.$function.' doesn\'t exist.'); - } - } catch (\Exception $ex) { - // Do nothing! + if (PHP_OS != 'WINNT' && @unlink($this->tempFileName) === false) { + throw new \Exception('The file '.$this->tempFileName.' could not be deleted.'); } } @@ -125,9 +99,9 @@ public function __call($function, $args) public function getData() { if ($this->tempFileName == '') { - return $this->xmlWriter->outputMemory(true); + return $this->outputMemory(true); } else { - $this->xmlWriter->flush(); + $this->flush(); return file_get_contents($this->tempFileName); } } @@ -147,14 +121,14 @@ public function getData() */ public function writeElementBlock($element, $attributes, $value = null) { - $this->xmlWriter->startElement($element); + $this->startElement($element); if (!is_array($attributes)) { $attributes = array($attributes => $value); } foreach ($attributes as $attribute => $value) { - $this->xmlWriter->writeAttribute($attribute, $value); + $this->writeAttribute($attribute, $value); } - $this->xmlWriter->endElement(); + $this->endElement(); } /** @@ -170,11 +144,11 @@ public function writeElementIf($condition, $element, $attribute = null, $value = { if ($condition == true) { if (is_null($attribute)) { - $this->xmlWriter->writeElement($element, $value); + $this->writeElement($element, $value); } else { - $this->xmlWriter->startElement($element); - $this->xmlWriter->writeAttribute($attribute, $value); - $this->xmlWriter->endElement(); + $this->startElement($element); + $this->writeAttribute($attribute, $value); + $this->endElement(); } } } @@ -190,7 +164,7 @@ public function writeElementIf($condition, $element, $attribute = null, $value = public function writeAttributeIf($condition, $attribute, $value) { if ($condition == true) { - $this->xmlWriter->writeAttribute($attribute, $value); + $this->writeAttribute($attribute, $value); } } } diff --git a/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php b/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php new file mode 100644 index 0000000..2bc4a32 --- /dev/null +++ b/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php @@ -0,0 +1,54 @@ +zipTest = tempnam($pathResources, 'PhpOfficeCommon'); + copy($pathResources.'Sample_01_Simple.pptx', $this->zipTest); + } + + public function tearDown() + { + parent::tearDown(); + + unlink($this->zipTest); + } + + public function testOpen() + { + $object = new PclZipAdapter(); + $this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $object->open($this->zipTest)); + } + + public function testClose() + { + $object = new PclZipAdapter(); + $object->open($this->zipTest); + $this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $object->close()); + } + + public function testAddFromString() + { + $expectedPath = 'file.test'; + $expectedContent = 'Content'; + + $object = new PclZipAdapter(); + $object->open($this->zipTest); + $object->addFromString($expectedPath, $expectedContent); + $object->close(); + + $this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath)); + $this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent)); + } +} diff --git a/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php b/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php new file mode 100644 index 0000000..2799b33 --- /dev/null +++ b/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php @@ -0,0 +1,56 @@ +zipTest = tempnam($pathResources, 'PhpOfficeCommon'); + copy($pathResources.'Sample_01_Simple.pptx', $this->zipTest); + } + + public function tearDown() + { + parent::tearDown(); + + if (is_file($this->zipTest)) { + unlink($this->zipTest); + } + } + + public function testOpen() + { + $object = new ZipArchiveAdapter(); + $this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $object->open($this->zipTest)); + } + + public function testClose() + { + $object = new ZipArchiveAdapter(); + $object->open($this->zipTest); + $this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $object->close()); + } + + public function testAddFromString() + { + $expectedPath = 'file.test'; + $expectedContent = 'Content'; + + $object = new ZipArchiveAdapter(); + $object->open($this->zipTest); + $object->addFromString($expectedPath, $expectedContent); + $object->close(); + + $this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath)); + $this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent)); + } +} diff --git a/tests/Common/Tests/_includes/TestHelperDOCX.php b/tests/Common/Tests/_includes/TestHelperDOCX.php deleted file mode 100644 index 2ea7bbc..0000000 --- a/tests/Common/Tests/_includes/TestHelperDOCX.php +++ /dev/null @@ -1,104 +0,0 @@ -save(self::$file); - - $zip = new \ZipArchive; - $res = $zip->open(self::$file); - if ($res === true) { - $zip->extractTo(sys_get_temp_dir() . '/PhpPowerpoint_Unit_Test/'); - $zip->close(); - } - - return new XmlDocument(sys_get_temp_dir() . '/PhpPowerpoint_Unit_Test/'); - } - - /** - * Clear document - */ - public static function clear() - { - if (file_exists(self::$file)) { - unlink(self::$file); - } - if (is_dir(sys_get_temp_dir() . '/PhpPowerpoint_Unit_Test/')) { - self::deleteDir(sys_get_temp_dir() . '/PhpPowerpoint_Unit_Test/'); - } - } - - /** - * Delete directory - * - * @param string $dir - */ - public static function deleteDir($dir) - { - foreach (scandir($dir) as $file) { - if ($file === '.' || $file === '..') { - continue; - } elseif (is_file($dir . "/" . $file)) { - unlink($dir . "/" . $file); - } elseif (is_dir($dir . "/" . $file)) { - self::deleteDir($dir . "/" . $file); - } - } - - rmdir($dir); - } - - /** - * Get file - * - * @return string - */ - public static function getFile() - { - return self::$file; - } -} diff --git a/tests/Common/Tests/_includes/TestHelperZip.php b/tests/Common/Tests/_includes/TestHelperZip.php new file mode 100644 index 0000000..6f7c54d --- /dev/null +++ b/tests/Common/Tests/_includes/TestHelperZip.php @@ -0,0 +1,33 @@ +open($fileZip) !== true) { + return false; + } + if ($oZip->statName($path) === false) { + return false; + } + return true; + } + + public static function assertFileContent($fileZip, $path, $content) + { + $oZip = new \ZipArchive; + if ($oZip->open($fileZip) !== true) { + return false; + } + $zipFileContent = $oZip->getFromName($path); + if ($zipFileContent === false) { + return false; + } + if ($zipFileContent != $content) { + return false; + } + return true; + } +}