Roll out meta properties in attributes and more schema classes (#129)

This commit is contained in:
Kaipi Yann 2025-11-08 13:58:26 +01:00 • committed by GitHub
commit d3a07912b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -68,9 +68,11 @@ class ResourceReference extends ElementReference
return [$readResult->resource];
}
$meta = $this->schema->meta;
if (\is_array($readResult)) {
if (empty($readResult)) {
return [new TextResourceContents($uri, 'application/json', '[]')];
return [new TextResourceContents($uri, 'application/json', '[]', $meta)];
}
$allAreResourceContents = true;
@ -118,14 +120,15 @@ class ResourceReference extends ElementReference
if (\is_string($readResult)) {
$mimeType = $mimeType ?? $this->guessMimeTypeFromString($readResult);
return [new TextResourceContents($uri, $mimeType, $readResult)];
return [new TextResourceContents($uri, $mimeType, $readResult, $meta)];
}
if (\is_resource($readResult) && 'stream' === get_resource_type($readResult)) {
$result = BlobResourceContents::fromStream(
$uri,
$readResult,
$mimeType ?? 'application/octet-stream'
$mimeType ?? 'application/octet-stream',
$meta
);
@fclose($readResult);
@ -136,21 +139,21 @@ class ResourceReference extends ElementReference
if (\is_array($readResult) && isset($readResult['blob']) && \is_string($readResult['blob'])) {
$mimeType = $readResult['mimeType'] ?? $mimeType ?? 'application/octet-stream';
return [new BlobResourceContents($uri, $mimeType, $readResult['blob'])];
return [new BlobResourceContents($uri, $mimeType, $readResult['blob'], $meta)];
}
if (\is_array($readResult) && isset($readResult['text']) && \is_string($readResult['text'])) {
$mimeType = $readResult['mimeType'] ?? $mimeType ?? 'text/plain';
return [new TextResourceContents($uri, $mimeType, $readResult['text'])];
return [new TextResourceContents($uri, $mimeType, $readResult['text'], $meta)];
}
if ($readResult instanceof \SplFileInfo && $readResult->isFile() && $readResult->isReadable()) {
if ($mimeType && str_contains(strtolower($mimeType), 'text')) {
return [new TextResourceContents($uri, $mimeType, file_get_contents($readResult->getPathname()))];
return [new TextResourceContents($uri, $mimeType, file_get_contents($readResult->getPathname()), $meta)];
}
return [BlobResourceContents::fromSplFileInfo($uri, $readResult, $mimeType)];
return [BlobResourceContents::fromSplFileInfo($uri, $readResult, $mimeType, $meta)];
}
if (\is_array($readResult)) {
@ -159,7 +162,7 @@ class ResourceReference extends ElementReference
try {
$jsonString = json_encode($readResult, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
return [new TextResourceContents($uri, $mimeType, $jsonString)];
return [new TextResourceContents($uri, $mimeType, $jsonString, $meta)];
} catch (\JsonException $e) {
throw new RuntimeException(\sprintf('Failed to encode array as JSON for URI "%s": %s', $uri, $e->getMessage()));
}
@ -169,7 +172,7 @@ class ResourceReference extends ElementReference
$jsonString = json_encode($readResult, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
$mimeType = $mimeType ?? 'application/json';
return [new TextResourceContents($uri, $mimeType, $jsonString)];
return [new TextResourceContents($uri, $mimeType, $jsonString, $meta)];
} catch (\JsonException $e) {
throw new RuntimeException(\sprintf('Failed to encode array as JSON for URI "%s": %s', $uri, $e->getMessage()));
}