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