|
1
|
|
|
<?php namespace Arcanedev\LaravelSitemap\Entities; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap as SitemapContract; |
|
4
|
|
|
use Illuminate\Support\Collection; |
|
5
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\Url as UrlContract; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Sitemap |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\LaravelSitemap\Entities |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Sitemap implements SitemapContract |
|
14
|
|
|
{ |
|
15
|
|
|
/* ----------------------------------------------------------------- |
|
16
|
|
|
| Properties |
|
17
|
|
|
| ----------------------------------------------------------------- |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** @var string|null */ |
|
21
|
|
|
protected $path; |
|
22
|
|
|
|
|
23
|
|
|
/** @var \Illuminate\Support\Collection */ |
|
24
|
|
|
protected $urls; |
|
25
|
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
|
27
|
|
|
| Constructor |
|
28
|
|
|
| ----------------------------------------------------------------- |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Sitemap constructor. |
|
33
|
|
|
*/ |
|
34
|
50 |
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
50 |
|
$this->urls = new Collection; |
|
37
|
50 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/* ----------------------------------------------------------------- |
|
40
|
|
|
| Main Methods |
|
41
|
|
|
| ----------------------------------------------------------------- |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set the sitemap path. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $path |
|
48
|
|
|
* |
|
49
|
|
|
* @return self |
|
50
|
|
|
*/ |
|
51
|
30 |
|
public function setPath($path) |
|
52
|
|
|
{ |
|
53
|
30 |
|
$this->path = $path; |
|
54
|
|
|
|
|
55
|
30 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the sitemap path. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string|null |
|
62
|
|
|
*/ |
|
63
|
12 |
|
public function getPath() |
|
64
|
|
|
{ |
|
65
|
12 |
|
return $this->path; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the sitemap's URLs. |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Illuminate\Support\Collection |
|
72
|
|
|
*/ |
|
73
|
32 |
|
public function getUrls() |
|
74
|
|
|
{ |
|
75
|
32 |
|
return $this->urls; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set the URLs Collection. |
|
80
|
|
|
* |
|
81
|
|
|
* @param \Illuminate\Support\Collection $urls |
|
82
|
|
|
* |
|
83
|
|
|
* @return self |
|
84
|
|
|
*/ |
|
85
|
6 |
|
public function setUrls(Collection $urls) |
|
86
|
|
|
{ |
|
87
|
6 |
|
$this->urls = $urls; |
|
88
|
|
|
|
|
89
|
6 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get a URL instance by its loc. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $loc |
|
96
|
|
|
* @param mixed|null $default |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Arcanedev\LaravelSitemap\Entities\Url|null |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function getUrl($loc, $default = null) |
|
101
|
|
|
{ |
|
102
|
2 |
|
return $this->getUrls()->get($loc, $default); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Add a sitemap URL to the collection. |
|
107
|
|
|
* |
|
108
|
|
|
* @param \Arcanedev\LaravelSitemap\Contracts\Entities\Url $url |
|
109
|
|
|
* |
|
110
|
|
|
* @return $this |
|
111
|
|
|
*/ |
|
112
|
42 |
|
public function add(UrlContract $url) |
|
113
|
|
|
{ |
|
114
|
42 |
|
$this->urls->put($url->getLoc(), $url); |
|
115
|
|
|
|
|
116
|
42 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Create and Add a sitemap URL to the collection. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $loc |
|
123
|
|
|
* @param callable $callback |
|
124
|
|
|
* |
|
125
|
|
|
* @return self |
|
126
|
|
|
*/ |
|
127
|
28 |
|
public function create($loc, callable $callback) |
|
128
|
|
|
{ |
|
129
|
28 |
|
return $this->add(tap(Url::make($loc), $callback)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Check if the url exists in the sitemap items. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $url |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
4 |
|
public function has($url) |
|
140
|
|
|
{ |
|
141
|
4 |
|
return $this->urls->has($url); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the urls' count. |
|
146
|
|
|
* |
|
147
|
|
|
* @return int |
|
148
|
|
|
*/ |
|
149
|
30 |
|
public function count() |
|
150
|
|
|
{ |
|
151
|
30 |
|
return $this->urls->count(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get the collection of items as a plain array. |
|
156
|
|
|
* |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
10 |
|
public function toArray() |
|
160
|
|
|
{ |
|
161
|
10 |
|
return $this->getUrls()->values()->toArray(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get the sitemap and its urls as JSON. |
|
166
|
|
|
* |
|
167
|
|
|
* @param int $options |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
2 |
|
public function toJson($options = 0) |
|
172
|
|
|
{ |
|
173
|
2 |
|
return json_encode($this->jsonSerialize(), $options); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Convert the object into something JSON serializable. |
|
178
|
|
|
* |
|
179
|
|
|
* @return array |
|
180
|
|
|
*/ |
|
181
|
2 |
|
public function jsonSerialize() |
|
182
|
|
|
{ |
|
183
|
2 |
|
return $this->toArray(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Check if the number of URLs is exceeded. |
|
188
|
|
|
* |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
20 |
|
public function isExceeded() |
|
192
|
|
|
{ |
|
193
|
20 |
|
return $this->count() > $this->getMaxSize(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Chunk the sitemap to multiple chunks. |
|
198
|
|
|
* |
|
199
|
|
|
* @return \Illuminate\Support\Collection |
|
200
|
|
|
*/ |
|
201
|
|
|
public function chunk() |
|
202
|
|
|
{ |
|
203
|
4 |
|
return $this->urls->chunk($this->getMaxSize())->mapWithKeys(function ($item, $index) { |
|
204
|
4 |
|
$pathInfo = pathinfo($this->getPath()); |
|
205
|
4 |
|
$index = $index + 1; |
|
206
|
4 |
|
$path = $pathInfo['dirname'].'/'.$pathInfo['filename'].'-'.$index.'.'.$pathInfo['extension']; |
|
207
|
|
|
|
|
208
|
4 |
|
return [$index => (new Sitemap)->setPath($path)->setUrls($item)]; |
|
209
|
4 |
|
}); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/* ----------------------------------------------------------------- |
|
213
|
|
|
| Other Methods |
|
214
|
|
|
| ----------------------------------------------------------------- |
|
215
|
|
|
*/ |
|
216
|
|
|
|
|
217
|
20 |
|
protected function getMaxSize() |
|
218
|
|
|
{ |
|
219
|
20 |
|
return config('sitemap.urls-max-size', 50000); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|