1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\ModelTraits\SpatieTranslatable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
|
7
|
|
|
class SlugService extends \Cviebrock\EloquentSluggable\Services\SlugService |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Slug the current model. |
11
|
|
|
* |
12
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
13
|
|
|
* @param bool $force |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function slug(Model $model, $force = false) |
17
|
|
|
{ |
18
|
|
|
$this->setModel($model); |
19
|
|
|
|
20
|
|
|
$attributes = []; |
21
|
|
|
|
22
|
|
|
foreach ($this->model->sluggable() as $attribute => $config) { |
23
|
|
|
if (is_numeric($attribute)) { |
24
|
|
|
$attribute = $config; |
25
|
|
|
$config = $this->getConfiguration(); |
26
|
|
|
} else { |
27
|
|
|
$config = $this->getConfiguration($config); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$slug = $this->buildSlug($attribute, $config, $force); |
31
|
|
|
|
32
|
|
|
// customized for Backpack using SpatieTranslatable |
33
|
|
|
// save the attribute as a JSON |
34
|
|
|
$this->model->setAttribute($attribute.'->'.$model->getLocale(), $slug); |
35
|
|
|
|
36
|
|
|
$attributes[] = $attribute; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->model->isDirty($attributes); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Checks if the slug should be unique, and makes it so if needed. |
44
|
|
|
* |
45
|
|
|
* @param string $slug |
46
|
|
|
* @param array $config |
47
|
|
|
* @param string $attribute |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function makeSlugUnique($slug, array $config, $attribute) |
51
|
|
|
{ |
52
|
|
|
if (! $config['unique']) { |
53
|
|
|
return $slug; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$separator = $config['separator']; |
57
|
|
|
|
58
|
|
|
// find all models where the slug is like the current one |
59
|
|
|
$list = $this->getExistingSlugs($slug, $attribute, $config); |
60
|
|
|
|
61
|
|
|
// if ... |
62
|
|
|
// a) the list is empty, or |
|
|
|
|
63
|
|
|
// b) our slug isn't in the list |
64
|
|
|
// ... we are okay |
65
|
|
|
if ( |
66
|
|
|
$list->count() === 0 || |
67
|
|
|
$list->contains($slug) === false |
68
|
|
|
) { |
69
|
|
|
return $slug; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// if our slug is in the list, but |
73
|
|
|
// a) it's for our model, or |
74
|
|
|
// b) it looks like a suffixed version of our slug |
75
|
|
|
// ... we are also okay (use the current slug) |
76
|
|
|
if ($list->has($this->model->getKey())) { |
77
|
|
|
$currentSlug = $list->get($this->model->getKey()); |
78
|
|
|
|
79
|
|
|
if ( |
80
|
|
|
$currentSlug === $slug || |
81
|
|
|
strpos($currentSlug, $slug) === 0 |
82
|
|
|
) { |
83
|
|
|
return $currentSlug; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$method = $config['uniqueSuffix']; |
88
|
|
|
if ($method === null) { |
89
|
|
|
$suffix = $this->generateSuffix($slug, $separator, $list); |
90
|
|
|
} elseif (is_callable($method)) { |
91
|
|
|
$suffix = call_user_func($method, $slug, $separator, $list); |
92
|
|
|
} else { |
93
|
|
|
throw new \UnexpectedValueException('Sluggable "reserved" for '.get_class($this->model).':'.$attribute.' is not null, an array, or a closure that returns null/array.'); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $slug.$separator.$suffix; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.