66
77use Illuminate \Database \Eloquent \Model ;
88use Illuminate \Http \Request ;
9+ use Illuminate \Support \Facades \App ;
910use Illuminate \Support \Str ;
1011use TiMacDonald \JsonApi \Exceptions \ResourceIdentificationException ;
1112use TiMacDonald \JsonApi \ResourceIdentifier ;
1213
1314trait 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 /**
0 commit comments