|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the DpnXmlSitemapBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Björn Fromme <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the Resources/meta/LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Dpn\XmlSitemapBundle\Sitemap; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Sitemap manager class |
|
18
|
|
|
* |
|
19
|
|
|
* @author Björn Fromme <[email protected]> |
|
20
|
|
|
* @author Kevin Bond <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class SitemapManager |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $defaults; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $maxPerSitemap; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \Dpn\XmlSitemapBundle\Sitemap\Entry[]|null |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $entries; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var GeneratorInterface[] |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $generators = array(); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var EngineInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $templating; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $defaults |
|
51
|
|
|
* @param int $maxPerSitemap |
|
52
|
|
|
* @param EngineInterface $templating |
|
53
|
|
|
*/ |
|
54
|
17 |
|
public function __construct(array $defaults, $maxPerSitemap, EngineInterface $templating) |
|
55
|
|
|
{ |
|
56
|
17 |
|
$this->defaults = array_merge( |
|
57
|
|
|
array( |
|
58
|
17 |
|
'priority' => null, |
|
59
|
17 |
|
'changefreq' => null, |
|
60
|
17 |
|
), |
|
61
|
|
|
$defaults |
|
62
|
17 |
|
); |
|
63
|
|
|
|
|
64
|
17 |
|
$this->maxPerSitemap = intval($maxPerSitemap); |
|
65
|
17 |
|
$this->templating = $templating; |
|
66
|
17 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param GeneratorInterface $generator |
|
70
|
|
|
*/ |
|
71
|
15 |
|
public function addGenerator(GeneratorInterface $generator) |
|
72
|
|
|
{ |
|
73
|
15 |
|
$this->generators[] = $generator; |
|
74
|
15 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return \Dpn\XmlSitemapBundle\Sitemap\Entry[] |
|
78
|
|
|
*/ |
|
79
|
15 |
|
public function getSitemapEntries() |
|
80
|
|
|
{ |
|
81
|
15 |
|
if (null !== $this->entries) { |
|
82
|
9 |
|
return $this->entries; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
15 |
|
$entries = array(); |
|
86
|
|
|
|
|
87
|
15 |
|
foreach ($this->generators as $generator) { |
|
88
|
15 |
|
$entries = array_merge($entries, $generator->generate()); |
|
89
|
15 |
|
} |
|
90
|
|
|
|
|
91
|
15 |
|
$this->entries = $entries; |
|
92
|
|
|
|
|
93
|
15 |
|
return $this->entries; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return int |
|
98
|
|
|
*/ |
|
99
|
14 |
|
public function countSitemapEntries() |
|
100
|
|
|
{ |
|
101
|
14 |
|
return count($this->getSitemapEntries()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return int |
|
106
|
|
|
*/ |
|
107
|
13 |
|
public function getNumberOfSitemaps() |
|
108
|
|
|
{ |
|
109
|
13 |
|
$total = $this->countSitemapEntries(); |
|
110
|
|
|
|
|
111
|
13 |
|
if ($total <= $this->maxPerSitemap) { |
|
112
|
6 |
|
return 1; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
9 |
|
return intval(ceil($total / $this->maxPerSitemap)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param int $number |
|
120
|
|
|
* |
|
121
|
|
|
* @return \Dpn\XmlSitemapBundle\Sitemap\Entry[] |
|
122
|
|
|
* |
|
123
|
|
|
* @throws \InvalidArgumentException |
|
124
|
|
|
*/ |
|
125
|
6 |
|
public function getEntriesForSitemap($number) |
|
126
|
|
|
{ |
|
127
|
6 |
|
$numberOfSitemaps = $this->getNumberOfSitemaps(); |
|
128
|
|
|
|
|
129
|
6 |
|
if ($number > $numberOfSitemaps) { |
|
130
|
1 |
|
throw new \InvalidArgumentException('Number exceeds total sitemap count.'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
6 |
|
if (1 === $numberOfSitemaps) { |
|
134
|
1 |
|
return $this->getSitemapEntries(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
6 |
|
$sitemaps = array_chunk($this->getSitemapEntries(), $this->maxPerSitemap); |
|
138
|
|
|
|
|
139
|
6 |
|
return $sitemaps[$number - 1]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param int|null $number |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
8 |
|
public function renderSitemap($number = null) |
|
148
|
|
|
{ |
|
149
|
8 |
|
$entries = null === $number ? $this->getSitemapEntries() : $this->getEntriesForSitemap($number); |
|
150
|
|
|
|
|
151
|
8 |
|
return $this->templating->render( |
|
152
|
8 |
|
'DpnXmlSitemapBundle::sitemap.xml.twig', |
|
153
|
|
|
array( |
|
154
|
8 |
|
'entries' => $entries, |
|
155
|
8 |
|
'default_priority' => Entry::normalizePriority($this->defaults['priority']), |
|
156
|
8 |
|
'default_changefreq' => Entry::normalizeChangeFreq($this->defaults['changefreq']), |
|
157
|
|
|
) |
|
158
|
8 |
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $host |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
5 |
|
public function renderSitemapIndex($host) |
|
167
|
|
|
{ |
|
168
|
5 |
|
return $this->templating->render( |
|
169
|
5 |
|
'DpnXmlSitemapBundle::sitemap_index.xml.twig', |
|
170
|
|
|
array( |
|
171
|
5 |
|
'num_sitemaps' => $this->getNumberOfSitemaps(), |
|
172
|
5 |
|
'host' => $host, |
|
173
|
|
|
) |
|
174
|
5 |
|
); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|