diff --git a/src/Concerns/Identification.php b/src/Concerns/Identification.php index 1637aa2..5923e1f 100644 --- a/src/Concerns/Identification.php +++ b/src/Concerns/Identification.php @@ -6,29 +6,20 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; +use Illuminate\Support\Facades\App; use Illuminate\Support\Str; use TiMacDonald\JsonApi\Exceptions\ResourceIdentificationException; use TiMacDonald\JsonApi\ResourceIdentifier; trait Identification { - private string|null $idCache = null; + private const ID_RESOLVER_KEY = self::class.':$idResolver'; - private string|null $typeCache = null; + private const TYPE_RESOLVER_KEY = self::class.':$typeResolver'; - /** - * @internal - * - * @var (callable(mixed, Request): string)|null - */ - private static $idResolver; + private string|null $idCache = null; - /** - * @internal - * - * @var (callable(mixed, Request): string)|null - */ - private static $typeResolver; + private string|null $typeCache = null; /** * @internal @@ -58,7 +49,7 @@ public function withResourceIdentifier(callable $callback) */ public static function resolveIdUsing(callable $callback) { - self::$idResolver = $callback; + App::instance(self::ID_RESOLVER_KEY, $callback); } /** @@ -69,27 +60,7 @@ public static function resolveIdUsing(callable $callback) */ public static function resolveTypeUsing(callable $callback) { - self::$typeResolver = $callback; - } - - /** - * @internal - * - * @return void - */ - public static function resolveIdNormally() - { - self::$idResolver = null; - } - - /** - * @internal - * - * @return void - */ - public static function resolveTypeNormally() - { - self::$typeResolver = null; + App::instance(self::TYPE_RESOLVER_KEY, $callback); } /** @@ -129,16 +100,20 @@ private function resolveType(Request $request) */ private static function idResolver() { - return self::$idResolver ??= function (mixed $resource, Request $request): string { - if (! $resource instanceof Model) { - throw ResourceIdentificationException::attemptingToDetermineIdFor($resource); - } - - /** - * @phpstan-ignore-next-line - */ - return (string) $resource->getKey(); - }; + if (! App::bound(self::ID_RESOLVER_KEY)) { + App::instance(self::ID_RESOLVER_KEY, function (mixed $resource, Request $request): string { + if (! $resource instanceof Model) { + throw ResourceIdentificationException::attemptingToDetermineIdFor($resource); + } + + /** + * @phpstan-ignore-next-line + */ + return (string) $resource->getKey(); + }); + } + + return App::make(self::ID_RESOLVER_KEY); } /** @@ -148,13 +123,17 @@ private static function idResolver() */ private static function typeResolver() { - return self::$typeResolver ??= function (mixed $resource, Request $request): string { - if (! $resource instanceof Model) { - throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource); - } + if (! App::bound(self::TYPE_RESOLVER_KEY)) { + App::instance(self::TYPE_RESOLVER_KEY, function (mixed $resource, Request $request): string { + if (! $resource instanceof Model) { + throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource); + } + + return Str::camel($resource->getTable()); + }); + } - return Str::camel($resource->getTable()); - }; + return App::make(self::TYPE_RESOLVER_KEY); } /** diff --git a/tests/Feature/JsonApiTest.php b/tests/Feature/JsonApiTest.php index bc1d61b..af57523 100644 --- a/tests/Feature/JsonApiTest.php +++ b/tests/Feature/JsonApiTest.php @@ -208,8 +208,6 @@ public function testItCanCustomiseTheTypeResolution(): void ], ]); $this->assertValidJsonApi($response); - - JsonApiResource::resolveTypeNormally(); } public function testItCanCustomiseTheIdResolution(): void @@ -229,8 +227,6 @@ public function testItCanCustomiseTheIdResolution(): void ], ]); $this->assertValidJsonApi($response); - - JsonApiResource::resolveIdNormally(); } public function testItExcludesEmptyResourceIdentifierMeta(): void diff --git a/tests/Unit/LinkTest.php b/tests/Unit/LinkTest.php index 465acf4..111278b 100644 --- a/tests/Unit/LinkTest.php +++ b/tests/Unit/LinkTest.php @@ -71,8 +71,5 @@ public function testItCanUseHash() $links = json_encode($resource->toArray($request)['links']); $this->assertSame('{"foo":{"href":"http:\/\/foo.com"}}', $links); - - JsonApiResource::resolveIdNormally(); - JsonApiResource::resolveTypeNormally(); } }