1
|
|
|
<?php namespace Arcanedev\LaravelSitemap; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap as SitemapContract; |
4
|
|
|
use Arcanedev\LaravelSitemap\Contracts\SitemapManager as SitemapManagerContract; |
5
|
|
|
use Arcanedev\LaravelSitemap\Entities\Sitemap; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SitemapManager |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\LaravelSitemap |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class SitemapManager implements SitemapManagerContract |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Properties |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** @var \Illuminate\Support\Collection */ |
22
|
|
|
protected $sitemaps; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $format = 'xml'; |
26
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Constructor |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* SitemapManager constructor. |
34
|
|
|
*/ |
35
|
38 |
|
public function __construct() |
36
|
|
|
{ |
37
|
38 |
|
$this->sitemaps = new Collection; |
38
|
38 |
|
} |
39
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
41
|
|
|
| Getters & Setters |
42
|
|
|
| ----------------------------------------------------------------- |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set the format. |
47
|
|
|
* |
48
|
|
|
* @param string $format |
49
|
|
|
* |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
10 |
|
public function format($format) |
53
|
|
|
{ |
54
|
10 |
|
$this->format = $format; |
55
|
|
|
|
56
|
10 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/* ----------------------------------------------------------------- |
60
|
|
|
| Main Methods |
61
|
|
|
| ----------------------------------------------------------------- |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create and add a sitemap to the collection. |
66
|
|
|
* |
67
|
|
|
* @param string $name |
68
|
|
|
* @param callable $callback |
69
|
|
|
* |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
2 |
|
public function create($name, callable $callback) |
73
|
|
|
{ |
74
|
2 |
|
return $this->add($name, tap((new Sitemap)->setPath($name), $callback)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add a sitemap to the collection. |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* @param \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap $sitemap |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
32 |
|
public function add($name, SitemapContract $sitemap) |
86
|
|
|
{ |
87
|
32 |
|
$this->sitemaps->put($name, $sitemap); |
88
|
|
|
|
89
|
32 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the sitemaps collection. |
94
|
|
|
* |
95
|
|
|
* @return \Illuminate\Support\Collection |
96
|
|
|
*/ |
97
|
10 |
|
public function all() |
98
|
|
|
{ |
99
|
10 |
|
return $this->sitemaps; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get a sitemap instance. |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* @param mixed|null $default |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
4 |
|
public function get($name, $default = null) |
111
|
|
|
{ |
112
|
4 |
|
return $this->sitemaps->get($name, $default); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Check if a sitemap exists. |
117
|
|
|
* |
118
|
|
|
* @param string $name |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
2 |
|
public function has($name) |
123
|
|
|
{ |
124
|
2 |
|
return $this->sitemaps->has($name); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Remove a sitemap from the collection by key. |
129
|
|
|
* |
130
|
|
|
* @param string|array $names |
131
|
|
|
* |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
2 |
|
public function forget($names) |
135
|
|
|
{ |
136
|
2 |
|
$this->sitemaps->forget($names); |
137
|
|
|
|
138
|
2 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the sitemaps count. |
143
|
|
|
* |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
8 |
|
public function count() |
147
|
|
|
{ |
148
|
8 |
|
return $this->sitemaps->count(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Render the sitemaps. |
153
|
|
|
* |
154
|
|
|
* @param string $name |
155
|
|
|
* |
156
|
|
|
* @return string|null |
157
|
|
|
*/ |
158
|
22 |
|
public function render($name = null) |
159
|
|
|
{ |
160
|
22 |
|
return SitemapBuilder::make()->build($name, $this->sitemaps, $this->format); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Save the sitemaps. |
165
|
|
|
* |
166
|
|
|
* @param string $path |
167
|
|
|
* @param string|null $name |
168
|
|
|
* |
169
|
|
|
* @return self |
170
|
|
|
*/ |
171
|
8 |
|
public function save($path, $name = null) |
172
|
|
|
{ |
173
|
8 |
|
if ($this->sitemaps->isEmpty()) |
174
|
2 |
|
return $this; |
175
|
|
|
|
176
|
8 |
|
file_put_contents($path, $this->render($name)); |
177
|
|
|
|
178
|
|
|
/** @var \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap $sitemap */ |
179
|
8 |
|
foreach ($this->sitemaps as $key => $sitemap) { |
180
|
8 |
|
if ($sitemap->isExceeded()) |
181
|
8 |
|
$this->saveMultiple($path, $sitemap); |
182
|
|
|
} |
183
|
|
|
|
184
|
8 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Render the Http response. |
189
|
|
|
* |
190
|
|
|
* @param string $name |
191
|
|
|
* @param int $status |
192
|
|
|
* @param array $headers |
193
|
|
|
* |
194
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
195
|
|
|
*/ |
196
|
2 |
|
public function respond($name = null, $status = 200, array $headers = []) |
197
|
|
|
{ |
198
|
2 |
|
return response($this->render($name), $status, array_merge($this->getResponseHeaders(), $headers)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get the collection of items as a plain array. |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
4 |
|
public function toArray() |
207
|
|
|
{ |
208
|
4 |
|
return $this->all()->toArray(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get the collection of sitemaps as JSON. |
213
|
|
|
* |
214
|
|
|
* @param int $options |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
2 |
|
public function toJson($options = 0) |
219
|
|
|
{ |
220
|
2 |
|
return json_encode($this->jsonSerialize(), $options); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Convert the object into something JSON serializable. |
225
|
|
|
* |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
2 |
|
public function jsonSerialize() |
229
|
|
|
{ |
230
|
2 |
|
return $this->toArray(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Save multiple sitemap. |
235
|
|
|
* |
236
|
|
|
* @param string $path |
237
|
|
|
* @param \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap $sitemap |
238
|
|
|
*/ |
239
|
2 |
|
private function saveMultiple($path, $sitemap) |
240
|
|
|
{ |
241
|
2 |
|
$pathInfo = pathinfo($path); |
242
|
2 |
|
$chunks = $sitemap->chunk(); |
243
|
|
|
|
244
|
2 |
|
foreach ($chunks as $key => $item) { |
245
|
2 |
|
file_put_contents( |
246
|
2 |
|
$pathInfo['dirname'].DS.$pathInfo['filename'].'-'.$key.'.'.$pathInfo['extension'], |
247
|
2 |
|
SitemapBuilder::make()->build($key, $chunks, $this->format) |
248
|
|
|
); |
249
|
|
|
} |
250
|
2 |
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get the response header. |
254
|
|
|
* |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
2 |
|
protected function getResponseHeaders() |
258
|
|
|
{ |
259
|
2 |
|
return array_get([ |
260
|
2 |
|
'xml' => ['Content-Type' => 'application/xml'], |
261
|
|
|
'rss' => ['Content-Type' => 'application/rss+xml'], |
262
|
|
|
'txt' => ['Content-Type' => 'text/plain'], |
263
|
2 |
|
], $this->format, []); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|