phplibrary/src/Registry.php

50 lines
1.4 KiB
PHP

<?php
namespace ctiso;
use ctiso\File,
Exception;
class Registry {
private array $namespace = [];
function importFile(string $namespace, ?string $filePath = null) {
$data = json_decode(File::getContents($filePath), true);
$data['_file'] = $filePath;
$this->namespace[$namespace] = [
'path' => $filePath,
'data' => $data
];
}
function importArray(string $namespace, array $data = []) {
if (isset($this->namespace[$namespace])) {
$data = array_merge($this->namespace[$namespace]['data'], $data);
}
$this->namespace[$namespace] = [
'path' => null,
'data' => $data
];
}
public function get(string $ns, string $key) {
if (isset($this->namespace[$ns]['data'][$key])) {
return $this->namespace[$ns]['data'][$key];
}
throw new Exception('Unknown key ' . $ns . '::' . $key);
}
public function getOpt(string $ns, string $key) {
if (isset($this->namespace[$ns]['data'][$key])) {
return $this->namespace[$ns]['data'][$key];
}
return null;
}
public function has(string $ns, string $key): bool {
return isset($this->namespace[$ns]['data'][$key]);
}
function set(string $ns, string $key, mixed $value): void {
$this->namespace[$ns]['data'][$key] = $value;
}
}