Skip to content

Commit

Permalink
Basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Mar 6, 2018
1 parent 6bda60e commit bf449fa
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public function __construct(
$this->delimeter = $delimeter;
}

public function setDefault(
string $defaultDirectoryPath,
string $filename = "config.default.ini"
):void {
$defaults = $this->loadIni(
$defaultDirectoryPath,
$filename
);

$this->kvp = array_merge($defaults, $this->kvp);
}

public function get(string $name):?string {
$env = getenv($name);
if($env) {
Expand All @@ -24,6 +36,14 @@ public function get(string $name):?string {
return $this->getSectionValue($name);
}

public function getSection(string $sectionName):?ConfigSection {
if(!isset($this->kvp[$sectionName])) {
return null;
}

return new ConfigSection($this->kvp[$sectionName]);
}

protected function getSectionValue(string $name):?string {
$parts = explode($this->delimeter, $name);
$current = $this->kvp;
Expand All @@ -42,10 +62,10 @@ protected function getSectionValue(string $name):?string {
return $current;
}

protected function loadIni(string $projectRoot, string $filename):array {
protected function loadIni(string $directoryPath, string $filename):array {
$kvp = [];

$iniPath = $projectRoot . DIRECTORY_SEPARATOR . $filename;
$iniPath = $directoryPath . DIRECTORY_SEPARATOR . $filename;

if(is_file($iniPath)) {
$kvp = parse_ini_file($iniPath, true);
Expand Down
88 changes: 88 additions & 0 deletions src/ConfigSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace Gt\Config;

use ArrayAccess;
use Iterator;

class ConfigSection implements ArrayAccess, Iterator {
protected $data;
protected $iteratorIndex;

public function __construct(array $data) {
$this->data = $data;
}

/**
* @link http://php.net/manual/en/iterator.current.php
*/
public function current() {
$key = $this->getIteratorKey();
return $this->data[$key];
}

/**
* @link http://php.net/manual/en/iterator.next.php
*/
public function next():void {
$this->iteratorIndex++;
}

/**
* @link http://php.net/manual/en/iterator.key.php
*/
public function key():?string {
return $this->getIteratorKey();
}

/**
* @link http://php.net/manual/en/iterator.valid.php
*/
public function valid():bool {
$key = $this->getIteratorKey();
return isset($this->data[$key]);
}

/**
* @link http://php.net/manual/en/iterator.rewind.php
*/
public function rewind():void {
$this->iteratorIndex = 0;
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param string $offset
*/
public function offsetExists($offset):bool {
return isset($this->data[$offset]);
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param string $offset
*/
public function offsetGet($offset):?string {
return $this->data[$offset];
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetset.php
*/
public function offsetSet($offset, $value):void {
// TODO: Config should be immutable
// TODO: Implement offsetSet() method.
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
*/
public function offsetUnset($offset):void {
// TODO: Config should be immutable
// TODO: Implement offsetUnset() method.
}

protected function getIteratorKey():?string {
$keys = array_keys($this->data);
return $keys[$this->iteratorIndex] ?? null;
}
}

0 comments on commit bf449fa

Please sign in to comment.