Skip to content

Commit

Permalink
[11.x] Changed the name to withoutRecursion(), accept a callable de…
Browse files Browse the repository at this point in the history
…fault
  • Loading branch information
samlev committed Aug 13, 2024
1 parent 1e175f4 commit 1cb4499
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected static function getRecursiveCallStack($object): array
* @param mixed $default
* @return mixed
*/
protected function once($callback, $default = null)
protected function withoutRecursion($callback, $default = null)
{
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);

Expand All @@ -58,8 +58,7 @@ protected function once($callback, $default = null)
}

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

return call_user_func($onceable->callable);
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ protected function decrementQuietly($column, $amount = 1, array $extra = [])
*/
public function push()
{
return $this->once(function () {
return $this->withoutRecursion(function () {
if (! $this->save()) {
return false;
}
Expand Down Expand Up @@ -1647,9 +1647,9 @@ public function callNamedScope($scope, array $parameters = [])
*/
public function toArray()
{
return $this->once(
return $this->withoutRecursion(
fn () => array_merge($this->attributesToArray(), $this->relationsToArray()),
$this->attributesToArray(),
fn () => $this->attributesToArray(),
);
}

Expand Down Expand Up @@ -1997,7 +1997,7 @@ public function getQueueableId()
*/
public function getQueueableRelations()
{
return $this->once(function () {
return $this->withoutRecursion(function () {
$relations = [];

foreach ($this->getRelations() as $key => $relation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function __construct(

public function callStack(): int
{
return $this->once(
return $this->withoutRecursion(
function () {
static::$globalStack++;
$this->instanceStack++;
Expand All @@ -150,7 +150,7 @@ function () {

public function callOtherStack(): int
{
return $this->once(
return $this->withoutRecursion(
function () {
$this->other->callStack();

Expand Down

0 comments on commit 1cb4499

Please sign in to comment.