-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathSitemap.php
252 lines (217 loc) · 4.67 KB
/
Sitemap.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace Melbahja\Seo;
use Closure;
use Melbahja\Seo\{
Sitemap\SitemapIndex,
Exceptions\SitemapException,
Interfaces\SitemapIndexInterface,
Interfaces\SitemapBuilderInterface
};
/**
* @package Melbahja\Seo
* @since v2.0
* @see https://git.io/phpseo
* @license MIT
* @copyright 2019-present Mohamed Elabhja
*/
class Sitemap implements SitemapIndexInterface
{
/**
* Sitemap options
* @var array
*/
protected $options =
[
'save_path' => null,
'index_name' => 'sitemap.xml',
'sitemaps_url' => null,
]
/**
* Sitemap files
* @var array
*/
, $sitemaps = []
/**
* Sitemaps domain name
* @var string
*/
, $domain;
/**
* Initialize new sitemap builder
*
* @param string $domain The domain name only
* @param array $options
*/
public function __construct(string $domain, array $options = null)
{
$this->domain = $domain;
if ($options !== null) {
$this->setOptions($options);
}
}
/**
* Set builer options
*
* @param array $options
* @return SitemapIndexInterface
*/
public function setOptions(array $options): SitemapIndexInterface
{
$this->options = array_merge($this->options, $options);
return $this;
}
/**
* Get all sitemap options
*
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
/**
* Set save path
*
* @param string $path
* @return SitemapIndexInterface
*/
public function setSavePath(string $path): SitemapIndexInterface
{
$this->options['save_path'] = $path;
return $this;
}
/**
* Get save path
*
* @return null|string
*/
public function getSavePath(): ?string
{
return $this->options['save_path'];
}
/**
* Set index name
*
* @param string $name
* @return SitemapIndexInterface
*/
public function setIndexName(string $name): SitemapIndexInterface
{
$this->options['index_name'] = $name;
return $this;
}
/**
* Get Index name
*
* @return string
*/
public function getIndexName(): string
{
return $this->options['index_name'];
}
/**
* Set sitemaps url
*
* @param string $url
* @return SitemapIndexInterface
*/
public function setSitemapsUrl(string $url): SitemapIndexInterface
{
$this->options['sitemaps_url'] = $url;
return $this;
}
/**
* Get sitemaps url
*
* @return null|string
*/
public function getSitemapsUrl(): ?string
{
return $this->options['sitemaps_url'] ?? $this->domain;
}
/**
* Get sitemaps domain
*
* @return string
*/
public function getDomain(): string
{
return $this->domain;
}
/**
* Set sitemaps to a path
*
* @param string $path
* @return bool
*/
public function saveTo(string $path): bool
{
return SitemapIndex::build(
$this->getIndexName(), $path, $this->getSitemapsUrl(), $this->sitemaps
);
}
/**
* {@method saveTo} by pre defined save_path option
*
* @param string $path
* @return bool
*/
public function save(): bool
{
if (is_string($this->options['save_path']) === false) {
throw new SitemapException('Invalid or missing save_path option');
}
return $this->saveTo($this->options['save_path']);
}
/**
* Generate sitemaps
*
* @param SitemapBuilderInterface $builder
* @param array $options
* @param callable $func
* @return SitemapIndexInterface
*/
public function build(SitemapBuilderInterface $builder, array $options, callable $func): SitemapIndexInterface
{
if (isset($this->sitemaps[$options['name']])) {
throw new SitemapException("The sitemap {$name} already registred!");
}
// Generate urls.
call_user_func_array($func, [$builder]);
return $this->buildTemp($options['name'], $builder);
}
/**
* Sitemaps generator
*
* @param string $builder
* @param array $args
* @return SitemapIndexInterface
*/
public function __call(string $builder, array $args): SitemapIndexInterface
{
if (class_exists($builder = '\Melbahja\Seo\Sitemap\\' . ucfirst($builder) . 'Builder')) {
if (count($args) !== 2) {
throw new SitemapException("Invalid {$builder} arguments");
} elseif (is_string($args[0])) {
$args[0] = ['name' => $args[0]];
}
if (isset($args[0]['name']) === false) {
throw new SitemapException("Sitemap name is required for {$builder}");
}
return $this->build(new $builder($this->domain, $args[0]), ...$args);
}
throw new SitemapException("Sitemap builder {$builder} not exists");
}
/**
* Build registred sitemap and save it on temp
*
* @param string $name
* @param SitemapBuilderInterface $builder
* @return SitemapIndexInterface
*/
protected function buildTemp(string $name, SitemapBuilderInterface $builder): SitemapIndexInterface
{
$this->sitemaps[$name] = $builder->saveTemp();
return $this;
}
}