Skip to content

Commit d283b51

Browse files
committed
feat: Add BaseModules property $registrarHasData
1 parent 86cf38a commit d283b51

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
@@ -276,21 +276,36 @@ protected function registerProperties()
276276

277277
$shortName = (new ReflectionClass($this))->getShortName();
278278

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

286-
$properties = $callable::$shortName();
299+
$currentProps = static::$moduleConfig->registrarHasData ? $worker->getProperties($this) : [];
300+
$properties = $callable::$shortName($currentProps);
287301

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

292306
foreach ($properties as $property => $value) {
293-
if (isset($this->{$property}) && is_array($this->{$property}) && is_array($value)) {
307+
// TODO: The array check can be removed if the option `registrarHasData` is accepted.
308+
if (isset($this->{$property}) && is_array($this->{$property}) && is_array($value) && ! static::$moduleConfig->registrarHasData) {
294309
$this->{$property} = array_merge($this->{$property}, $value);
295310
} else {
296311
$this->{$property} = $value;

0 commit comments

Comments
 (0)