Loheide-eu-mitarbeiterplatt.../app/Layout/HeaderItems.php
Friederich Loheide f3eebbef0f
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m31s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m44s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m18s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m35s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m38s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 9m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m28s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 45s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m27s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m13s
Kopfzeilen-Symbole für Plugins (0.39.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 16:13:33 +00:00

50 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Layout;
use App\Models\User;
use App\Plugins\PluginAccess;
use Throwable;
/**
* Symbole von Plugins in der Kopfzeile, links neben der Glocke – z. B. „Nachrichten“ mit
* Zahl der ungelesenen. Komponente: „slug::Name“ aus resources/js/header/Name.vue.
*
* app(HeaderItems::class)->add('chat', 'chat::Unread', fn (User $user) => ['unread' => 3], sort: 10);
*
* Liefert der Callback null, erscheint das Symbol für diese Person nicht.
*/
class HeaderItems
{
/** @var array<string, array{component: string, props: callable, sort: int}> */
private array $items = [];
public function add(string $key, string $component, callable $props, int $sort = 50): void
{
$this->items[$key] = compact('component', 'props', 'sort');
}
/**
* @return list<array{key: string, component: string, props: array<string, mixed>}>
*/
public function resolve(User $user): array
{
$access = app(PluginAccess::class);
return collect($this->items)
->filter(fn ($item, $key) => $access->allows($key, $user))
->sortBy('sort')
->map(function (array $item, string $key) use ($user) {
try {
$props = ($item['props'])($user);
} catch (Throwable $e) {
report($e);
return null;
}
return $props === null ? null : ['key' => $key, 'component' => $item['component'], 'props' => $props];
})
->filter()->values()->all();
}
}