-
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
a63be5e
commit ca9e5a9
Showing
16 changed files
with
288 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 | 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; | |||
} | |||
|
|||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
<?php |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
<?php |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
<?php |
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 | 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() | |||
{ | |||
|
|||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
<?php |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
<?php | |||
|
|||
namespace opensourceame\Config\Output; | |||
|
|||
class YAML | |||
{ | |||
|
|||
public function output($options) | |||
{ | |||
|
|||
|
|||
} | |||
} |
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 | 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); | |||
} | |||
|
|||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
<?php | |||
|
|||
namespace opensourceame\Config\Parser; | |||
|
|||
class CommandLine extends Parser | |||
{ | |||
|
|||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
<?php |
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 | 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; | |||
} | |||
} |
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 | 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; | |||
} | |||
|
|||
|
|||
} |
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 | 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); | |||
} | |||
|
|||
} |
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 | 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'); | |||
|
|||
|
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
this: | |||
is: | |||
a: | |||
test: yes | |||
|
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 | 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); |