Skip to content

Commit 10bcf57

Browse files
committed
Cache endpoints
1 parent 99a9297 commit 10bcf57

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/Context.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ class Context
2929
private ?array $body;
3030
private ?string $path;
3131

32+
private WeakMap $endpoints;
3233
private WeakMap $fields;
3334
private WeakMap $sparseFields;
3435

3536
public function __construct(public JsonApi $api, public ServerRequestInterface $request)
3637
{
38+
$this->endpoints = new WeakMap();
3739
$this->fields = new WeakMap();
3840
$this->sparseFields = new WeakMap();
3941

@@ -88,6 +90,11 @@ public function resource(string $type): Resource
8890
return $this->api->getResource($type);
8991
}
9092

93+
public function endpoints(Collection $collection): array
94+
{
95+
return $this->endpoints[$collection] ??= $collection->endpoints();
96+
}
97+
9198
/**
9299
* Get the fields for the given resource, keyed by name.
93100
*

src/Serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function addToMap(Context $context): array
5757
foreach ($context->api->getResourceCollections($resource->type()) as $collection) {
5858
$collectionContext = $context->withCollection($collection);
5959

60-
foreach ($collection->endpoints() as $endpoint) {
60+
foreach ($context->endpoints($collection) as $endpoint) {
6161
if ($endpoint instanceof ResourceEndpoint) {
6262
if ($links = $endpoint->resourceLinks($model, $collectionContext)) {
6363
$this->map[$key]['links'] ??= [];

0 commit comments

Comments
 (0)