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\SitemapIndexRender; |
13
|
|
|
use GpsLab\Component\Sitemap\Render\SitemapRender; |
14
|
|
|
use GpsLab\Component\Sitemap\Stream\Exception\OverflowException; |
15
|
|
|
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException; |
16
|
|
|
use GpsLab\Component\Sitemap\Stream\State\StreamState; |
17
|
|
|
use GpsLab\Component\Sitemap\Url\Url; |
18
|
|
|
|
19
|
|
|
class RenderIndexFileStream implements FileStream |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var SitemapIndexRender |
23
|
|
|
*/ |
24
|
|
|
private $render; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var SitemapRender |
28
|
|
|
*/ |
29
|
|
|
private $substream; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var StreamState |
33
|
|
|
*/ |
34
|
|
|
private $state; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $host = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $filename = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $index = 0; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
private $counter = 0; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $buffer = ''; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param SitemapIndexRender $render |
63
|
|
|
* @param FileStream $substream |
64
|
|
|
* @param string $host |
65
|
|
|
* @param string $filename |
66
|
|
|
*/ |
67
|
|
|
public function __construct(SitemapIndexRender $render, FileStream $substream, $host, $filename) |
68
|
|
|
{ |
69
|
|
|
$this->render = $render; |
70
|
|
|
$this->substream = $substream; |
|
|
|
|
71
|
|
|
$this->host = $host; |
72
|
|
|
$this->filename = $filename; |
73
|
|
|
$this->state = new StreamState(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getFilename() |
80
|
|
|
{ |
81
|
|
|
return $this->filename; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function open() |
85
|
|
|
{ |
86
|
|
|
$this->state->open(); |
87
|
|
|
$this->substream->open(); |
|
|
|
|
88
|
|
|
$this->buffer = $this->render->start(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function close() |
92
|
|
|
{ |
93
|
|
|
$this->state->close(); |
94
|
|
|
$this->addSubStreamFileToIndex(); |
95
|
|
|
|
96
|
|
|
file_put_contents($this->filename, $this->buffer.$this->render->end()); |
97
|
|
|
$this->buffer = ''; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Url $url |
102
|
|
|
*/ |
103
|
|
|
public function push(Url $url) |
104
|
|
|
{ |
105
|
|
|
if (!$this->state->isReady()) { |
106
|
|
|
throw StreamStateException::notReady(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
$this->substream->push($url); |
|
|
|
|
111
|
|
|
} catch (OverflowException $e) { |
112
|
|
|
$this->addSubStreamFileToIndex(); |
113
|
|
|
$this->substream->open(); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
++$this->counter; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function addSubStreamFileToIndex() |
120
|
|
|
{ |
121
|
|
|
$this->substream->close(); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$filename = $this->substream->getFilename(); |
|
|
|
|
124
|
|
|
$indexed_filename = $this->getIndexPartFilename($filename, ++$this->index); |
125
|
|
|
$last_mod = (new \DateTimeImmutable())->setTimestamp(filemtime($filename)); |
126
|
|
|
|
127
|
|
|
// rename sitemap file to the index part file |
128
|
|
|
rename($filename, dirname($filename).'/'.$indexed_filename); |
129
|
|
|
|
130
|
|
|
$this->buffer .= $this->render->sitemap($this->host.$indexed_filename, $last_mod); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $filename |
135
|
|
|
* @param int $index |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
private function getIndexPartFilename($filename, $index) |
140
|
|
|
{ |
141
|
|
|
// use explode() for correct add index |
142
|
|
|
// sitemap.xml -> sitemap1.xml |
143
|
|
|
// sitemap.xml.gz -> sitemap1.xml.gz |
144
|
|
|
|
145
|
|
|
list($filename, $extension) = explode('.', basename($filename), 2); |
146
|
|
|
|
147
|
|
|
return sprintf('%s%s.%s', $filename, $index, $extension); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return int |
152
|
|
|
*/ |
153
|
|
|
public function count() |
154
|
|
|
{ |
155
|
|
|
return $this->counter; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..