Version produccion
This commit is contained in:
138
aldarien/common/Alias/ConfigFile.php
Normal file
138
aldarien/common/Alias/ConfigFile.php
Normal file
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
namespace Aldarien\Common\Alias;
|
||||
|
||||
use Aldarien\Common\Definition\ConfigFile as ConfigFileInterface;
|
||||
|
||||
abstract class ConfigFile implements ConfigFileInterface {
|
||||
protected $name;
|
||||
public function setName(string $name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
protected $filename;
|
||||
public function setFilename(string $filename) {
|
||||
if (!file_exists($filename)) {
|
||||
throw new \DomainException('File not found: ' . $filename . ' in ' . get_called_class() . '.');
|
||||
}
|
||||
$this->filename = $filename;
|
||||
}
|
||||
protected $data;
|
||||
public function get(string $name, $default = null) {
|
||||
$result = $default;
|
||||
if ($this->has($name)) {
|
||||
$result = $this->data[$name];
|
||||
}
|
||||
if (is_array($result)) {
|
||||
foreach ($result as $k => $v) {
|
||||
$result[$k] = $this->translate($v);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
if (is_object($result)) {
|
||||
foreach ($result as $k => $v) {
|
||||
$result->$k = $this->translate($v);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
$result = $this->translate($result);
|
||||
return $result;
|
||||
}
|
||||
protected function translate($value) {
|
||||
if (is_object($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$value->$k = $this->translate($v);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$value[$k] = $this->translate($v);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
if (strpos($value, '{') !== false) {
|
||||
preg_match_all('/{(.*)}/', $value, $matches, \PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
$value = str_replace($match[0], $this->get($match[1]), $value);
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
public function set(string $name, $value): void {
|
||||
if ($this->data === null) {
|
||||
$this->data = [];
|
||||
}
|
||||
if (strpos($name, '.') === false) {
|
||||
$name = implode('.', [$this->name, $name]);
|
||||
}
|
||||
$this->data[$name] = $value;
|
||||
if (is_array($value)) {
|
||||
$is_object = false;
|
||||
foreach ($value as $k => $v) {
|
||||
if (is_numeric($k)) {
|
||||
continue;
|
||||
}
|
||||
$is_object = true;
|
||||
$n = implode('.', [$name, $k]);
|
||||
$this->set($n, $v);
|
||||
}
|
||||
if ($is_object) {
|
||||
$this->data[$name] = (object) $this->parse($value);
|
||||
}
|
||||
}
|
||||
if (is_object($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$n = implode('.', [$name, $k]);
|
||||
$this->set($n, $v);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected function parse($value) {
|
||||
if (is_array($value)) {
|
||||
$is_object = false;
|
||||
foreach ($value as $k => $v) {
|
||||
if (!is_numeric($k)) {
|
||||
$is_object = true;
|
||||
}
|
||||
$value[$k] = $this->parse($v);
|
||||
}
|
||||
$value = (object) $value;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
public function has(string $name): bool {
|
||||
if (isset($this->data[$name])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected $loaded;
|
||||
public function isLoaded(): bool {
|
||||
return ($this->loaded !== null and $this->loaded);
|
||||
}
|
||||
public function toArray() {
|
||||
$arr = [];
|
||||
foreach ($this->data as $key => $value) {
|
||||
$arr[$key] = $this->translate($value);
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
protected $position;
|
||||
public function current() {
|
||||
return $this->data[array_keys($this->data)[$this->position]];
|
||||
}
|
||||
public function key(): scalar {
|
||||
return array_keys($this->data)[$this->position];
|
||||
}
|
||||
public function rewind() {
|
||||
$this->position = 0;
|
||||
}
|
||||
public function next() {
|
||||
$this->position ++;
|
||||
}
|
||||
public function valid() {
|
||||
return isset(array_keys($this->data)[$this->position]);
|
||||
}
|
||||
}
|
8
aldarien/common/Alias/Model.php
Normal file
8
aldarien/common/Alias/Model.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace Aldarien\Common\Alias;
|
||||
|
||||
use Model as BaseModel;
|
||||
|
||||
class Model extends BaseModel {
|
||||
|
||||
}
|
14
aldarien/common/Definition/ConfigFile.php
Normal file
14
aldarien/common/Definition/ConfigFile.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Aldarien\Common\Definition;
|
||||
|
||||
interface ConfigFile extends \Iterator {
|
||||
public function setName(string $name);
|
||||
public function getName(): string;
|
||||
public function setFilename(string $filename);
|
||||
public function get(string $name, $default = null);
|
||||
public function set(string $name, $value): void;
|
||||
public function has(string $name): bool;
|
||||
public function load();
|
||||
public function isLoaded(): bool;
|
||||
public function save();
|
||||
}
|
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);
|
||||
}
|
||||
}
|
17
aldarien/src/Config/JSON.php
Normal file
17
aldarien/src/Config/JSON.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Aldarien\Config;
|
||||
|
||||
use Aldarien\Common\Alias\ConfigFile;
|
||||
|
||||
class JSON extends ConfigFile {
|
||||
public function load() {
|
||||
$data = json_decode(file_get_contents($this->filename));
|
||||
foreach ($data as $k => $v) {
|
||||
$this->set($k, $v);
|
||||
}
|
||||
$this->loaded = true;
|
||||
}
|
||||
public function save() {
|
||||
file_put_contents($this->filename, json_encode($this->data, \JSON_PRETTY_PRINT || \JSON_NUMERIC_CHECK || \JSON_PRESERVE_ZERO_FRACTION));
|
||||
}
|
||||
}
|
24
aldarien/src/Config/PHP.php
Normal file
24
aldarien/src/Config/PHP.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php namespace Aldarien\Config;
|
||||
|
||||
use Aldarien\Common\Alias\ConfigFile;
|
||||
|
||||
class PHP extends ConfigFile {
|
||||
public function load() {
|
||||
$data = include($this->filename);
|
||||
foreach ($data as $k => $v) {
|
||||
$this->set($k, $v);
|
||||
}
|
||||
$this->loaded = true;
|
||||
}
|
||||
public function save() {
|
||||
$str = [];
|
||||
$str []= '<?php';
|
||||
$str []= 'return [';
|
||||
foreach ($this->data as $k => $v) {
|
||||
$str []= "'" . $k . "' => " . $v;
|
||||
}
|
||||
$str []= '];';
|
||||
$str []= '';
|
||||
file_put_contents($this->filename, implode(PHP_EOL, $str));
|
||||
}
|
||||
}
|
18
aldarien/src/Config/YAML.php
Normal file
18
aldarien/src/Config/YAML.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Aldarien\Config;
|
||||
|
||||
use Spyc;
|
||||
use Aldarien\Common\Alias\ConfigFile;
|
||||
|
||||
class YAML extends ConfigFile {
|
||||
public function load() {
|
||||
$data = Spyc::YAMLLoad($this->filename);
|
||||
foreach ($data as $k => $v) {
|
||||
$this->set($k, $v);
|
||||
}
|
||||
$this->loaded = true;
|
||||
}
|
||||
public function save() {
|
||||
file_put_contents($this->filename, Spyc::YAMLDump($this->data));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user