*/ private array $handlers = []; /** * @param iterable $handlers */ public function __construct( iterable $handlers, private readonly EventDispatcherInterface $eventDispatcher, ) { foreach ($handlers as $handler) { $this->handlers[$handler->getIdentifier()] = $handler; } } /** * Get a handler by its identifier * * @throws \InvalidArgumentException if handler not found */ public function getHandler(string $identifier): LocalizationHandlerInterface { if (!$this->hasHandler($identifier)) { throw new \InvalidArgumentException( sprintf('Localization handler "%s" not found', $identifier), 1733832000 ); } return $this->handlers[$identifier]; } /** * Check if a handler with the given identifier exists */ public function hasHandler(string $identifier): bool { return isset($this->handlers[$identifier]); } /** * Get available handlers for the given localization context * * Filters handlers based on their availability for the specific context. * * @return array Available handlers indexed by identifier */ public function getAvailableHandlers(LocalizationInstructions $instructions): array { $availableHandlers = []; foreach ($this->handlers as $handler) { $isAvailable = $this->eventDispatcher->dispatch(new ModifyLocalizationHandlerIsAvailableEvent( identifier: $handler->getIdentifier(), className: $handler::class, instructions: $instructions, isAvailable: $handler->isAvailable($instructions), ))->isAvailable; if ($isAvailable === true) { $availableHandlers[$handler->getIdentifier()] = $handler; } } return $availableHandlers; } }