Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
giveit-deploy committed Nov 21, 2012
1 parent a63be5e commit ca9e5a9
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Config.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* A config parsing class with multiple types of input and output
*
* @author David Kelly
* @license GPL v3
*
*/
namespace opensourceame;

class Config
{
const version = '0.1';

private $parsers = array();
private $outputs = array();

public function __call($method, $arguments = null)
{
var_dump($method);
if (! method_exists($this, $method)) {

if (substr($method, 0, 4) == 'read') {

$parser = substr($method, 4);
$plugin = $this->getParser($parser);

if ($plugin === false) {
return false;
}

return call_user_func_array(array($plugin, 'parse'), $arguments);
}

if (substr($method, 0, 6) == 'saveTo') {

$name = substr($method, 6);
$plugin = $this->getOutput($name);

if ($plugin === false) {
return false;
}

return call_user_func_array(array($plugin, 'input'), $arguments);
}


}

}

private function getParser($name)
{
require_once __DIR__ . "/Parser/Parser.php";
require_once __DIR__ . "/Parser/$name.php";

if ($name == 'Array') {
$name = 'ArrayParser';
}

$className = '\\opensourceame\\Config\Parser\\' . $name;
$plugin = new $className($this);

return $plugin;
}


private function getOuput($name)
{

}

public function setFromArray($data)
{
if (! is_array($data)) {
return false;
}

$this->config = $data;

return true;
}

}
1 change: 1 addition & 0 deletions Output/Array.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
<?php
1 change: 1 addition & 0 deletions Output/Flat.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
<?php
1 change: 1 addition & 0 deletions Output/JSON.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
<?php
19 changes: 19 additions & 0 deletions Output/Output.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace opensourceame\Config\Output;

class Output
{

protected $parent = null;

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

public function output()
{

}
}
1 change: 1 addition & 0 deletions Output/Redis.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
<?php
13 changes: 13 additions & 0 deletions Output/YAML.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace opensourceame\Config\Output;

class YAML
{

public function output($options)
{


}
}
20 changes: 20 additions & 0 deletions Parser/Array.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace opensourceame\Config\Parser;

/**
* Note, this class name uses a different name to prevent an issue with using the reserved
* word "Array"
*
* @author David Kelly
*
*/
class ArrayParser extends Parser
{

public function parse($data)
{
return $this->parent->setFromArray($data);
}

}
8 changes: 8 additions & 0 deletions Parser/CommandLine.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace opensourceame\Config\Parser;

class CommandLine extends Parser
{

}
1 change: 1 addition & 0 deletions Parser/Json.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
<?php
13 changes: 13 additions & 0 deletions Parser/Parser.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace opensourceame\Config\Parser;

class Parser
{
protected $parent = null;

public function __construct($parent)
{
$this->parent = $parent;
}
}
47 changes: 47 additions & 0 deletions Parser/Redis.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace opensourceame\Config\Parser;

class Yaml extends Parser
{
private $redis;
private $defaultSettings = array(
'host' => 'localhost',
'db' => 1,

);

public function parse($settings)
{
$this->settings = array_merge($this->defaultSettings, $settings);

$this->setupRedis();


}


protected function setupRedis()
{
if (is_object($this->redis)) {
return true;
}

$this->redis = new Redis;

try {
$this->redis->connect($this->settings['host']);
$this->redis->select($this->settings['db']);

} catch (Exception $e) {

throw new ErrorException('unable to connect to redis server');

return false;
}

return true;
}


}
48 changes: 48 additions & 0 deletions Parser/Yaml.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace opensourceame\Config\Parser;

class Yaml extends Parser
{
public function parse($yaml)
{

echo "FJKDLFJLK";
// first check if we have been passed a file

if (is_readable($yaml)) {
return $this->parseFile($yaml);
}

return $this->parseText($yaml);
}

public function parseFile($configFile)
{

if (function_exists('yaml_parse_file')) {

$yaml = yaml_parse_file($configFile);

} else {

$yaml = \Spyc::YAMLLoad($configFile);
}

if (! is_array($yaml))
return false;

$this->parent->registerFileRead($configFile, 'yaml');

return $this->parent->parseArray($yaml);

}

public function parseText($text)
{
$yaml = yaml_parse($text);

print_r($yaml);
}

}
10 changes: 10 additions & 0 deletions Parser/test.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


require_once 'Config.php';

$config = new \opensourceame\Config;

$config->readYaml('test.yaml');


5 changes: 5 additions & 0 deletions Parser/test.yaml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
this:
is:
a:
test: yes

16 changes: 16 additions & 0 deletions test.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require_once 'Config.php';

$yaml = <<<END
this:
is:
a:
test: yay
END;

$config = new \opensourceame\Config;

$config->readYaml($yaml);

print_r($config);

0 comments on commit ca9e5a9

Please sign in to comment.