You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.0 KiB
42 lines
1.0 KiB
<?php
|
|
|
|
namespace App\Livewire\Concerns;
|
|
|
|
use App\Attributes\Seo;
|
|
use App\UI\Actions\SetSeoMetadata;
|
|
use Livewire\Component;
|
|
use ReflectionClass;
|
|
|
|
/**
|
|
* @mixin Component
|
|
*/
|
|
trait HasSeoAttributes
|
|
{
|
|
/**
|
|
* Unified logic to apply SEO from PHP 8 Attributes.
|
|
*/
|
|
public function applySeoMetadata(SetSeoMetadata $setSeo): void
|
|
{
|
|
$reflection = new ReflectionClass($this);
|
|
|
|
// 1. Check Class (Livewire default) or Method (Controller default)
|
|
$attribute = $reflection->getAttributes(Seo::class)[0]
|
|
?? $reflection->getMethod('render')->getAttributes(Seo::class)[0]
|
|
?? null;
|
|
|
|
if ($attribute) {
|
|
/** @var Seo $seo */
|
|
$seo = $attribute->newInstance();
|
|
|
|
$setSeo->applySeo($seo, $this, request());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Livewire Lifecycle hook: triggers on every render.
|
|
*/
|
|
public function renderingHasSeoAttributes(SetSeoMetadata $setSeo): void
|
|
{
|
|
$this->applySeoMetadata($setSeo);
|
|
}
|
|
}
|
|
|