Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eurobertics committed Jul 15, 2020
1 parent cd095f2 commit 68d54fa
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
composer.lock
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "eurobertics/connfetti-ini",
"description": "A INI config file reader displaying the config in class hierarchy.",
"type": "library",
"minimum-stability": "stable",
"require": {
"php": "^7.3",
"eurobertics/connfetti-io": "*"
},
"autoload": {
"psr-4" : {
"Connfetti\\INI\\" : "src/"
}
},
"license": "GPL-3.0-or-later",
"authors": [
{
"name": "Bernd Robertz",
"email": "brobertz.net@gmail.com"
}
]
}
81 changes: 81 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
namespace Connfetti\INI\Config;

use Connfetti\IO\Base\IOFactory;
use Connfetti\IO\Reader\INIReader;

class Config
{
public static $VERSION = '0.1.0';

private $cfgfile;
private $cfgpath;

private $cfgdata;

public function __construct(string $file, string $path = './')
{
$this->cfgfile = $file;
$this->cfgpath = $path;

$this->init();
}

public function __set($name, $value)
{
throw new \Exception("Config is readonly!");
}

public function __get($name)
{
if(isset($this->cfgdata->$name)) {
return $this->cfgdata->$name;
}
return null;
}

public function __isset($name)
{
return isset($this->cfgdata->$name);
}

private function init()
{
/** @var INIReader $oReader */
$oReader = IOFactory::createReader(IOFactory::FILE_INI, $this->cfgpath . "/" . $this->cfgfile);
$this->cfgdata = $this->setPropertiesByCfg($this->setCfgArray($oReader->getContent()));
}

private function setCfgArray($cfgdata)
{
$tree = array();
foreach($cfgdata as $prop => $iniset) {
$propparts = explode(".", $iniset[0]);
$val = $iniset[1];

foreach(array_reverse($propparts) as $part) {
$val = array($part => $val);
}
$tree = array_merge_recursive($tree, $val);
}

return $tree;
}

private function setPropertiesByCfg($cfgtree_ar)
{
if(!is_array($cfgtree_ar)) {
return $cfgtree_ar;
}

$obj = new \stdClass();
if(count($cfgtree_ar) > 0) {
foreach($cfgtree_ar as $node => $item) {
$obj->$node = $this->setPropertiesByCfg($item);
}
return $obj;
} else {
return null;
}
}
}
17 changes: 17 additions & 0 deletions src/confetti_ini_autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
function confetti_ini_autoloader($class) {

$basedir = __DIR__;

$classname = $class;
if(strpos($class, "\\") !== false) {
$classname = str_replace("Connfetti\\INI\\", "\\", $classname);
$classname = str_replace("\\", "/", $classname);
}

$class_uri = $basedir."/".$classname.".php";
if(file_exists($class_uri)) {
include $class_uri;
}
}
spl_autoload_register('connfetti_ini_autoloader');

0 comments on commit 68d54fa

Please sign in to comment.