-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[11.x] Added PreventsCircularRecursion
This adds a trait for Eloquent which can be used to prevent recursively serializing circular references.
- Loading branch information
Showing
4 changed files
with
277 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/Illuminate/Database/Eloquent/Concerns/PreventsCircularRecursion.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\Concerns; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Onceable; | ||
|
||
trait PreventsCircularRecursion | ||
{ | ||
/** | ||
* The cache of objects processed to prevent infinite recursion. | ||
* | ||
* @var \WeakMap<static, array<string, mixed>> | ||
*/ | ||
protected static $recursionCache; | ||
|
||
/** | ||
* Get the current recursion cache being used by the model. | ||
* | ||
* @return \WeakMap | ||
*/ | ||
protected static function getRecursionCache() | ||
{ | ||
return static::$recursionCache ??= new \WeakMap(); | ||
} | ||
|
||
/** | ||
* Get the current stack of methods being called recursively. | ||
* | ||
* @param object $object | ||
* @return array | ||
*/ | ||
protected static function getRecursiveCallStack($object): array | ||
{ | ||
return static::getRecursionCache()->offsetExists($object) | ||
? static::getRecursionCache()->offsetGet($object) | ||
: []; | ||
} | ||
|
||
/** | ||
* Prevent a method from being called multiple times on the same object within the same call stack. | ||
* | ||
* @param callable $callback | ||
* @param mixed $default | ||
* @return mixed | ||
*/ | ||
protected function once($callback, $default = null) | ||
{ | ||
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2); | ||
|
||
$onceable = Onceable::tryFromTrace($trace, $callback); | ||
|
||
$object = $onceable->object ?? $this; | ||
$stack = static::getRecursiveCallStack($object); | ||
|
||
if (isset($stack[$onceable->hash])) { | ||
return $stack[$onceable->hash]; | ||
} | ||
|
||
try { | ||
// Set the default first to prevent recursion | ||
$stack[$onceable->hash] = $default; | ||
static::getRecursionCache()->offsetSet($object, $stack); | ||
|
||
return call_user_func($onceable->callable); | ||
} finally { | ||
if ($stack = Arr::except($this->getRecursiveCallStack($object), $onceable->hash)) { | ||
static::getRecursionCache()->offsetSet($object, $stack); | ||
} elseif (static::getRecursionCache()->offsetExists($object)) { | ||
static::getRecursionCache()->offsetUnset($object); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
tests/Database/DatabaseConcernsPreventsCircularRecursionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Database; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\PreventsCircularRecursion; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DatabaseConcernsPreventsCircularRecursionTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
PreventsCircularRecursionWithRecursiveMethod::$globalStack = 0; | ||
} | ||
|
||
public function testRecursiveCallsArePreventedWithoutPreventingSubsequentCalls() | ||
{ | ||
$instance = new PreventsCircularRecursionWithRecursiveMethod(); | ||
|
||
$this->assertEquals(0, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(0, $instance->instanceStack); | ||
|
||
$this->assertEquals(0, $instance->callStack()); | ||
$this->assertEquals(1, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(1, $instance->instanceStack); | ||
|
||
$this->assertEquals(1, $instance->callStack()); | ||
$this->assertEquals(2, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(2, $instance->instanceStack); | ||
} | ||
|
||
public function testRecursiveCallsAreLimitedToIndividualInstances() | ||
{ | ||
$instance = new PreventsCircularRecursionWithRecursiveMethod(); | ||
$other = $instance->other; | ||
|
||
$this->assertEquals(0, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(0, $instance->instanceStack); | ||
$this->assertEquals(0, $other->instanceStack); | ||
|
||
$instance->callStack(); | ||
$this->assertEquals(1, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(1, $instance->instanceStack); | ||
$this->assertEquals(0, $other->instanceStack); | ||
|
||
$instance->callStack(); | ||
$this->assertEquals(2, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(2, $instance->instanceStack); | ||
$this->assertEquals(0, $other->instanceStack); | ||
|
||
$other->callStack(); | ||
$this->assertEquals(3, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(2, $instance->instanceStack); | ||
$this->assertEquals(1, $other->instanceStack); | ||
|
||
$other->callStack(); | ||
$this->assertEquals(4, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(2, $instance->instanceStack); | ||
$this->assertEquals(2, $other->instanceStack); | ||
} | ||
|
||
public function testRecursiveCallsToCircularReferenceCallsOtherInstanceOnce() | ||
{ | ||
$instance = new PreventsCircularRecursionWithRecursiveMethod(); | ||
$other = $instance->other; | ||
|
||
$this->assertEquals(0, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(0, $instance->instanceStack); | ||
$this->assertEquals(0, $other->instanceStack); | ||
|
||
$instance->callOtherStack(); | ||
$this->assertEquals(2, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(1, $instance->instanceStack); | ||
$this->assertEquals(1, $other->instanceStack); | ||
|
||
$instance->callOtherStack(); | ||
$this->assertEquals(4, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(2, $instance->instanceStack); | ||
$this->assertEquals(2, $other->instanceStack); | ||
|
||
$other->callOtherStack(); | ||
$this->assertEquals(6, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(3, $other->instanceStack); | ||
$this->assertEquals(3, $instance->instanceStack); | ||
|
||
$other->callOtherStack(); | ||
$this->assertEquals(8, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(4, $other->instanceStack); | ||
$this->assertEquals(4, $instance->instanceStack); | ||
} | ||
|
||
public function testRecursiveCallsToCircularLinkedListCallsEachInstanceOnce() | ||
{ | ||
$instance = new PreventsCircularRecursionWithRecursiveMethod(); | ||
$second = $instance->other; | ||
$third = new PreventsCircularRecursionWithRecursiveMethod($second); | ||
$instance->other = $third; | ||
|
||
$this->assertEquals(0, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(0, $instance->instanceStack); | ||
$this->assertEquals(0, $second->instanceStack); | ||
$this->assertEquals(0, $third->instanceStack); | ||
|
||
$instance->callOtherStack(); | ||
$this->assertEquals(3, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(1, $instance->instanceStack); | ||
$this->assertEquals(1, $second->instanceStack); | ||
$this->assertEquals(1, $third->instanceStack); | ||
|
||
$second->callOtherStack(); | ||
$this->assertEquals(6, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(2, $instance->instanceStack); | ||
$this->assertEquals(2, $second->instanceStack); | ||
$this->assertEquals(2, $third->instanceStack); | ||
|
||
$third->callOtherStack(); | ||
$this->assertEquals(9, PreventsCircularRecursionWithRecursiveMethod::$globalStack); | ||
$this->assertEquals(3, $instance->instanceStack); | ||
$this->assertEquals(3, $second->instanceStack); | ||
$this->assertEquals(3, $third->instanceStack); | ||
} | ||
} | ||
|
||
class PreventsCircularRecursionWithRecursiveMethod | ||
{ | ||
use PreventsCircularRecursion; | ||
|
||
public function __construct( | ||
public ?PreventsCircularRecursionWithRecursiveMethod $other = null, | ||
) { | ||
$this->other ??= new PreventsCircularRecursionWithRecursiveMethod($this); | ||
} | ||
|
||
public static int $globalStack = 0; | ||
public int $instanceStack = 0; | ||
|
||
public function callStack(): int | ||
{ | ||
return $this->once( | ||
function () { | ||
static::$globalStack++; | ||
$this->instanceStack++; | ||
return $this->callStack(); | ||
}, | ||
$this->instanceStack | ||
); | ||
} | ||
|
||
public function callOtherStack(): int | ||
{ | ||
return $this->once( | ||
function () { | ||
$this->other->callStack(); | ||
return $this->other->callOtherStack(); | ||
}, | ||
$this->instanceStack | ||
); | ||
} | ||
} |