|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* GpsLab component. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace GpsLab\Component\Sitemap\Stream; |
|
11
|
|
|
|
|
12
|
|
|
use GpsLab\Component\Sitemap\Render\SitemapRender; |
|
13
|
|
|
use GpsLab\Component\Sitemap\Stream\Exception\FileAccessException; |
|
14
|
|
|
use GpsLab\Component\Sitemap\Stream\Exception\LinksOverflowException; |
|
15
|
|
|
use GpsLab\Component\Sitemap\Stream\Exception\SizeOverflowException; |
|
16
|
|
|
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException; |
|
17
|
|
|
use GpsLab\Component\Sitemap\Stream\State\StreamState; |
|
18
|
|
|
use GpsLab\Component\Sitemap\Url\Url; |
|
19
|
|
|
|
|
20
|
|
|
class RenderBzip2FileStream implements FileStream |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var SitemapRender |
|
24
|
|
|
*/ |
|
25
|
|
|
private $render; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var StreamState |
|
29
|
|
|
*/ |
|
30
|
|
|
private $state; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var resource|null |
|
34
|
|
|
*/ |
|
35
|
|
|
private $handle; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $filename; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $tmp_filename = ''; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var int |
|
49
|
|
|
*/ |
|
50
|
|
|
private $counter = 0; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
private $end_string = ''; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var int |
|
59
|
|
|
*/ |
|
60
|
|
|
private $used_bytes = 0; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param SitemapRender $render |
|
64
|
|
|
* @param string $filename |
|
65
|
|
|
*/ |
|
66
|
11 |
|
public function __construct(SitemapRender $render, $filename) |
|
67
|
|
|
{ |
|
68
|
11 |
|
$this->render = $render; |
|
69
|
11 |
|
$this->state = new StreamState(); |
|
70
|
11 |
|
$this->filename = $filename; |
|
71
|
11 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getFilename() |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->filename; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
7 |
|
public function open() |
|
82
|
|
|
{ |
|
83
|
7 |
|
$this->state->open(); |
|
84
|
|
|
|
|
85
|
7 |
|
$tmp_filename = tempnam(sys_get_temp_dir(), 'sitemap'); |
|
86
|
|
|
|
|
87
|
7 |
|
if ($tmp_filename === false) { |
|
88
|
|
|
throw FileAccessException::failedCreateUnique(sys_get_temp_dir(), 'sitemap'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
7 |
|
if (file_exists($this->filename) && !is_writable($this->filename)) { |
|
92
|
|
|
throw FileAccessException::notWritable($this->filename); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
7 |
|
$handle = @bzopen($tmp_filename, 'w'); |
|
96
|
|
|
|
|
97
|
7 |
|
if ($handle === false) { |
|
98
|
|
|
throw FileAccessException::notWritable($tmp_filename); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
7 |
|
$this->tmp_filename = $tmp_filename; |
|
102
|
7 |
|
$this->handle = $handle; |
|
103
|
|
|
|
|
104
|
7 |
|
$this->write($this->render->start()); |
|
105
|
|
|
// render end string only once |
|
106
|
7 |
|
$this->end_string = $this->render->end(); |
|
107
|
7 |
|
} |
|
108
|
|
|
|
|
109
|
11 |
View Code Duplication |
public function close() |
|
110
|
|
|
{ |
|
111
|
11 |
|
$this->state->close(); |
|
112
|
7 |
|
$this->write($this->end_string); |
|
113
|
7 |
|
bzclose($this->handle); |
|
114
|
|
|
|
|
115
|
7 |
|
if (!rename($this->tmp_filename, $this->filename)) { |
|
116
|
|
|
unlink($this->tmp_filename); |
|
117
|
|
|
|
|
118
|
|
|
throw FileAccessException::failedOverwrite($this->tmp_filename, $this->filename); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
7 |
|
$this->handle = null; |
|
122
|
7 |
|
$this->tmp_filename = ''; |
|
123
|
7 |
|
$this->counter = 0; |
|
124
|
7 |
|
$this->used_bytes = 0; |
|
125
|
7 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param Url $url |
|
129
|
|
|
*/ |
|
130
|
5 |
View Code Duplication |
public function push(Url $url) |
|
131
|
|
|
{ |
|
132
|
5 |
|
if (!$this->state->isReady()) { |
|
133
|
2 |
|
throw StreamStateException::notReady(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
3 |
|
if ($this->counter >= self::LINKS_LIMIT) { |
|
137
|
1 |
|
throw LinksOverflowException::withLimit(self::LINKS_LIMIT); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
3 |
|
$render_url = $this->render->url($url); |
|
141
|
|
|
|
|
142
|
3 |
|
$expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string); |
|
143
|
3 |
|
if ($expected_bytes > self::BYTE_LIMIT) { |
|
144
|
|
|
throw SizeOverflowException::withLimit(self::BYTE_LIMIT); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
3 |
|
$this->write($render_url); |
|
148
|
3 |
|
++$this->counter; |
|
149
|
3 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return int |
|
153
|
|
|
*/ |
|
154
|
2 |
|
public function count() |
|
155
|
|
|
{ |
|
156
|
2 |
|
return $this->counter; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $string |
|
161
|
|
|
*/ |
|
162
|
7 |
|
private function write($string) |
|
163
|
|
|
{ |
|
164
|
7 |
|
bzwrite($this->handle, $string); |
|
165
|
7 |
|
$this->used_bytes += strlen($string); |
|
166
|
7 |
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|