1
|
|
|
<?php namespace Arcanedev\LaravelSitemap; |
2
|
|
|
|
3
|
|
|
use DOMDocument; |
4
|
|
|
use Illuminate\Support\Str; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class SitemapBuilder |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelSitemap\Tests |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class SitemapBuilder |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Main Methods |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create the builder instance. |
21
|
|
|
* |
22
|
|
|
* @return self |
23
|
|
|
*/ |
24
|
20 |
|
public static function make() |
25
|
|
|
{ |
26
|
20 |
|
return new static(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Build the sitemap. |
31
|
|
|
* |
32
|
|
|
* @param string $name |
33
|
|
|
* @param \Illuminate\Support\Collection $sitemaps |
34
|
|
|
* @param string $format |
35
|
|
|
* |
36
|
|
|
* @return string|null |
37
|
|
|
*/ |
38
|
20 |
|
public function build($name, $sitemaps, $format) |
39
|
|
|
{ |
40
|
20 |
|
if ($sitemaps->isEmpty()) |
41
|
6 |
|
return null; |
42
|
|
|
|
43
|
18 |
|
if (is_null($name)) { |
44
|
14 |
|
return $sitemaps->count() > 1 |
45
|
10 |
|
? static::renderSitemapIndex($format, $sitemaps) |
46
|
14 |
|
: static::renderSitemap($format, $sitemaps->first()); |
47
|
|
|
} |
48
|
|
|
|
49
|
16 |
|
list($name, $key) = Str::contains($name, '.') ? explode('.', $name, 2) : [$name, null]; |
50
|
|
|
|
51
|
16 |
|
if ($sitemaps->has($name)) |
52
|
16 |
|
return static::renderSitemap($format, $sitemaps->get($name), $key); |
53
|
|
|
|
54
|
2 |
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/* ----------------------------------------------------------------- |
58
|
|
|
| Other Methods |
59
|
|
|
| ----------------------------------------------------------------- |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Build a single sitemap item. |
64
|
|
|
* |
65
|
|
|
* @param string $format |
66
|
|
|
* @param \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap|null $sitemap |
67
|
|
|
* @param string|null $key |
68
|
|
|
* |
69
|
|
|
* @return string|null |
70
|
|
|
*/ |
71
|
16 |
|
protected function renderSitemap($format, $sitemap, $key = null) |
72
|
|
|
{ |
73
|
16 |
|
if (is_null($sitemap)) |
74
|
2 |
|
return null; |
75
|
|
|
|
76
|
16 |
|
if ( ! $sitemap->isExceeded()) |
77
|
16 |
|
return static::render($format, 'sitemap', ['sitemap' => $sitemap]); |
78
|
|
|
|
79
|
4 |
|
$chunks = $sitemap->chunk(); |
80
|
|
|
|
81
|
4 |
|
return is_null($key) |
82
|
4 |
|
? static::renderSitemapIndex($format, $chunks) |
83
|
4 |
|
: static::renderSitemap($format, $chunks->get($key)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Render sitemap index. |
88
|
|
|
* |
89
|
|
|
* @param string $format |
90
|
|
|
* @param \Illuminate\Support\Collection $sitemaps |
91
|
|
|
* |
92
|
|
|
* @return null|string |
93
|
|
|
*/ |
94
|
14 |
|
protected function renderSitemapIndex($format, $sitemaps) |
95
|
|
|
{ |
96
|
14 |
|
return static::render($format, 'sitemap-index', compact('sitemaps')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Render the file. |
101
|
|
|
* |
102
|
|
|
* @param string $format |
103
|
|
|
* @param string $type |
104
|
|
|
* @param array $data |
105
|
|
|
* |
106
|
|
|
* @return null|string |
107
|
|
|
*/ |
108
|
18 |
|
protected function render($format, $type, array $data) |
109
|
|
|
{ |
110
|
|
|
switch ($format) { |
111
|
18 |
|
case 'xml': |
112
|
8 |
|
case 'rss': |
113
|
12 |
|
return static::renderXml($format, $type, $data); |
114
|
|
|
|
115
|
6 |
|
case 'txt': |
116
|
4 |
|
return static::renderView($type, $format, $data); |
117
|
|
|
|
118
|
|
|
default: |
119
|
2 |
|
return null; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Render the xml file. |
125
|
|
|
* |
126
|
|
|
* @param string $format |
127
|
|
|
* @param string $type |
128
|
|
|
* @param array $data |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected function renderXml($format, $type, array $data) |
133
|
|
|
{ |
134
|
12 |
|
return tap(new DOMDocument('1.0'), function (DOMDocument $document) use ($format, $type, $data) { |
135
|
12 |
|
$document->preserveWhiteSpace = false; |
136
|
12 |
|
$document->formatOutput = true; |
137
|
12 |
|
$document->loadXML( |
138
|
12 |
|
static::renderView($type, $format, $data) |
139
|
|
|
); |
140
|
12 |
|
})->saveXML(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Render with illuminate. |
145
|
|
|
* |
146
|
|
|
* @param string $type |
147
|
|
|
* @param string $format |
148
|
|
|
* @param array $data |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
16 |
|
protected function renderView($type, $format, array $data) |
153
|
|
|
{ |
154
|
16 |
|
return view("sitemap::{$type}.{$format}", $data)->render(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|