Skip to content

Commit 05cdf3b

Browse files
committed
[11.x] Changed the name to withoutRecursion(), accept a callable default
1 parent 1e175f4 commit 05cdf3b

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

src/Illuminate/Database/Eloquent/Concerns/PreventsCircularRecursion.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected static function getRecursiveCallStack($object): array
4444
* @param mixed $default
4545
* @return mixed
4646
*/
47-
protected function once($callback, $default = null)
47+
protected function withoutRecursion($callback, $default = null)
4848
{
4949
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
5050

@@ -53,13 +53,12 @@ protected function once($callback, $default = null)
5353
$object = $onceable->object ?? $this;
5454
$stack = static::getRecursiveCallStack($object);
5555

56-
if (isset($stack[$onceable->hash])) {
56+
if (array_key_exists($onceable->hash, $stack)) {
5757
return $stack[$onceable->hash];
5858
}
5959

6060
try {
61-
// Set the default first to prevent recursion
62-
$stack[$onceable->hash] = $default;
61+
$stack[$onceable->hash] = is_callable($default) ? call_user_func($default) : $default;
6362
static::getRecursionCache()->offsetSet($object, $stack);
6463

6564
return call_user_func($onceable->callable);

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ protected function decrementQuietly($column, $amount = 1, array $extra = [])
10841084
*/
10851085
public function push()
10861086
{
1087-
return $this->once(function () {
1087+
return $this->withoutRecursion(function () {
10881088
if (! $this->save()) {
10891089
return false;
10901090
}
@@ -1647,9 +1647,9 @@ public function callNamedScope($scope, array $parameters = [])
16471647
*/
16481648
public function toArray()
16491649
{
1650-
return $this->once(
1650+
return $this->withoutRecursion(
16511651
fn () => array_merge($this->attributesToArray(), $this->relationsToArray()),
1652-
$this->attributesToArray(),
1652+
fn () => $this->attributesToArray(),
16531653
);
16541654
}
16551655

@@ -1997,7 +1997,7 @@ public function getQueueableId()
19971997
*/
19981998
public function getQueueableRelations()
19991999
{
2000-
return $this->once(function () {
2000+
return $this->withoutRecursion(function () {
20012001
$relations = [];
20022002

20032003
foreach ($this->getRelations() as $key => $relation) {

tests/Database/DatabaseConcernsPreventsCircularRecursionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function __construct(
137137

138138
public function callStack(): int
139139
{
140-
return $this->once(
140+
return $this->withoutRecursion(
141141
function () {
142142
static::$globalStack++;
143143
$this->instanceStack++;
@@ -150,7 +150,7 @@ function () {
150150

151151
public function callOtherStack(): int
152152
{
153-
return $this->once(
153+
return $this->withoutRecursion(
154154
function () {
155155
$this->other->callStack();
156156

0 commit comments

Comments
 (0)