|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MichaelRubel\SeoManager\Composers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Illuminate\View\View; |
|
10
|
|
|
use MichaelRubel\SeoManager\Exceptions\ShouldImplementSeoTagInterfaceException; |
|
11
|
|
|
use MichaelRubel\SeoManager\Models\SeoTag; |
|
12
|
|
|
use MichaelRubel\SeoManager\Models\SeoTagContract; |
|
13
|
|
|
|
|
14
|
|
|
class SeoComposer |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Collection|null |
|
18
|
|
|
*/ |
|
19
|
|
|
protected ?Collection $seo_manager; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* SeoComposer constructor. |
|
23
|
|
|
*/ |
|
24
|
6 |
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
6 |
|
$this->seo_manager = $this->getSeoTags(); |
|
27
|
5 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Bind data to the view. |
|
31
|
|
|
* |
|
32
|
|
|
* @param View $view |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
5 |
|
public function compose(View $view): void |
|
36
|
|
|
{ |
|
37
|
5 |
|
$variable = config('seo-manager.variable_name'); |
|
38
|
|
|
|
|
39
|
5 |
|
$view->with( |
|
40
|
5 |
|
is_string($variable) |
|
41
|
5 |
|
? $variable |
|
42
|
5 |
|
: 'seo_manager', |
|
43
|
5 |
|
$this->seo_manager |
|
44
|
|
|
); |
|
45
|
5 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return Collection|null |
|
49
|
|
|
* @throws ShouldImplementSeoTagInterfaceException |
|
50
|
|
|
*/ |
|
51
|
6 |
|
protected function getSeoTags(): ?Collection |
|
52
|
|
|
{ |
|
53
|
6 |
|
$url = Str::start( |
|
54
|
6 |
|
request()->path(), |
|
55
|
6 |
|
'/' |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
6 |
|
$wildcardUrl = $this->wildcard($url); |
|
59
|
|
|
|
|
60
|
6 |
|
$configuredModel = config('seo-manager.model'); |
|
61
|
|
|
|
|
62
|
6 |
|
$model = app( |
|
63
|
6 |
|
is_string($configuredModel) && class_exists($configuredModel) |
|
64
|
6 |
|
? $configuredModel |
|
65
|
6 |
|
: SeoTag::class |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
6 |
|
if (! $model instanceof SeoTagContract) { |
|
69
|
1 |
|
throw new ShouldImplementSeoTagInterfaceException(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
$instance = $this->getInstance($model, $url); |
|
73
|
|
|
|
|
74
|
5 |
|
if (! $instance->exists()) { |
|
75
|
1 |
|
$instance = $this->getInstance($model, $wildcardUrl); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
5 |
|
return $instance->first()?->{$model->getTagsColumnName()}; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $url |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
6 |
|
protected function wildcard(string $url): string |
|
87
|
|
|
{ |
|
88
|
6 |
|
$array = explode('/', $url); |
|
89
|
|
|
|
|
90
|
6 |
|
array_pop($array); |
|
91
|
6 |
|
array_push($array, '*'); |
|
92
|
|
|
|
|
93
|
6 |
|
return implode('/', $array); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param SeoTagContract $model |
|
98
|
|
|
* @param string $url |
|
99
|
|
|
* |
|
100
|
|
|
* @return Builder |
|
101
|
|
|
*/ |
|
102
|
5 |
|
private function getInstance(SeoTagContract $model, string $url): Builder |
|
103
|
|
|
{ |
|
104
|
5 |
|
return $model::where($model->getUrlColumnName(), $url); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|