Skip to content

Commit

Permalink
fix: bindable cache memory efficiency when using nested parent objects
Browse files Browse the repository at this point in the history
fixes #495
  • Loading branch information
g105b committed Aug 12, 2024
1 parent c3cc26e commit 4bc901d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/BindableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct() {
$this->nonBindableClassMap = [];
}

public function isBindable(object $object):bool {
public function isBindable(object $object, bool $recursive = false):bool {
$refObj = null;

if($object instanceof ReflectionClass) {
Expand Down Expand Up @@ -115,6 +115,7 @@ public function isBindable(object $object):bool {
return false;
}

$this->bindableClassMap[$classString] = $attributeCache;
$attributeCache = $this->expandObjects(
$attributeCache,
$cacheObjectKeys,
Expand All @@ -137,14 +138,14 @@ private function expandObjects(array $cache, array $objectKeys):array {
foreach($cache as $key => $closure) {
if($objectType = $objectKeys[$key] ?? null) {
$refClass = new ReflectionClass($objectType);
if($this->isBindable($refClass)) {
$refClassName = $refClass->getName();

if(isset($this->bindableClassMap[$refClassName]) || $this->isBindable($refClass)) {
$bindable = $this->bindableClassMap[$objectType];
foreach($bindable as $bindableKey => $bindableClosure) {
$cache["$key.$bindableKey"] = $bindableClosure;
}
}

// unset($cache[$key]);
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/phpunit/BindableCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Gt\DomTemplate\BindGetter;
use Gt\DomTemplate\BindableCache;
use Gt\DomTemplate\BindGetterMethodDoesNotStartWithGetException;
use Gt\DomTemplate\Test\TestHelper\Model\Address;
use Gt\DomTemplate\Test\TestHelper\Model\Customer;
use Gt\DomTemplate\Test\TestHelper\Model\Order;
use Gt\DomTemplate\Test\TestHelper\TestData;
use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down Expand Up @@ -178,6 +181,15 @@ public function testConvertToKvp_nestedObject():void {
}
}

public function testConvertKvp_nestedObjectSameType():void {
$sut = new BindableCache();
$parentCustomer = new Customer(100, "Parent Customer");
$customer = new Customer(101, "Test Customer", parentCustomer: $parentCustomer);
$kvp = $sut->convertToKvp($customer);
self::assertSame("101", $kvp["id"]);
self::assertSame("Test Customer", $kvp["name"]);
}

public function testConvertToKvp_nestedNullProperty():void {
$sut = new BindableCache();

Expand Down
1 change: 1 addition & 0 deletions test/phpunit/TestHelper/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public function __construct(
public string $name,
public ?Address $address = null,
public array $orderList = [],
public ?Customer $parentCustomer = null,
) {}

public function addOrder(Order $order):void {
Expand Down

0 comments on commit 4bc901d

Please sign in to comment.