- makeCustomerForShowPhase8a() nach tests/Pest.php — die Suite läuft damit auch parallel (--parallel) und dateiweise grün - phpunit.xml leert DB_URL, der Guard in TestCase::setUpTraits() bricht bei gesetzter url ab, bevor RefreshDatabase eine Verbindung öffnet - HasUniqueSlug ohne widersprüchliche @var-Annotation Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
4.2 KiB
PHP
149 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Concerns;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Generates a unique slug for an Eloquent model based on a source attribute.
|
|
*
|
|
* Models opt-in by overriding two methods:
|
|
* - {@see slugScopeAttributes()} returns the attribute names whose current
|
|
* model values (or provided overrides) should restrict slug uniqueness.
|
|
* Defaults to an empty array (global uniqueness on the slug column).
|
|
* - {@see slugFallback()} returns the fallback when the source attribute is
|
|
* blank (defaults to "item").
|
|
*
|
|
* The model is expected to expose `slug_column` (defaults to `slug`) and
|
|
* the source attribute name to slugify (defaults to `title` or `name`).
|
|
*
|
|
* Typical usage:
|
|
*
|
|
* $pr->slug = $pr->generateUniqueSlug($pr->title, [
|
|
* 'portal' => $pr->portal,
|
|
* 'language' => $pr->language,
|
|
* ]);
|
|
*
|
|
* @mixin Model
|
|
*/
|
|
trait HasUniqueSlug
|
|
{
|
|
/**
|
|
* Build a unique slug from `$source`. Optionally pass overrides for the
|
|
* scoping attributes (matched against {@see slugScopeAttributes()}).
|
|
*
|
|
* @param array<string, mixed> $scope
|
|
*/
|
|
public function generateUniqueSlug(string $source, array $scope = []): string
|
|
{
|
|
$base = Str::slug($source) ?: $this->slugFallback();
|
|
$slug = $base;
|
|
$suffix = 2;
|
|
|
|
while ($this->slugExists($slug, $scope)) {
|
|
$slug = $base.'-'.$suffix++;
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
|
|
/**
|
|
* Persist the model and recover from a concurrent slug insert by
|
|
* regenerating the next available suffix.
|
|
*
|
|
* @param array<string, mixed> $scope
|
|
* @param array<string, mixed> $options
|
|
*/
|
|
public function saveWithUniqueSlugRetry(
|
|
string $source,
|
|
array $scope = [],
|
|
array $options = [],
|
|
int $maxAttempts = 3,
|
|
): bool {
|
|
for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) {
|
|
try {
|
|
return $this->save($options);
|
|
} catch (QueryException $exception) {
|
|
if ($attempt === $maxAttempts || ! $this->isSlugUniqueConstraintViolation($exception)) {
|
|
throw $exception;
|
|
}
|
|
|
|
$this->setAttribute(
|
|
$this->slugColumn(),
|
|
$this->generateUniqueSlug($source, $scope),
|
|
);
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $scope
|
|
*/
|
|
protected function slugExists(string $slug, array $scope): bool
|
|
{
|
|
$self = $this;
|
|
|
|
$query = static::query()
|
|
->withoutGlobalScopes()
|
|
->where($this->slugColumn(), $slug);
|
|
|
|
if ($self->exists) {
|
|
$query->where($self->getKeyName(), '!=', $self->getKey());
|
|
}
|
|
|
|
foreach ($this->slugScopeAttributes() as $attribute) {
|
|
$value = array_key_exists($attribute, $scope)
|
|
? $scope[$attribute]
|
|
: $self->getAttribute($attribute);
|
|
|
|
$value !== null
|
|
? $query->where($attribute, $value)
|
|
: $query->whereNull($attribute);
|
|
}
|
|
|
|
$this->applySlugConstraints($query);
|
|
|
|
return $query->exists();
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
protected function slugScopeAttributes(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
protected function slugColumn(): string
|
|
{
|
|
return 'slug';
|
|
}
|
|
|
|
protected function slugFallback(): string
|
|
{
|
|
return 'item';
|
|
}
|
|
|
|
private function isSlugUniqueConstraintViolation(QueryException $exception): bool
|
|
{
|
|
$message = Str::lower($exception->getMessage());
|
|
$isUniqueViolation = in_array((string) $exception->getCode(), ['23000', '23505'], true)
|
|
|| Str::contains($message, ['unique constraint', 'duplicate entry', 'duplicate key']);
|
|
|
|
return $isUniqueViolation && Str::contains($message, $this->slugColumn());
|
|
}
|
|
|
|
/**
|
|
* Hook for models to add additional WHERE constraints (e.g. soft-deleted
|
|
* records). Default: no additional constraints.
|
|
*/
|
|
protected function applySlugConstraints(Builder $query): void
|
|
{
|
|
// no-op
|
|
}
|
|
}
|