77 lines
2.1 KiB
PHP
77 lines
2.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the official PHP MCP SDK.
|
|
*
|
|
* A collaboration between Symfony and the PHP Foundation.
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Mcp\Capability;
|
|
|
|
use Mcp\Capability\Resource\CollectionInterface;
|
|
use Mcp\Capability\Resource\IdentifierInterface;
|
|
use Mcp\Capability\Resource\MetadataInterface;
|
|
use Mcp\Capability\Resource\ResourceRead;
|
|
use Mcp\Capability\Resource\ResourceReaderInterface;
|
|
use Mcp\Capability\Resource\ResourceReadResult;
|
|
use Mcp\Exception\InvalidCursorException;
|
|
use Mcp\Exception\ResourceNotFoundException;
|
|
use Mcp\Exception\ResourceReadException;
|
|
|
|
/**
|
|
* A collection of resources. All resources need to implement IdentifierInterface.
|
|
*
|
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
|
*/
|
|
class ResourceChain implements CollectionInterface, ResourceReaderInterface
|
|
{
|
|
public function __construct(
|
|
/**
|
|
* @var IdentifierInterface[]
|
|
*/
|
|
private readonly array $items,
|
|
) {
|
|
}
|
|
|
|
public function getMetadata(int $count, ?string $lastIdentifier = null): iterable
|
|
{
|
|
$found = null === $lastIdentifier;
|
|
foreach ($this->items as $item) {
|
|
if (!$item instanceof MetadataInterface) {
|
|
continue;
|
|
}
|
|
|
|
if (false === $found) {
|
|
$found = $item->getUri() === $lastIdentifier;
|
|
continue;
|
|
}
|
|
|
|
yield $item;
|
|
if (--$count <= 0) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$found) {
|
|
throw new InvalidCursorException($lastIdentifier);
|
|
}
|
|
}
|
|
|
|
public function read(ResourceRead $input): ResourceReadResult
|
|
{
|
|
foreach ($this->items as $item) {
|
|
if ($item instanceof ResourceReaderInterface && $input->uri === $item->getUri()) {
|
|
try {
|
|
return $item->read($input);
|
|
} catch (\Throwable $e) {
|
|
throw new ResourceReadException($input, $e);
|
|
}
|
|
}
|
|
}
|
|
|
|
throw new ResourceNotFoundException($input);
|
|
}
|
|
}
|