*/ private static array $singletons = []; /** * @deprecated actually just `@internal` - but it might be removed later */ public readonly ?Site $site; public static function backend(): self { return self::asSingleton(new self(ApplicationType::BACKEND)); } public static function frontend(): self { return self::asSingleton(new self(ApplicationType::FRONTEND)); } /** * @internal might be removed later */ public static function frontendSite(?SiteInterface $site): self { if (!$site instanceof Site || is_subclass_of($site, Site::class)) { return self::frontend(); } return self::asSingleton(new self(ApplicationType::FRONTEND, $site->getIdentifier(), $site)); } public static function frontendSiteIdentifier(string $siteIdentifier): self { return self::asSingleton(new self(ApplicationType::FRONTEND, $siteIdentifier)); } public static function from(string $value): self { $parts = GeneralUtility::trimExplode('.', $value, true); $type = ApplicationType::tryFrom($parts[0] ?? ''); $siteIdentifier = $parts[1] ?? null; if ($type === null) { throw new \LogicException( sprintf('Could not resolve application type from "%s"', $value), 1677424928 ); } return self::asSingleton(new self($type, $siteIdentifier)); } public static function reset(): void { self::$singletons = []; } public static function tryFrom(string $value): ?self { try { return self::from($value); } catch (\LogicException) { return null; } } private static function asSingleton(self $self): self { $id = (string)$self; if (!isset(self::$singletons[$id])) { self::$singletons[$id] = $self; } return self::$singletons[$id]; } /** * Use static functions to create singleton instances. */ private function __construct( public readonly ApplicationType $type, public readonly ?string $siteIdentifier = null, ?Site $site = null, ) { $this->site = $site; } public function __toString(): string { $value = $this->type->value; if ($this->siteIdentifier !== null) { $value .= '.' . $this->siteIdentifier; } return $value; } public function isFrontendSite(): bool { return $this->siteIdentifier !== null && $this->type->isFrontend(); } public function jsonSerialize(): string { return (string)$this; } }