| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | /** |
| 6: | * This file is part of the Nexus MCP SDK package. |
| 7: | * |
| 8: | * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com> |
| 9: | * |
| 10: | * For the full copyright and license information, please view |
| 11: | * the LICENSE file that was distributed with this source code. |
| 12: | */ |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Handler; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | |
| 18: | /** |
| 19: | * Method-name to handler dispatch table, generic over the handler interface. |
| 20: | * |
| 21: | * @template-covariant THandler of object |
| 22: | */ |
| 23: | final readonly class HandlerRegistry |
| 24: | { |
| 25: | /** |
| 26: | * @param array<non-empty-string, THandler> $handlers |
| 27: | * @param class-string $handlerInterface |
| 28: | * @param non-empty-string $label |
| 29: | */ |
| 30: | public function __construct( |
| 31: | private array $handlers, |
| 32: | string $handlerInterface, |
| 33: | string $label, |
| 34: | ) { |
| 35: | Assert::that($handlers) |
| 36: | ->keys() |
| 37: | ->isNonEmptyString(\sprintf('%s registry key must be a non-empty string.', $label)) |
| 38: | ; |
| 39: | Assert::that($handlers) |
| 40: | ->values() |
| 41: | ->isInstanceOf( |
| 42: | $handlerInterface, |
| 43: | \sprintf('%s registry value must implement {class}.', $label), |
| 44: | ) |
| 45: | ; |
| 46: | } |
| 47: | |
| 48: | /** |
| 49: | * @param non-empty-string $method |
| 50: | * |
| 51: | * @return null|THandler |
| 52: | */ |
| 53: | public function get(string $method): ?object |
| 54: | { |
| 55: | return $this->handlers[$method] ?? null; |
| 56: | } |
| 57: | } |
| 58: |