PSR-4 prefix => relative source directory */ public readonly array $autoload, public readonly ?string $uiEntry, public readonly string $uiMode, /** @var list */ public readonly array $screens, /** @var list view names the bundle overrides, declared under `ui.views` */ public readonly array $views, /** @var array<'panel'|'sdk'|'php', string> */ public readonly array $requirements, /** @var array */ public readonly array $requiredExtensions, /** @var list top-level URL prefixes claimed under `routes.root` */ public readonly array $rootPrefixes = [], /** The Tailwind prefix this extension builds its utilities and theme variables with. */ public readonly ?string $uiPrefix = null, public readonly ?string $icon = null, /** @var 'extension'|'theme' */ public readonly string $type = 'extension', public readonly ?ThemeDefinition $theme = null, ) {} /** * @param ExtensionManifestInput $data * @param array $autoload */ public static function fromValidatedData( string $directory, array $data, array $autoload, ?string $uiEntry, string $uiMode, ?string $provider, ): self { return new self( directory: mb_rtrim($directory, '/\\'), id: $data['id'], name: $data['name'], version: $data['version'], description: is_string($data['description'] ?? null) ? $data['description'] : null, author: is_string($data['author'] ?? null) ? $data['author'] : null, provider: $provider, autoload: $autoload, uiEntry: $uiEntry, uiMode: $uiMode, screens: $data['ui']['screens'] ?? [], views: $data['ui']['views'] ?? [], requirements: array_intersect_key($data['requires'] ?? [], ['panel' => true, 'sdk' => true, 'php' => true]), requiredExtensions: $data['requires']['extensions'] ?? [], rootPrefixes: $data['routes']['root'] ?? [], uiPrefix: $data['ui']['prefix'] ?? null, icon: is_string($data['icon'] ?? null) ? $data['icon'] : null, type: $data['type'] ?? 'extension', theme: ($data['type'] ?? null) === 'theme' ? ThemeDefinition::fromValidatedData($data['theme'] ?? []) : null, ); } /** * Whether two PSR-4 prefixes can serve the same class: one equals or contains the * other. PHP class names are case-insensitive, so the comparison is too. */ public static function namespacesOverlap(string $first, string $second): bool { $first = mb_strtolower($first); $second = mb_strtolower($second); return str_starts_with($first, $second) || str_starts_with($second, $first); } /** Whether a Tailwind prefix is well formed and not reserved; uniqueness is compared when an extension is enabled. */ public static function isUsableUiPrefix(string $prefix): bool { return preg_match(self::UI_PREFIX_REGEX, $prefix) === 1 && ! in_array($prefix, self::RESERVED_UI_PREFIXES, true); } /** * The prefix suggested for an extension id: the initials of its words (`hello-world` * gives `hw`), else its letters, else its letters followed by `ui` when both are * too short or reserved. */ public static function defaultUiPrefix(string $id): string { $words = []; foreach (explode('-', $id) as $word) { $word = implode('', array_filter(mb_str_split($word), ctype_lower(...))); if ($word !== '') { $words[] = $word; } } $letters = implode('', $words); $fallback = mb_substr($letters, 0, 10).'ui'; $initials = implode('', array_map(fn (string $word): string => mb_substr($word, 0, 1), $words)); return array_find([mb_substr($initials, 0, 12), mb_substr($letters, 0, 12)], self::isUsableUiPrefix(...)) ?? $fallback; } public function hasUi(): bool { return $this->uiEntry !== null; } /** @phpstan-assert-if-true !null $this->theme */ public function isTheme(): bool { return $this->theme instanceof ThemeDefinition; } /** Whether the package ships a browser build to publish: a bundle, a theme stylesheet or both. */ public function hasAssets(): bool { return $this->hasUi() || $this->theme?->stylesheet !== null; } /** * The extensions this one needs enabled: its `requires.extensions`, and for a theme the * theme it extends unless that is the built-in classic theme. * * @return array id => version constraint */ public function dependencies(): array { $parent = $this->theme?->extends; return $parent === null || $parent === ThemeRepository::CLASSIC ? $this->requiredExtensions : $this->requiredExtensions + [$parent => '*']; } public function iconName(): ?string { return $this->icon !== null && preg_match(self::ICON_REGEX, $this->icon) === 1 ? $this->icon : null; } public function iconFile(): ?string { return $this->icon !== null && preg_match(self::ICON_FILE_REGEX, $this->icon) === 1 ? $this->icon : null; } public function path(string ...$parts): string { return $this->directory.($parts === [] ? '' : DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts)); } }