Skip to content

Commit

Permalink
Adjustments for gzip support
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Jan 28, 2017
1 parent 1f5ebb3 commit 9b0d0d1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
16 changes: 8 additions & 8 deletions Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,23 +91,23 @@ 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());
}
}

/**
* 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;
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 14 additions & 21 deletions Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -74,7 +69,7 @@ class Sitemap
/**
* @var bool whether to gzip the resulting files or not
*/
private $gzip = false;
private $useGzip = false;

/**
* @var XMLWriter
Expand Down Expand Up @@ -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');
Expand All @@ -147,8 +137,6 @@ private function finishFile()
$this->writer->endElement();
$this->writer->endDocument();
$this->flush();
fclose($this->fileHandle);
$this->fileHandle = null;
}
}

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions tests/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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++) {
Expand Down

0 comments on commit 9b0d0d1

Please sign in to comment.