Skip to content

Commit 93dfac0

Browse files
committed
wip
1 parent 47f11d1 commit 93dfac0

File tree

8 files changed

+38
-89
lines changed

8 files changed

+38
-89
lines changed

src/Concerns/Caching.php

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,12 @@
88
use TiMacDonald\JsonApi\JsonApiResource;
99
use TiMacDonald\JsonApi\JsonApiResourceCollection;
1010

11+
/**
12+
* @internal
13+
* @todo can we get rid of this?
14+
*/
1115
trait Caching
1216
{
13-
/**
14-
* @internal
15-
*/
16-
private string|null $idCache = null;
17-
18-
/**
19-
* @internal
20-
*/
21-
private string|null $typeCache = null;
22-
23-
/**
24-
* @internal
25-
*
26-
* @var Collection<string, JsonApiResource|JsonApiResourceCollection>|null
27-
*/
28-
private Collection|null $requestedRelationshipsCache = null;
29-
3017
/**
3118
* @internal
3219
* @infection-ignore-all
@@ -35,51 +22,14 @@ trait Caching
3522
*/
3623
public function flush()
3724
{
38-
$this->idCache = null;
39-
40-
$this->typeCache = null;
41-
42-
if ($this->requestedRelationshipsCache !== null) {
43-
$this->requestedRelationshipsCache->each(fn (JsonApiResource|JsonApiResourceCollection $relation) => $relation->flush());
44-
}
25+
// TODO can we just let this garbage collect?
26+
$this->requestedRelationshipsCache?->each(fn (JsonApiResource|JsonApiResourceCollection $relation) => $relation->flush());
4527

4628
$this->requestedRelationshipsCache = null;
47-
}
4829

49-
/**
50-
* @internal
51-
* @infection-ignore-all
52-
*
53-
* @param (callable(): string) $callback
54-
* @return string
55-
*/
56-
private function rememberId(callable $callback)
57-
{
58-
return $this->idCache ??= $callback();
59-
}
60-
61-
/**
62-
* @internal
63-
* @infection-ignore-all
64-
*
65-
* @param (callable(): string) $callback
66-
* @return string
67-
*/
68-
private function rememberType(callable $callback)
69-
{
70-
return $this->typeCache ??= $callback();
71-
}
30+
$this->idCache = null;
7231

73-
/**
74-
* @internal
75-
* @infection-ignore-all
76-
*
77-
* @param (callable(): Collection<string, JsonApiResource|JsonApiResourceCollection>) $callback
78-
* @return Collection<string, JsonApiResource|JsonApiResourceCollection>
79-
*/
80-
private function rememberRequestRelationships(callable $callback)
81-
{
82-
return $this->requestedRelationshipsCache ??= $callback();
32+
$this->typeCache = null;
8333
}
8434

8535
/**
@@ -89,6 +39,7 @@ private function rememberRequestRelationships(callable $callback)
8939
*/
9040
public function requestedRelationshipsCache()
9141
{
42+
// TODO can we remove this if we ditch caching? Only here for tests.
9243
return $this->requestedRelationshipsCache;
9344
}
9445
}

src/Concerns/Identification.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
trait Identification
1414
{
15+
private string|null $idCache = null;
16+
17+
private string|null $typeCache = null;
18+
1519
/**
1620
* @internal
1721
*
@@ -105,7 +109,7 @@ public function toUniqueResourceIdentifier(Request $request)
105109
*/
106110
private function resolveId(Request $request)
107111
{
108-
return $this->rememberId(fn (): string => $this->toId($request));
112+
return $this->idCache ??= $this->toId($request);
109113
}
110114

111115
/**
@@ -115,7 +119,7 @@ private function resolveId(Request $request)
115119
*/
116120
private function resolveType(Request $request)
117121
{
118-
return $this->rememberType(fn (): string => $this->toType($request));
122+
return $this->typeCache ??= $this->toType($request);
119123
}
120124

121125
/**

src/Concerns/Relationships.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
trait Relationships
2525
{
26+
/**
27+
* @var Collection<string, JsonApiResource|JsonApiResourceCollection>|null
28+
*/
29+
private Collection|null $requestedRelationshipsCache = null;
30+
2631
/**
2732
* @internal
2833
*
@@ -103,10 +108,10 @@ private function requestedRelationshipsAsIdentifiers(Request $request)
103108
*/
104109
private function requestedRelationships(Request $request)
105110
{
106-
return $this->rememberRequestRelationships(fn (): Collection => $this->resolveRelationships($request)
111+
return $this->requestedRelationshipsCache ??= $this->resolveRelationships($request)
107112
->only($this->requestedIncludes($request))
108113
->map(fn (callable $value, string $prefix): null|JsonApiResource|JsonApiResourceCollection => $this->resolveInclude($value(), $prefix))
109-
->reject(fn (JsonApiResource|JsonApiResourceCollection|null $resource): bool => $resource === null));
114+
->reject(fn (JsonApiResource|JsonApiResourceCollection|null $resource): bool => $resource === null);
110115
}
111116

112117
/**

src/JsonApiResource.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Http\Resources\PotentiallyMissing;
1111
use Illuminate\Support\Collection;
1212
use stdClass;
13+
1314
use function property_exists;
1415

1516
abstract class JsonApiResource extends JsonResource
@@ -115,7 +116,7 @@ public function toArray(Request $request)
115116
}
116117

117118
/**
118-
* @return array{included?: Collection<int, JsonApiResource>, jsonapi: JsonApiServerImplementation}
119+
* @return array{included?: array<int, JsonApiResource>, jsonapi: JsonApiServerImplementation}
119120
*/
120121
public function with(Request $request)
121122
{
@@ -129,6 +130,8 @@ public function with(Request $request)
129130
}
130131

131132
/**
133+
* TODO this may be removed once all supported versions have `newCollection`.
134+
*
132135
* @return JsonApiResourceCollection<int, mixed>
133136
*/
134137
public static function collection(mixed $resource)
@@ -155,7 +158,7 @@ public static function newCollection(mixed $resource)
155158
*/
156159
public function toResponse($request)
157160
{
158-
// TODO: the flush call here is triggering repeated Includes::flush() cals, because of collection.s
159-
return tap(parent::toResponse($request)->header('Content-type', 'application/vnd.api+json'), fn () => $this->flush());
161+
// TODO: should this header be configurable? Should it be a middleware? Should we not set it if one exists?
162+
return tap(parent::toResponse($request)->header('Content-type', 'application/vnd.api+json'), $this->flush(...));
160163
}
161164
}

src/JsonApiResourceCollection.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private function resolveResourceIdentifiers(Request $request)
4242
}
4343

4444
/**
45-
* @return array{included?: Collection<int, JsonApiResource>, jsonapi: JsonApiServerImplementation}
45+
* @return array{included?: array<int, JsonApiResource>, jsonapi: JsonApiServerImplementation}
4646
*/
4747
public function with(Request $request)
4848
{
@@ -63,7 +63,8 @@ public function with(Request $request)
6363
*/
6464
public function toResponse($request)
6565
{
66-
return tap(parent::toResponse($request)->header('Content-type', 'application/vnd.api+json'), fn () => $this->flush());
66+
// TODO: should this header be configurable? Should it be a middleware? Should we not set it if one exists?
67+
return tap(parent::toResponse($request)->header('Content-type', 'application/vnd.api+json'), $this->flush(...));
6768
}
6869

6970
/**
@@ -98,9 +99,9 @@ function (array $link): array {
9899
*/
99100
public function withIncludePrefix(string $prefix)
100101
{
101-
return tap($this, function (JsonApiResourceCollection $resource) use ($prefix): void {
102-
$resource->collection->each(fn (JsonApiResource $resource): JsonApiResource => $resource->withIncludePrefix($prefix));
103-
});
102+
$this->collection->each(fn (JsonApiResource $resource): JsonApiResource => $resource->withIncludePrefix($prefix));
103+
104+
return $this;
104105
}
105106

106107
/**

src/Support/Fields.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class Fields
2525
*/
2626
private WeakMap $cache;
2727

28-
private function __construct(WeakMap $cache = new WeakMap)
28+
private function __construct(WeakMap $cache = new WeakMap())
2929
{
3030
$this->cache = $cache;
3131
}
@@ -78,12 +78,4 @@ private function rememberResourceType(Request $request, string $resourceType, ca
7878

7979
return $this->cache[$request][$resourceType] ??= $callback();
8080
}
81-
82-
/**
83-
* @return void
84-
*/
85-
public function flush()
86-
{
87-
$this->cache = new WeakMap();
88-
}
8981
}

src/Support/Includes.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Str;
1010
use Symfony\Component\HttpKernel\Exception\HttpException;
1111
use WeakMap;
12+
1213
use function explode;
1314
use function is_array;
1415

@@ -24,7 +25,7 @@ final class Includes
2425
*/
2526
private WeakMap $cache;
2627

27-
private function __construct(WeakMap $cache = new WeakMap)
28+
private function __construct(WeakMap $cache = new WeakMap())
2829
{
2930
$this->cache = $cache;
3031
}
@@ -80,12 +81,4 @@ private function rememberIncludes(Request $request, string $prefix, callable $ca
8081

8182
return $this->cache[$request][$prefix] ??= $callback();
8283
}
83-
84-
/**
85-
* @return void
86-
*/
87-
public function flush()
88-
{
89-
$this->cache = new WeakMap();
90-
}
9184
}

tests/Feature/JsonApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use Tests\TestCase;
1212
use TiMacDonald\JsonApi\JsonApiResource;
1313
use TiMacDonald\JsonApi\JsonApiResourceCollection;
14-
use TiMacDonald\JsonApi\ServerImplementation;
1514
use TiMacDonald\JsonApi\Link;
1615
use TiMacDonald\JsonApi\RelationshipObject;
1716
use TiMacDonald\JsonApi\ResourceIdentifier;
17+
use TiMacDonald\JsonApi\ServerImplementation;
1818

1919
class JsonApiTest extends TestCase
2020
{

0 commit comments

Comments
 (0)