forked from wprigollopes/sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeflateWriter.php
63 lines (54 loc) · 1.35 KB
/
DeflateWriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace samdark\sitemap;
/**
* Flushes buffer into file with incremental deflating data, available in PHP 7.0+
*/
class DeflateWriter implements WriterInterface
{
/**
* @var resource for target file
*/
private $file;
/**
* @var resource for writable incremental deflate context
*/
private $deflateContext;
/**
* @param string $filename target file
*/
public function __construct($filename)
{
$this->file = fopen($filename, 'ab');
$this->deflateContext = deflate_init(ZLIB_ENCODING_GZIP);
}
/**
* Deflate data in a deflate context and write it to the target file
*
* @param string $data
* @param int $flushMode zlib flush mode to use for writing
*/
private function write($data, $flushMode)
{
assert($this->file !== null);
$compressedChunk = deflate_add($this->deflateContext, $data, $flushMode);
fwrite($this->file, $compressedChunk);
}
/**
* Store data in a deflate stream
*
* @param string $data
*/
public function append($data)
{
$this->write($data, ZLIB_NO_FLUSH);
}
/**
* Make sure all data was written
*/
public function finish()
{
$this->write('', ZLIB_FINISH);
$this->file = null;
$this->deflateContext = null;
}
}