50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace AKSRT\WidgetSystem;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
/**
|
|
* Lazy, immutable registry for all modern widgets.
|
|
*/
|
|
final class WidgetRegistry {
|
|
/** @var array<string, WidgetDefinition>|null */
|
|
private static ?array $definitions = null;
|
|
|
|
/** @return array<string, WidgetDefinition> */
|
|
public static function all(): array {
|
|
if (self::$definitions === null) {
|
|
self::$definitions = [];
|
|
foreach (WidgetCatalog::definitions() as $raw) {
|
|
$definition = new WidgetDefinition($raw);
|
|
if (isset(self::$definitions[$definition->slug()])) {
|
|
throw new \LogicException('Duplicate widget slug: ' . $definition->slug());
|
|
}
|
|
self::$definitions[$definition->slug()] = $definition;
|
|
}
|
|
}
|
|
return self::$definitions;
|
|
}
|
|
|
|
public static function get(string $slug): ?WidgetDefinition {
|
|
return self::all()[$slug] ?? null;
|
|
}
|
|
|
|
public static function get_by_id_base(string $id_base): ?WidgetDefinition {
|
|
$prefix = 'aksrt_widget_';
|
|
if (!str_starts_with($id_base, $prefix)) {
|
|
return null;
|
|
}
|
|
return self::get(substr($id_base, strlen($prefix)));
|
|
}
|
|
|
|
public static function count(): int {
|
|
return count(self::all());
|
|
}
|
|
|
|
public static function reset(): void {
|
|
self::$definitions = null;
|
|
}
|
|
}
|
|
|