-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd095f2
commit 68d54fa
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |