1
|
|
|
<?php namespace Arcanedev\LaravelSitemap\Helpers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSitemap\Contracts\SitemapGenerator as SitemapGeneratorContract; |
4
|
|
|
use Arcanedev\LaravelSitemap\Contracts\SitemapStyler as SitemapStylerContract; |
5
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache; |
6
|
|
|
use Illuminate\Contracts\View\Factory as View; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SitemapGenerator |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\LaravelSitemap\Helpers |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class SitemapGenerator implements SitemapGeneratorContract |
16
|
|
|
{ |
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
18
|
|
|
| Properties |
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
20
|
|
|
*/ |
21
|
|
|
/** @var \Illuminate\Contracts\View\Factory */ |
22
|
|
|
protected $view; |
23
|
|
|
|
24
|
|
|
/** @var \Illuminate\Contracts\Cache\Repository */ |
25
|
|
|
protected $cache; |
26
|
|
|
|
27
|
|
|
/** @var \Arcanedev\LaravelSitemap\Contracts\SitemapStyler */ |
28
|
|
|
protected $styler; |
29
|
|
|
|
30
|
|
|
/** @var array */ |
31
|
|
|
protected $contentTypes = [ |
32
|
|
|
'html' => 'text/html', |
33
|
|
|
'ror-rdf' => 'text/rdf+xml; charset=utf-8', |
34
|
|
|
'ror-rss' => 'text/rss+xml; charset=utf-8', |
35
|
|
|
'txt' => 'text/plain', |
36
|
|
|
'sitemapindex' => 'text/xml; charset=utf-8', |
37
|
|
|
'xml' => 'text/xml; charset=utf-8' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/* ------------------------------------------------------------------------------------------------ |
41
|
|
|
| Constructor |
42
|
|
|
| ------------------------------------------------------------------------------------------------ |
43
|
|
|
*/ |
44
|
|
|
/** |
45
|
|
|
* SitemapGenerator constructor. |
46
|
|
|
* |
47
|
|
|
* @param \Arcanedev\LaravelSitemap\Contracts\SitemapStyler $styler |
48
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache |
49
|
|
|
* @param \Illuminate\Contracts\View\Factory $view |
50
|
|
|
*/ |
51
|
80 |
|
public function __construct( |
52
|
|
|
SitemapStylerContract $styler, |
53
|
|
|
Cache $cache, |
54
|
|
|
View $view |
55
|
|
|
) { |
56
|
80 |
|
$this->styler = $styler; |
57
|
80 |
|
$this->cache = $cache; |
58
|
80 |
|
$this->view = $view; |
59
|
80 |
|
} |
60
|
|
|
|
61
|
|
|
/* ------------------------------------------------------------------------------------------------ |
62
|
|
|
| Getters & Setters |
63
|
|
|
| ------------------------------------------------------------------------------------------------ |
64
|
|
|
*/ |
65
|
|
|
/** |
66
|
|
|
* Get the sitemap styles location. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
16 |
|
public function getStylesLocation() |
71
|
|
|
{ |
72
|
16 |
|
return $this->styler->getLocation(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the sitemap styles location. |
77
|
|
|
* |
78
|
|
|
* @param string $location |
79
|
|
|
* |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
16 |
|
public function setStylesLocation($location) |
83
|
|
|
{ |
84
|
16 |
|
$this->styler->setLocation($location); |
85
|
|
|
|
86
|
16 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the use styles value. |
91
|
|
|
* |
92
|
|
|
* @param bool $useStyles |
93
|
|
|
* |
94
|
|
|
* @return self |
95
|
|
|
*/ |
96
|
16 |
|
public function setUseStyles($useStyles) |
97
|
|
|
{ |
98
|
16 |
|
$this->styler->setEnabled($useStyles); |
99
|
|
|
|
100
|
16 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* ------------------------------------------------------------------------------------------------ |
104
|
|
|
| Main Functions |
105
|
|
|
| ------------------------------------------------------------------------------------------------ |
106
|
|
|
*/ |
107
|
|
|
/** |
108
|
|
|
* @param array $data |
109
|
|
|
* @param string $format |
110
|
|
|
* @param string|null $style |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
8 |
|
public function generate(array $data, $format = 'xml', $style = null) |
115
|
|
|
{ |
116
|
|
|
// check if styles are enabled |
117
|
8 |
|
$data['style'] = $this->styler->get($format, $style); |
118
|
|
|
|
119
|
8 |
|
$content = $this->getContent($data, $format); |
120
|
8 |
|
$headers = $this->getHeaders($format); |
121
|
|
|
|
122
|
8 |
|
return compact('content', 'headers'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* ------------------------------------------------------------------------------------------------ |
126
|
|
|
| Check Functions |
127
|
|
|
| ------------------------------------------------------------------------------------------------ |
128
|
|
|
*/ |
129
|
|
|
/** |
130
|
|
|
* Check is styles is enabled. |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
16 |
|
public function isStylesEnabled() |
135
|
|
|
{ |
136
|
16 |
|
return $this->styler->isEnabled(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/* ------------------------------------------------------------------------------------------------ |
140
|
|
|
| Other Functions |
141
|
|
|
| ------------------------------------------------------------------------------------------------ |
142
|
|
|
*/ |
143
|
|
|
/** |
144
|
|
|
* Get the rendered content. |
145
|
|
|
* |
146
|
|
|
* @param array $data |
147
|
|
|
* @param string $format |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
8 |
|
private function getContent(array $data, $format) |
152
|
|
|
{ |
153
|
8 |
|
$content = $this->view->make("sitemap::$format", $data)->render(); |
154
|
|
|
|
155
|
8 |
|
return $content; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the headers. |
160
|
|
|
* |
161
|
|
|
* @param string $format |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
8 |
|
protected function getHeaders($format) |
166
|
|
|
{ |
167
|
|
|
return [ |
168
|
8 |
|
'Content-type' => Arr::get($this->contentTypes, $format, 'text/xml; charset=utf-8') |
169
|
6 |
|
]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|