4
4
5
5
use Illuminate \Support \Arr ;
6
6
use Illuminate \Support \Onceable ;
7
+ use WeakMap ;
7
8
8
9
trait PreventsCircularRecursion
9
10
{
@@ -14,29 +15,6 @@ trait PreventsCircularRecursion
14
15
*/
15
16
protected static $ recursionCache ;
16
17
17
- /**
18
- * Get the current recursion cache being used by the model.
19
- *
20
- * @return \WeakMap
21
- */
22
- protected static function getRecursionCache ()
23
- {
24
- return static ::$ recursionCache ??= new \WeakMap ();
25
- }
26
-
27
- /**
28
- * Get the current stack of methods being called recursively.
29
- *
30
- * @param object $object
31
- * @return array
32
- */
33
- protected static function getRecursiveCallStack ($ object ): array
34
- {
35
- return static ::getRecursionCache ()->offsetExists ($ object )
36
- ? static ::getRecursionCache ()->offsetGet ($ object )
37
- : [];
38
- }
39
-
40
18
/**
41
19
* Prevent a method from being called multiple times on the same object within the same call stack.
42
20
*
@@ -51,6 +29,7 @@ protected function withoutRecursion($callback, $default = null)
51
29
$ onceable = Onceable::tryFromTrace ($ trace , $ callback );
52
30
53
31
$ object = $ onceable ->object ?? $ this ;
32
+
54
33
$ stack = static ::getRecursiveCallStack ($ object );
55
34
56
35
if (array_key_exists ($ onceable ->hash , $ stack )) {
@@ -59,6 +38,7 @@ protected function withoutRecursion($callback, $default = null)
59
38
60
39
try {
61
40
$ stack [$ onceable ->hash ] = is_callable ($ default ) ? call_user_func ($ default ) : $ default ;
41
+
62
42
static ::getRecursionCache ()->offsetSet ($ object , $ stack );
63
43
64
44
return call_user_func ($ onceable ->callable );
@@ -70,4 +50,27 @@ protected function withoutRecursion($callback, $default = null)
70
50
}
71
51
}
72
52
}
53
+
54
+ /**
55
+ * Get the current stack of methods being called recursively.
56
+ *
57
+ * @param object $object
58
+ * @return array
59
+ */
60
+ protected static function getRecursiveCallStack ($ object ): array
61
+ {
62
+ return static ::getRecursionCache ()->offsetExists ($ object )
63
+ ? static ::getRecursionCache ()->offsetGet ($ object )
64
+ : [];
65
+ }
66
+
67
+ /**
68
+ * Get the current recursion cache being used by the model.
69
+ *
70
+ * @return \WeakMap
71
+ */
72
+ protected static function getRecursionCache ()
73
+ {
74
+ return static ::$ recursionCache ??= new WeakMap ();
75
+ }
73
76
}
0 commit comments