Skip to content

Commit d8b2575

Browse files
author
Greg Bowler
committed
feature: only update the changed properties of the task blocks
1 parent f6866fa commit d8b2575

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/Configuration/Manifest.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(string $jsonFilePath, ?string $mode = null) {
4141
// For legacy reasons, stdClass is used to represent the block details.
4242
// This code might look weird, but it remains backwards compatible until an OOP
4343
// refactoring is made.
44-
$json = (object)array_merge((array)$json, (array)$modeJson);
44+
$json = $this->recursiveMerge($json, $modeJson);
4545
}
4646

4747
$this->taskBlockList = [];
@@ -84,4 +84,22 @@ protected function setIteratorKey():void {
8484
$keys = array_keys($this->taskBlockList);
8585
$this->iteratorKey = $keys[$this->iteratorIndex] ?? null;
8686
}
87+
88+
private function recursiveMerge(object $json, object $diff):object {
89+
foreach($diff as $key => $value) {
90+
if(property_exists($json, $key)) {
91+
if(is_object($value)) {
92+
$json->$key = $this->recursiveMerge($json->$key, $value);
93+
}
94+
else {
95+
$json->$key = $value;
96+
}
97+
}
98+
else {
99+
$json->$key = $value;
100+
}
101+
}
102+
103+
return $json;
104+
}
87105
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"/tmp/phpgt/build/*.txt": {
3+
"execute": {
4+
"arguments": ["hello", "text", "single", "property"]
5+
}
6+
}
7+
}

test/phpunit/ManifestTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,24 @@ public function testIterator_mode():void {
3535
next($jsonObj);
3636
}
3737
}
38+
39+
public function testIterator_modeOnlyOverridesSingleProperty():void {
40+
$jsonFile = "test/phpunit/Helper/Json/build.json";
41+
$jsonObj = json_decode(file_get_contents($jsonFile), true);
42+
43+
$sut = new Manifest($jsonFile, "single-property-override");
44+
/** @var TaskBlock $taskBlock */
45+
foreach($sut as $taskBlock) {
46+
$currentJsonObj = current($jsonObj);
47+
self::assertSame($currentJsonObj["name"], $taskBlock->getName());
48+
49+
if($currentJsonObj["name"] === "Example dev TXT") {
50+
self::assertSame(["hello", "text", "single", "property"], $taskBlock->getExecuteBlock()->arguments);
51+
}
52+
else {
53+
self::assertSame($currentJsonObj["execute"]["arguments"], $taskBlock->getExecuteBlock()->arguments);
54+
}
55+
next($jsonObj);
56+
}
57+
}
3858
}

0 commit comments

Comments
 (0)