Skip to content

Commit c84fbde

Browse files
committed
feat: Add BaseModules property $registrarHasData
1 parent a348792 commit c84fbde

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

app/Config/Modules.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ class Modules extends BaseModules
6262
*/
6363
public $composerPackages = [];
6464

65+
/**
66+
* If set to `true`, Registrars may have previous config values as an array.
67+
* This is useful when updating arrays or checking properties.
68+
*
69+
* NOTE: Enable this option only for trusted modules/packages.
70+
*/
71+
public bool $registrarHasData = false;
72+
6573
/**
6674
* --------------------------------------------------------------------------
6775
* Auto-Discovery Rules

system/Config/BaseConfig.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,21 +274,36 @@ protected function registerProperties()
274274

275275
$shortName = (new ReflectionClass($this))->getShortName();
276276

277+
if (static::$moduleConfig->registrarHasData) {
278+
// Get all public properties for this config
279+
$worker = new class () {
280+
/**
281+
* @return array<string, mixed>
282+
*/
283+
public function getProperties(BaseConfig $obj): array
284+
{
285+
return get_object_vars($obj);
286+
}
287+
};
288+
}
289+
277290
// Check the registrar class for a method named after this class' shortName
278291
foreach (static::$registrars as $callable) {
279292
// ignore non-applicable registrars
280293
if (! method_exists($callable, $shortName)) {
281294
continue; // @codeCoverageIgnore
282295
}
283296

284-
$properties = $callable::$shortName();
297+
$currentProps = static::$moduleConfig->registrarHasData ? $worker->getProperties($this) : [];
298+
$properties = $callable::$shortName($currentProps);
285299

286300
if (! is_array($properties)) {
287301
throw new RuntimeException('Registrars must return an array of properties and their values.');
288302
}
289303

290304
foreach ($properties as $property => $value) {
291-
if (isset($this->{$property}) && is_array($this->{$property}) && is_array($value)) {
305+
// TODO: The array check can be removed if the option `registrarHasData` is accepted.
306+
if (isset($this->{$property}) && is_array($this->{$property}) && is_array($value) && ! static::$moduleConfig->registrarHasData) {
292307
$this->{$property} = array_merge($this->{$property}, $value);
293308
} else {
294309
$this->{$property} = $value;

0 commit comments

Comments
 (0)