Skip to content
Browse files

initial commit

  • Loading branch information...
1 parent a63be5e commit ca9e5a95e584be10af2a16471e4f0cf029975150 @giveit-deploy giveit-deploy committed Nov 21, 2012
Showing with 288 additions and 0 deletions.
  1. +84 −0 Config.php
  2. +1 −0 Output/Array.php
  3. +1 −0 Output/Flat.php
  4. +1 −0 Output/JSON.php
  5. +19 −0 Output/Output.php
  6. +1 −0 Output/Redis.php
  7. +13 −0 Output/YAML.php
  8. +20 −0 Parser/Array.php
  9. +8 −0 Parser/CommandLine.php
  10. +1 −0 Parser/Json.php
  11. +13 −0 Parser/Parser.php
  12. +47 −0 Parser/Redis.php
  13. +48 −0 Parser/Yaml.php
  14. +10 −0 Parser/test.php
  15. +5 −0 Parser/test.yaml
  16. +16 −0 test.php
View
84 Config.php
@@ -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;
+ }
+
+}
View
1 Output/Array.php
@@ -0,0 +1 @@
+<?php
View
1 Output/Flat.php
@@ -0,0 +1 @@
+<?php
View
1 Output/JSON.php
@@ -0,0 +1 @@
+<?php
View
19 Output/Output.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace opensourceame\Config\Output;
+
+class Output
+{
+
+ protected $parent = null;
+
+ public function __construct($parent)
+ {
+ $this->parent = $parent;
+ }
+
+ public function output()
+ {
+
+ }
+}
View
1 Output/Redis.php
@@ -0,0 +1 @@
+<?php
View
13 Output/YAML.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace opensourceame\Config\Output;
+
+class YAML
+{
+
+ public function output($options)
+ {
+
+
+ }
+}
View
20 Parser/Array.php
@@ -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);
+ }
+
+}
View
8 Parser/CommandLine.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace opensourceame\Config\Parser;
+
+class CommandLine extends Parser
+{
+
+}
View
1 Parser/Json.php
@@ -0,0 +1 @@
+<?php
View
13 Parser/Parser.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace opensourceame\Config\Parser;
+
+class Parser
+{
+ protected $parent = null;
+
+ public function __construct($parent)
+ {
+ $this->parent = $parent;
+ }
+}
View
47 Parser/Redis.php
@@ -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;
+ }
+
+
+}
View
48 Parser/Yaml.php
@@ -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);
+ }
+
+}
View
10 Parser/test.php
@@ -0,0 +1,10 @@
+<?php
+
+
+require_once 'Config.php';
+
+$config = new \opensourceame\Config;
+
+$config->readYaml('test.yaml');
+
+
View
5 Parser/test.yaml
@@ -0,0 +1,5 @@
+this:
+ is:
+ a:
+ test: yes
+
View
16 test.php
@@ -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.
Something went wrong with that request. Please try again.