*/ private array $adapters; /** * @param iterable $adapters */ public function __construct(iterable $adapters) { $this->adapters = $this->sortAdaptersByPriority($adapters); } /** * Get storage adapter that can handle the given persistence identifier * * Uses Chain of Responsibility pattern to find the first adapter * (in priority order) that supports the given identifier. * * @param string $identifier Persistence identifier (e.g., "EXT:my_extension/Forms/contact.form.yaml", "1:/forms/contact.form.yaml") * @throws \RuntimeException if no adapter can handle the identifier */ public function getAdapterForIdentifier(string $identifier): StorageAdapterInterface { foreach ($this->adapters as $adapter) { if ($adapter->supports($identifier)) { return $adapter; } } throw new \RuntimeException( sprintf( 'No storage adapter found that can handle identifier "%s". Registered adapters: %s', $identifier, implode(', ', array_map(fn($a) => $a->getTypeIdentifier(), $this->adapters)) ), 1731672000 ); } /** * Get adapter by type identifier * * @param string $typeIdentifier Type identifier (e.g., 'extension', 'filemount') * @throws \InvalidArgumentException if no adapter with this type identifier exists */ public function getAdapterByType(string $typeIdentifier): StorageAdapterInterface { foreach ($this->adapters as $adapter) { if ($adapter->getTypeIdentifier() === $typeIdentifier) { return $adapter; } } throw new \InvalidArgumentException( sprintf( 'No storage adapter found with type identifier "%s". Available types: %s', $typeIdentifier, implode(', ', array_map(fn($a) => $a->getTypeIdentifier(), $this->adapters)) ), 1731672002 ); } /** * Check if an adapter with the given type identifier exists */ public function hasAdapterType(string $typeIdentifier): bool { foreach ($this->adapters as $adapter) { if ($adapter->getTypeIdentifier() === $typeIdentifier) { return true; } } return false; } /** * Get all registered storage adapters * * @return list */ public function getAllAdapters(): array { return $this->adapters; } /** * Get all registered storage type identifiers * * @return list */ public function getRegisteredTypeIdentifiers(): array { return array_map( fn(StorageAdapterInterface $adapter) => $adapter->getTypeIdentifier(), $this->adapters ); } /** * Sort adapters by priority (highest first) * * @param iterable $adapters * @return list */ private function sortAdaptersByPriority(iterable $adapters): array { $sortedAdapters = [...$adapters]; usort( $sortedAdapters, fn(StorageAdapterInterface $a, StorageAdapterInterface $b) => $b->getPriority() <=> $a->getPriority() ); return $sortedAdapters; } }