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 05cdf3b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 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 @@ -53,13 +53,12 @@ protected function once($callback, $default = null)
$object = $onceable->object ?? $this;
$stack = static::getRecursiveCallStack($object);

if (isset($stack[$onceable->hash])) {
if (array_key_exists($onceable->hash, $stack)) {
return $stack[$onceable->hash];
}

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 05cdf3b

Please sign in to comment.