/
SitemapGenerator.php
200 lines (170 loc) · 6.64 KB
/
SitemapGenerator.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace OS\SitemapBundle;
use Doctrine\ORM\EntityManager,
DOMDocument,
DateTime;
/**
* @author ouardisoft
*/
class SitemapGenerator
{
/**
* @var EntityManager $em
*/
private $em;
/**
* @var
*/
private $router;
/**
*
* @var array $configs
*/
private $configs;
/**
* @var DOMDocument $root
*/
private $dom;
/**
*
* @param EntityManager $em
* @param Router $router
* @param array $configs
*/
function __construct(EntityManager $em, $router, $configs)
{
$this->em = $em;
$this->router = $router;
$this->configs = $configs;
}
public function generate($returnString = false)
{
// Create dom object
$this->dom = new DOMDocument('1.0', 'UTF-8');
$this->dom->formatOutput = true;
$this->dom->substituteEntities = false;
// Create <urlset> root tag
$urlset = $this->dom->createElement('urlset');
// Add attribute of urlset
$xmlns = $this->dom->createAttribute('xmlns');
$urlsetText = $this->dom->createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9');
$urlset->appendChild($xmlns);
$xmlns->appendChild($urlsetText);
//When scripts run from the command line, you'll need to manually set the desired host
if (isset($this->configs['host'])) {
$this->router->getContext()->setHost($this->configs['host']);
}
// Iterate over all routes in the config.
foreach($this->configs['routes'] as $route => $routeConfig) {
// If entity in the config has been defined
if (isset($this->configs['routes'][$route]['entity'])) {
if (isset($this->configs['routes'][$route]['repository_method'])) {
$method = $this->configs['routes'][$route]['repository_method'];
if (method_exists($this->em->getRepository($this->configs['routes'][$route]['entity']), $method)) {
$method = $this->configs['routes'][$route]['repository_method'];
} else {
throw new \BadMethodCallException(sprintf("The method %s not found in %s repository.", $method, $this->configs['routes'][$route]['entity']));
}
} else {
$method = "findAll";
}
// Fetch All entities
$entities = $this->em->getRepository($this->configs['routes'][$route]['entity'])->$method();
// Creates an url node for each entity
foreach ($entities as $entity) {
$url = $this->createUrlNode($this->configs['routes'][$route], $entity);
$urlset->appendChild($url);
}
} else {
$url = $this->createUrlNode($this->configs['routes'][$route]);
$urlset->appendChild($url);
}
}
$this->dom->appendChild($urlset);
if ($returnString == false)
return $this->dom->save($this->configs['path']);
return $this->dom->saveXML();
}
/**
* Creates a url node which represents a singel url.
* @param array configs
*/
public function createUrlNode($configs, $entity=null)
{
/*
* Generate <url> tags and bind them in urlset
* <url>
* <loc>link</loc>
* <lastmod>date</lastmod>
* <priority>date</priority>
* </url>
*/
$tags = array('loc', 'lastmod', 'priority');
$url = $this->dom->createElement('url');
foreach ($tags as $tag) {
$text = $this->dom->createTextNode($this->{'create' . ucfirst($tag) . 'Node'}($configs, $entity));
$elem = $this->dom->createElement($tag);
$elem->appendChild($text);
$url->appendChild($elem);
}
return $url;
}
/**
* Creates a loc node (<loc><loc>)
* @param array configs
*/
public function createLocNode($configs, $entity=null)
{
$params = array();
foreach ($configs['loc']['params'] as $key => $param) {
if (is_array($param)) {
// params: { key: {value: <some value>, static: true}}
if (isset($param['static']) && $param['static']) { $params[$key] = $param['value'];
// params: { key: {class: <some class>, method: <some method>, field: <some method>}}
} else {
$value = $entity->{'get' . ucfirst($param['field'])}();
$object = new $param['class'];
$params[$key] = $object->{$param['method']}($value);
}
} else {
if(is_null($entity)) {
$params[$key] = $param;
} else {
$value = $entity;
foreach (preg_split("/\./", $param) as $attr) {
$value = $value->{'get' . ucfirst($attr)}();
}
$params[$key] = $value;
}
}
}
return $this->router->generate($configs['loc']['route'], $params, true);
}
/**
* Creates a lastmod node (<lastmod></lastmod>)
* @param array configs
*/
public function createLastmodNode($configs, $entity=null)
{
if (!is_null($entity)) {
if (method_exists($entity, 'get' . ucfirst($configs['lastmod']))) {
$value = $entity->{'get' . ucfirst($configs['lastmod'])}();
if ($value instanceof DateTime) {
return $value->format('Y-m-d');
} else {
return substr($value, 0, 100);
}
}
}
return $configs['lastmod'];
}
/**
* Creates a priority node (<node></node>)
* @param array configs
*/
public function createPriorityNode($configs)
{
return $configs['priority'];
}
}
?>