generated from archtechx/template
-
Notifications
You must be signed in to change notification settings - Fork 24
/
SEOManager.php
309 lines (250 loc) · 8.69 KB
/
SEOManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
declare(strict_types=1);
namespace ArchTech\SEO;
use Closure;
use Illuminate\Support\Str;
/**
* @method $this title(string $title = null, ...$args) Set the title.
* @method $this description(string $description = null, ...$args) Set the description.
* @method $this url(string $url = null, ...$args) Set the canonical URL.
* @method $this site(string $site = null, ...$args) Set the site name.
* @method $this image(string $url = null, ...$args) Set the cover image.
* @method $this type(string $type = null, ...$args) Set the page type.
* @method $this twitter(enabled $bool = true, ...$args) Enable the Twitter extension.
* @method $this twitterSite(string $username = null, ...$args) Set the Twitter author.
* @method $this twitterTitle(string $title = null, ...$args) Set the Twitter title.
* @method $this twitterDescription(string $description = null, ...$args) Set the Twitter description.
* @method $this twitterImage(string $url = null, ...$args) Set the Twitter cover image.
*/
class SEOManager
{
/** Value modifiers. */
protected array $modifiers = [];
/** Default values. */
protected array $defaults = [];
/** User-configured values. */
protected array $values = [];
/** List of extensions. */
protected array $extensions = [
'twitter' => false,
];
/** Metadata for additional features. */
protected array $meta = [];
/** Extra head tags. */
protected array $tags = [];
/** Get all used values. */
public function all(): array
{
return collect($this->getKeys())
->mapWithKeys(fn (string $key) => [$key => $this->get($key)])
->toArray();
}
/** Get a list of used keys. */
protected function getKeys(): array
{
return collect([
'site', 'title', 'image', 'description', 'url', 'type',
'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description',
])
->merge(array_keys($this->defaults))
->merge(array_keys($this->values))
->unique()
->filter(function (string $key) {
if (count($parts = explode('.', $key)) > 1) {
if (isset($this->extensions[$parts[0]])) {
// Is the extension allowed?
return $this->extensions[$parts[0]];
}
return false;
}
return true;
})
->toArray();
}
/** Get a modified value. */
protected function modify(string $key): string|null
{
return isset($this->modifiers[$key])
? $this->modifiers[$key](value($this->values[$key]))
: value($this->values[$key]);
}
/**
* Set one or more values.
*
* @param string|array<string, string> $key
*/
public function set(string|array $key, string|Closure|null $value = null): string|array|null
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->set($k, $v);
}
return collect($key)
->keys()
->mapWithKeys(fn (string $key) => [$key => $this->get($key)])
->toArray();
}
$this->values[$key] = $value;
if (Str::contains($key, '.')) {
$this->extension(Str::before($key, '.'), enabled: true);
}
return $this->get($key);
}
/** Resolve a value. */
public function get(string $key): string|null
{
return isset($this->values[$key])
? $this->modify($key)
: value($this->defaults[$key] ?? (
Str::contains($key, '.') ? $this->get(Str::after($key, '.')) : null
));
}
/** Get a value without modifications. */
public function raw(string $key): string|null
{
return isset($this->values[$key])
? value($this->values[$key])
: value($this->defaults[$key] ?? (
Str::contains($key, '.') ? $this->get(Str::after($key, '.')) : null
));
}
/** Configure an extension. */
public function extension(string $name, bool $enabled = true, string $view = null): static
{
$this->extensions[$name] = $enabled;
if ($view) {
$this->meta("extensions.$name.view", $view);
}
return $this;
}
/** Get a list of enabled extensions. */
public function extensions(): array
{
return collect($this->extensions)
->filter(fn (bool $enabled) => $enabled)
->keys()
->mapWithKeys(fn (string $extension) => [
$extension => $this->meta("extensions.$extension.view") ?? ('seo::extensions.' . $extension),
])
->toArray();
}
/** Configure or use Flipp. */
public function flipp(string $alias, string|array $data = null): string|static
{
if (is_string($data)) {
$this->meta("flipp.templates.$alias", $data);
return $this;
}
if ($data === null) {
$data = [
'title' => $this->raw('title'),
'description' => $this->raw('description'),
];
}
$query = base64_encode(json_encode($data, JSON_THROW_ON_ERROR));
/** @var string $template */
$template = $this->meta("flipp.templates.$alias");
$signature = hash_hmac('sha256', $template . $query, config('services.flipp.key'));
return $this->set('image', "https://s.useflipp.com/{$template}.png?s={$signature}&v={$query}");
}
/** Enable favicon extension. */
public function favicon(): static
{
$this->extensions['favicon'] = true;
return $this;
}
/** Append canonical URL tags to the document head. */
public function withUrl(): static
{
$this->url(request()->url());
return $this;
}
/** Get all extra head tags. */
public function tags(): array
{
return $this->tags;
}
/** Has a specific tag been set? */
public function hasRawTag(string $key): bool
{
return isset($this->tags[$key]) && ($this->tags[$key] !== null);
}
/** Has a specific meta tag been set? */
public function hasTag(string $property): bool
{
return $this->hasRawTag("meta.{$property}");
}
/** Add a head tag. */
public function rawTag(string $key, string $tag = null): static
{
$tag ??= $key;
$this->tags[$key] = $tag;
return $this;
}
/** Add a meta tag. */
public function tag(string $property, string $content): static
{
$this->rawTag("meta.{$property}", "<meta property=\"{$property}\" content=\"{$content}\" />");
return $this;
}
/**
* Get or set metadata.
* @param string|array $key The key or key-value pair being set.
* @param string|array|null $value The value (if a single key is provided).
* @return $this|string|null
*/
public function meta(string|array $key, string|array $value = null): mixed
{
if (is_array($key)) {
/** @var array<string, string> $key */
foreach ($key as $k => $v) {
$this->meta($k, $v);
}
return $this;
}
if ($value === null) {
return data_get($this->meta, $key);
}
data_set($this->meta, $key, $value);
return $this;
}
/** Handle magic method calls. */
public function __call(string $name, array $arguments): string|array|null|static
{
if (isset($this->extensions[$name])) {
return $this->extension($name, $arguments[0] ?? true);
}
$key = Str::snake($name, '.');
if (isset($arguments['default'])) {
$this->defaults[$key] = $arguments['default'];
}
if (isset($arguments['modifier'])) {
$this->modifiers[$key] = $arguments['modifier'];
}
// modify: ... is an alias for modifier: ...
if (isset($arguments['modify'])) {
$this->modifiers[$key] = $arguments['modify'];
}
if (isset($arguments[0])) {
$this->set($key, $arguments[0]);
}
if (isset($arguments[0]) || isset($arguments['default']) || isset($arguments['modifier']) || isset($arguments['modify'])) {
return $this;
}
return $this->get($key);
}
/** Handle magic get. */
public function __get(string $key): string|null
{
return $this->get(Str::snake($key, '.'));
}
/**
* Handle magic set.
*
* @return string|array|null
*/
public function __set(string $key, string $value)
{
return $this->set(Str::snake($key, '.'), $value);
}
}