Version produccion
This commit is contained in:
86
aldarien/common/Service/Config.php
Normal file
86
aldarien/common/Service/Config.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
namespace Aldarien\Common\Service;
|
||||
|
||||
class Config implements \ArrayAccess {
|
||||
protected $folder;
|
||||
public function __construct(string $config_folder) {
|
||||
if (!file_exists($config_folder)) {
|
||||
throw new \InvalidArgumentException("\$config_folder for Config does not exist.");
|
||||
}
|
||||
$this->folder = $config_folder;
|
||||
}
|
||||
public function load() {
|
||||
$files = new \DirectoryIterator($this->folder);
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
continue;
|
||||
}
|
||||
$this->loadFile($file);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected $files;
|
||||
protected function loadFile(\SplFileInfo $file): void {
|
||||
$ext = $file->getExtension();
|
||||
$name = strtolower(str_replace(' ', '_', $file->getBasename('.' . $ext)));
|
||||
$class = implode("\\", [
|
||||
'Aldarien',
|
||||
'Config',
|
||||
str_replace('YML', 'YAML', strtoupper($ext))
|
||||
]);
|
||||
if (!class_exists($class)) {
|
||||
return;
|
||||
}
|
||||
$obj = new $class();
|
||||
$obj->setName($name);
|
||||
$obj->setFilename($file->getRealPath());
|
||||
if ($this->files === null) {
|
||||
$this->files = [];
|
||||
}
|
||||
$this->files []= $obj;
|
||||
}
|
||||
public function get(string $name, $default = null) {
|
||||
foreach ($this->files as $obj) {
|
||||
if (!$obj->isLoaded()) {
|
||||
$obj->load();
|
||||
}
|
||||
if ($obj->has($name)) {
|
||||
return $obj->get($name);
|
||||
}
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
public function set(string $name, $value) {
|
||||
if (strpos($name, '.') === false) {
|
||||
return false;
|
||||
}
|
||||
$arr = explode('.', $name);
|
||||
$file = array_shift($arr);
|
||||
$name = implode('.', $arr);
|
||||
$this->files[$file]->set($name, $value);
|
||||
}
|
||||
public function remove(string $name) {
|
||||
}
|
||||
public function toArray() {
|
||||
$arr = [];
|
||||
foreach ($this->files as $obj) {
|
||||
if (!$obj->isLoaded()) {
|
||||
$obj->load();
|
||||
}
|
||||
$arr = array_merge($arr, $obj->toArray());
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
public function offsetExists($offset): bool {
|
||||
return $this->has($offset);
|
||||
}
|
||||
public function offsetGet($offset) {
|
||||
return $this->get($offset);
|
||||
}
|
||||
public function offsetSet($offset, $value) {
|
||||
$this->set($offset, $value);
|
||||
}
|
||||
public function offsetUnset ($offset) {
|
||||
$this->remove($offset);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user