Browse Source

Simplify configuration property caching

rpm
J. King 3 years ago
parent
commit
c1eff8479c
  1. 16
      lib/Conf.php

16
lib/Conf.php

@ -128,16 +128,14 @@ class Conf {
'dbSQLite3Timeout' => "double", 'dbSQLite3Timeout' => "double",
]; ];
protected static $types = []; protected $types = [];
/** Creates a new configuration object /** Creates a new configuration object
* @param string $import_file Optional file to read configuration data from * @param string $import_file Optional file to read configuration data from
* @see self::importFile() */ * @see self::importFile() */
public function __construct(string $import_file = "") { public function __construct(string $import_file = "") {
if (!static::$types) { $this->types = $this->propertyDiscover();
static::$types = $this->propertyDiscover(); foreach (array_keys($this->types) as $prop) {
}
foreach (array_keys(static::$types) as $prop) {
$this->$prop = $this->propertyImport($prop, $this->$prop); $this->$prop = $this->propertyImport($prop, $this->$prop);
} }
if ($import_file !== "") { if ($import_file !== "") {
@ -273,9 +271,9 @@ class Conf {
} }
protected function propertyImport(string $key, $value, string $file = "") { protected function propertyImport(string $key, $value, string $file = "") {
$typeName = static::$types[$key]['name'] ?? "mixed"; $typeName = $this->types[$key]['name'] ?? "mixed";
$typeConst = static::$types[$key]['const'] ?? Value::T_MIXED; $typeConst = $this->types[$key]['const'] ?? Value::T_MIXED;
$nullable = (int) (bool) (static::$types[$key]['const'] & Value::M_NULL); $nullable = (int) (bool) ($this->types[$key]['const'] & Value::M_NULL);
try { try {
if ($typeName === "\\DateInterval") { if ($typeName === "\\DateInterval") {
// date intervals have special handling: if the existing value (ultimately, the default value) // date intervals have special handling: if the existing value (ultimately, the default value)
@ -319,7 +317,7 @@ class Conf {
} }
return $value; return $value;
} catch (ExceptionType $e) { } catch (ExceptionType $e) {
$type = static::$types[$key]['const'] & ~(Value::M_STRICT | Value::M_DROP | Value::M_NULL | Value::M_ARRAY); $type = $this->types[$key]['const'] & ~(Value::M_STRICT | Value::M_DROP | Value::M_NULL | Value::M_ARRAY);
throw new Conf\Exception("typeMismatch", ['param' => $key, 'type' => self::TYPE_NAMES[$type], 'file' => $file, 'nullable' => $nullable]); throw new Conf\Exception("typeMismatch", ['param' => $key, 'type' => self::TYPE_NAMES[$type], 'file' => $file, 'nullable' => $nullable]);
} }
} }

Loading…
Cancel
Save