From 9b0d0d11b26d96f463a3aa3ea74a7f6ab3c33a8a Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 28 Jan 2017 13:09:18 +0300 Subject: [PATCH] Adjustments for gzip support --- Index.php | 16 ++++++++-------- README.md | 8 ++++---- Sitemap.php | 35 ++++++++++++++--------------------- tests/IndexTest.php | 2 +- tests/SitemapTest.php | 8 ++++++-- 5 files changed, 33 insertions(+), 36 deletions(-) diff --git a/Index.php b/Index.php index df83108..239de58 100644 --- a/Index.php +++ b/Index.php @@ -23,7 +23,7 @@ class Index /** * @var bool whether to gzip the resulting file or not */ - private $gzip = false; + private $useGzip = false; /** * @param string $filePath index file path @@ -91,8 +91,8 @@ public function write() $this->writer->endElement(); $this->writer->endDocument(); $filePath = $this->getFilePath(); - if ($this->gzip) { - $filePath = 'compress.zlib://'.$filePath; + if ($this->useGzip) { + $filePath = 'compress.zlib://' . $filePath; } file_put_contents($filePath, $this->writer->flush()); } @@ -100,14 +100,14 @@ public function write() /** * Sets whether the resulting file will be gzipped or not. - * @param bool $bool + * @param bool $value + * @throws \RuntimeException when trying to enable gzip while zlib is not available */ - public function setGzip($bool) + public function setUseGzip($value) { - $gzip = (bool)$bool; - if ($bool && !extension_loaded('zlib')) { + if ($value && !extension_loaded('zlib')) { throw new \RuntimeException('Zlib extension must be installed to gzip the sitemap.'); } - $this->gzip = $bool; + $this->useGzip = $value; } } diff --git a/README.md b/README.md index 5806637..236db79 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ $index->write(); Options ------- -There are three methods to configure `Sitemap` instance: +There are methods to configure `Sitemap` instance: - `setMaxUrls($number)`. Sets maximum number of URLs to write in a single file. Default is 50000 which is the limit according to specification and most of @@ -90,12 +90,12 @@ There are three methods to configure `Sitemap` instance: Default is 1000. If you have more memory consider increasing it. If 1000 URLs doesn't fit, decrease it. - `setUseIndent($bool)`. Sets if XML should be indented. Default is true. -- `setGzip($bool)`. Sets whether the resulting sitemap files will be gzipped or not. +- `setUseGzip($bool)`. Sets whether the resulting sitemap files will be gzipped or not. Default is `false`. `zlib` extension must be enabled to use this feature. -There is one method to configure `Index` instance: +There is a method to configure `Index` instance: -- `setGzip($bool)`. Sets whether the resulting index file will be gzipped or not. +- `setUseGzip($bool)`. Sets whether the resulting index file will be gzipped or not. Default is `false`. `zlib` extension must be enabled to use this feature. Running tests diff --git a/Sitemap.php b/Sitemap.php index dc9fb0e..27d592d 100644 --- a/Sitemap.php +++ b/Sitemap.php @@ -33,11 +33,6 @@ class Sitemap */ private $filePath; - /** - * @var resource handle of the file to be written - */ - private $fileHandle; - /** * @var integer number of files written */ @@ -74,7 +69,7 @@ class Sitemap /** * @var bool whether to gzip the resulting files or not */ - private $gzip = false; + private $useGzip = false; /** * @var XMLWriter @@ -125,11 +120,6 @@ private function createNewFile() } } - if ($this->gzip) { - $filePath = 'compress.zlib://' . $filePath; - } - $this->fileHandle = fopen($filePath, 'w'); - $this->writer = new XMLWriter(); $this->writer->openMemory(); $this->writer->startDocument('1.0', 'UTF-8'); @@ -147,8 +137,6 @@ private function finishFile() $this->writer->endElement(); $this->writer->endDocument(); $this->flush(); - fclose($this->fileHandle); - $this->fileHandle = null; } } @@ -165,7 +153,11 @@ public function write() */ private function flush() { - fwrite($this->fileHandle, $this->writer->flush(true)); + $filePath = $this->getCurrentFilePath(); + if ($this->useGzip) { + $filePath = 'compress.zlib://' . $filePath; + } + file_put_contents($filePath, $this->writer->flush(true), FILE_APPEND); } /** @@ -251,7 +243,7 @@ private function getCurrentFilePath() } $parts = pathinfo($this->filePath); - if ($parts['extension'] == 'gz') { + if ($parts['extension'] === 'gz') { $filenameParts = pathinfo($parts['filename']); if (!empty($filenameParts['extension'])) { $parts['filename'] = $filenameParts['filename']; @@ -311,17 +303,18 @@ public function setUseIndent($value) /** * Sets whether the resulting files will be gzipped or not. - * @param bool $bool + * @param bool $value + * @throws \RuntimeException when trying to enable gzip while zlib is not available or when trying to change + * setting when some items are already written */ - public function setGzip($bool) + public function setUseGzip($value) { - $bool = (bool)$bool; - if ($bool && !extension_loaded('zlib')) { + if ($value && !extension_loaded('zlib')) { throw new \RuntimeException('Zlib extension must be enabled to gzip the sitemap.'); } - if ($this->urlsCount && $bool != $this->gzip) { + if ($this->urlsCount && $value != $this->useGzip) { throw new \RuntimeException('Cannot change the gzip value once items have been added to the sitemap.'); } - $this->gzip = $bool; + $this->useGzip = $value; } } diff --git a/tests/IndexTest.php b/tests/IndexTest.php index 0cfd098..eafb7b9 100644 --- a/tests/IndexTest.php +++ b/tests/IndexTest.php @@ -40,7 +40,7 @@ public function testWritingFileGzipped() { $fileName = __DIR__ . '/sitemap_index.xml.gz'; $index = new Index($fileName); - $index->setGzip(true); + $index->setUseGzip(true); $index->addSitemap('http://example.com/sitemap.xml'); $index->addSitemap('http://example.com/sitemap_2.xml', time()); $index->write(); diff --git a/tests/SitemapTest.php b/tests/SitemapTest.php index 8990566..e46ada9 100644 --- a/tests/SitemapTest.php +++ b/tests/SitemapTest.php @@ -5,6 +5,10 @@ class SitemapTest extends \PHPUnit_Framework_TestCase { + /** + * Asserts validity of simtemap according to XSD schema + * @param string $fileName + */ protected function assertIsValidSitemap($fileName) { $xml = new \DOMDocument(); @@ -114,7 +118,7 @@ public function testWritingFileGzipped() { $fileName = __DIR__ . '/sitemap_gzipped.xml.gz'; $sitemap = new Sitemap($fileName); - $sitemap->setGzip(true); + $sitemap->setUseGzip(true); $sitemap->addItem('http://example.com/mylink1'); $sitemap->addItem('http://example.com/mylink2', time()); $sitemap->addItem('http://example.com/mylink3', time(), Sitemap::HOURLY); @@ -132,7 +136,7 @@ public function testWritingFileGzipped() public function testMultipleFilesGzipped() { $sitemap = new Sitemap(__DIR__ . '/sitemap_multi_gzipped.xml.gz'); - $sitemap->setGzip(true); + $sitemap->setUseGzip(true); $sitemap->setMaxUrls(2); for ($i = 0; $i < 20; $i++) {