Skip to content

Commit 0e9f0cc

Browse files
committed
Bind and resolver type / id resolver from container
1 parent f112ff6 commit 0e9f0cc

File tree

3 files changed

+31
-59
lines changed

3 files changed

+31
-59
lines changed

src/Concerns/Identification.php

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,20 @@
66

77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Http\Request;
9+
use Illuminate\Support\Facades\App;
910
use Illuminate\Support\Str;
1011
use TiMacDonald\JsonApi\Exceptions\ResourceIdentificationException;
1112
use TiMacDonald\JsonApi\ResourceIdentifier;
1213

1314
trait Identification
1415
{
15-
private string|null $idCache = null;
16+
private const ID_RESOLVER_KEY = self::class.':$idResolver';
1617

17-
private string|null $typeCache = null;
18+
private const TYPE_RESOLVER_KEY = self::class.':$typeResolver';
1819

19-
/**
20-
* @internal
21-
*
22-
* @var (callable(mixed, Request): string)|null
23-
*/
24-
private static $idResolver;
20+
private string|null $idCache = null;
2521

26-
/**
27-
* @internal
28-
*
29-
* @var (callable(mixed, Request): string)|null
30-
*/
31-
private static $typeResolver;
22+
private string|null $typeCache = null;
3223

3324
/**
3425
* @internal
@@ -58,7 +49,7 @@ public function withResourceIdentifier(callable $callback)
5849
*/
5950
public static function resolveIdUsing(callable $callback)
6051
{
61-
self::$idResolver = $callback;
52+
App::instance(self::ID_RESOLVER_KEY, $callback);
6253
}
6354

6455
/**
@@ -69,27 +60,7 @@ public static function resolveIdUsing(callable $callback)
6960
*/
7061
public static function resolveTypeUsing(callable $callback)
7162
{
72-
self::$typeResolver = $callback;
73-
}
74-
75-
/**
76-
* @internal
77-
*
78-
* @return void
79-
*/
80-
public static function resolveIdNormally()
81-
{
82-
self::$idResolver = null;
83-
}
84-
85-
/**
86-
* @internal
87-
*
88-
* @return void
89-
*/
90-
public static function resolveTypeNormally()
91-
{
92-
self::$typeResolver = null;
63+
App::instance(self::TYPE_RESOLVER_KEY, $callback);
9364
}
9465

9566
/**
@@ -129,16 +100,20 @@ private function resolveType(Request $request)
129100
*/
130101
private static function idResolver()
131102
{
132-
return self::$idResolver ??= function (mixed $resource, Request $request): string {
133-
if (! $resource instanceof Model) {
134-
throw ResourceIdentificationException::attemptingToDetermineIdFor($resource);
135-
}
136-
137-
/**
138-
* @phpstan-ignore-next-line
139-
*/
140-
return (string) $resource->getKey();
141-
};
103+
if (! App::bound(self::ID_RESOLVER_KEY)) {
104+
App::instance(self::ID_RESOLVER_KEY, function (mixed $resource, Request $request): string {
105+
if (! $resource instanceof Model) {
106+
throw ResourceIdentificationException::attemptingToDetermineIdFor($resource);
107+
}
108+
109+
/**
110+
* @phpstan-ignore-next-line
111+
*/
112+
return (string) $resource->getKey();
113+
});
114+
}
115+
116+
return App::make(self::ID_RESOLVER_KEY);
142117
}
143118

144119
/**
@@ -148,13 +123,17 @@ private static function idResolver()
148123
*/
149124
private static function typeResolver()
150125
{
151-
return self::$typeResolver ??= function (mixed $resource, Request $request): string {
152-
if (! $resource instanceof Model) {
153-
throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource);
154-
}
126+
if (! App::bound(self::TYPE_RESOLVER_KEY)) {
127+
App::instance(self::TYPE_RESOLVER_KEY, function (mixed $resource, Request $request): string {
128+
if (! $resource instanceof Model) {
129+
throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource);
130+
}
131+
132+
return Str::camel($resource->getTable());
133+
});
134+
}
155135

156-
return Str::camel($resource->getTable());
157-
};
136+
return App::make(self::TYPE_RESOLVER_KEY);
158137
}
159138

160139
/**

tests/Feature/JsonApiTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ public function testItCanCustomiseTheTypeResolution(): void
208208
],
209209
]);
210210
$this->assertValidJsonApi($response);
211-
212-
JsonApiResource::resolveTypeNormally();
213211
}
214212

215213
public function testItCanCustomiseTheIdResolution(): void
@@ -229,8 +227,6 @@ public function testItCanCustomiseTheIdResolution(): void
229227
],
230228
]);
231229
$this->assertValidJsonApi($response);
232-
233-
JsonApiResource::resolveIdNormally();
234230
}
235231

236232
public function testItExcludesEmptyResourceIdentifierMeta(): void

tests/Unit/LinkTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,5 @@ public function testItCanUseHash()
7171
$links = json_encode($resource->toArray($request)['links']);
7272

7373
$this->assertSame('{"foo":{"href":"http:\/\/foo.com"}}', $links);
74-
75-
JsonApiResource::resolveIdNormally();
76-
JsonApiResource::resolveTypeNormally();
7774
}
7875
}

0 commit comments

Comments
 (0)