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