Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

various updates, especially to parsers

  • Loading branch information...
commit 31d9eb657f463d5a7a7a9aeb8325e18cbc556d13 1 parent 6a3937d
giveit-deploy authored
116  Config.php
@@ -11,13 +11,15 @@
11 11
 class Config
12 12
 {
13 13
 	const			version		= '0.1';
  14
+	const			notset		= '__not_set';
14 15
 
15  
-	private			$parsers	= array();
16  
-	private			$outputs	= array();
  16
+	private			$filesRead	= array();
  17
+	private			$errors		= array();
  18
+
  19
+	public			$delimiter	= ':';
17 20
 
18 21
 	public function __call($method, $arguments = null)
19 22
 	{
20  
-		var_dump($method);
21 23
 		if (! method_exists($this, $method)) {
22 24
 
23 25
 			if (substr($method, 0, 4) == 'read') {
@@ -49,6 +51,11 @@ public function __call($method, $arguments = null)
49 51
 
50 52
 	}
51 53
 
  54
+	public function addError($message)
  55
+	{
  56
+		array_push($this->errors, $message);
  57
+	}
  58
+
52 59
 	private function getParser($name)
53 60
 	{
54 61
 		require_once __DIR__ . "/Parser/Parser.php";
@@ -70,15 +77,116 @@ private function getOuput($name)
70 77
 
71 78
 	}
72 79
 
  80
+	public function set($key, $value) {
  81
+
  82
+		if (substr($key, -2) == $this->delimiter . '+') {
  83
+			return $this->append(substr($name, 0, strlen($name) -2), $value);
  84
+		}
  85
+
  86
+		$this->config[$key] = $value;
  87
+
  88
+		return true;
  89
+	}
  90
+
  91
+	public function append($name, $value)
  92
+	{
  93
+		$values = $this->getArray($name . $this->delimiter . '*');
  94
+		$index	= count($values) + 1;
  95
+
  96
+		$this->set($name . $this->delimiter . $index, $value);
  97
+
  98
+		return true;
  99
+	}
  100
+
  101
+	public function get($name = null)
  102
+	{
  103
+
  104
+		if (isset($this->config[$name])) {
  105
+			return $this->config[$name];
  106
+		}
  107
+
  108
+		if($name == null) {
  109
+			return $this->config;
  110
+		}
  111
+
  112
+		if (substr($name, -1) == '*') {
  113
+
  114
+			$matches	= array();
  115
+			$search		= substr($name, 0, strlen($name) -1);
  116
+
  117
+			foreach($this->config as $key => $val) {
  118
+
  119
+				if (substr($key, 0, strlen($search)) == $search) {
  120
+
  121
+					$matches[substr($key, strlen($search))] = $val;
  122
+				}
  123
+			}
  124
+
  125
+			return $matches;
  126
+		}
  127
+
  128
+		return self::notset;
  129
+	}
  130
+
  131
+	static public function objectToArray( $object )
  132
+	{
  133
+		if ( (! is_object($object)) and (! is_array( $object ))) {
  134
+
  135
+			return $object;
  136
+		}
  137
+
  138
+		if (is_object($object)) {
  139
+
  140
+			$object = get_object_vars($object);
  141
+		}
  142
+
  143
+		return array_map('\opensourceame\Config::objectToArray', $object);
  144
+	}
  145
+
73 146
 	public function setFromArray($data)
74 147
 	{
  148
+		if (is_object($data)) {
  149
+			$data = $this->objectToArray($data);
  150
+		}
  151
+
75 152
 		if (! is_array($data)) {
76 153
 			return false;
77 154
 		}
78 155
 
79  
-		$this->config = $data;
  156
+		$flat = $this->flattenArray($data);
  157
+
  158
+		foreach ($flat as $key => $val) {
  159
+			$this->set($key, $val);
  160
+		}
80 161
 
81 162
 		return true;
82 163
 	}
83 164
 
  165
+	private function flattenArray($data, $prefix = false, & $flat = array())
  166
+	{
  167
+		if ($prefix !== false) {
  168
+			$prefix .= $this->delimiter;
  169
+		}
  170
+
  171
+		foreach ($data as $key => $val) {
  172
+
  173
+			if (is_array($val) and ! empty($val)) {
  174
+
  175
+				$this->flattenArray($val, $prefix.$key, $flat);
  176
+
  177
+			} else {
  178
+
  179
+				$flat[$prefix.$key] = $val;
  180
+			}
  181
+		}
  182
+
  183
+		return $flat;
  184
+	}
  185
+
  186
+	public function registerFileRead($filename, $type = 'unknown')
  187
+	{
  188
+		$this->filesRead[$type][] = $filename;
  189
+
  190
+		return true;
  191
+	}
84 192
 }
0  Output/JSON.php → Output/Json.php
File renamed without changes
0  Output/YAML.php → Output/Yaml.php
File renamed without changes
23  Parser/Cli.php
... ...
@@ -0,0 +1,23 @@
  1
+<?php
  2
+
  3
+namespace opensourceame\Config\Parser;
  4
+
  5
+class Cli extends Parser
  6
+{
  7
+	public function parse()
  8
+	{
  9
+		$argc	= $GLOBALS['argc'];
  10
+		$argv	= $GLOBALS['argv'];
  11
+
  12
+		for ($i = 1; $i < $argc; $i++) {
  13
+
  14
+			if (substr($argv[$i], 0, 2) == '--') {
  15
+
  16
+				$name 	= substr($argv[$i], 2);
  17
+				$value	= isset($argv[$i + 1]) ? $argv[$i + 1] : true;
  18
+
  19
+				$this->parent->set($name, $value);
  20
+			}
  21
+		}
  22
+	}
  23
+}
8  Parser/CommandLine.php
... ...
@@ -1,8 +0,0 @@
1  
-<?php
2  
-
3  
-namespace opensourceame\Config\Parser;
4  
-
5  
-class CommandLine extends Parser
6  
-{
7  
-
8  
-}
55  Parser/Json.php
... ...
@@ -1 +1,56 @@
1 1
 <?php
  2
+
  3
+namespace opensourceame\Config\Parser;
  4
+
  5
+class Json extends Parser
  6
+{
  7
+	private		$errors		= array(
  8
+					'No error',
  9
+					'The maximum stack depth has been exceeded',
  10
+					'Invalid or malformed JSON',
  11
+					'Control character error, possibly incorrectly encoded',
  12
+					'Syntax error',
  13
+					'Malformed UTF-8 characters, possibly incorrectly encoded',
  14
+				);
  15
+	public function parse($json)
  16
+	{
  17
+		// first check if we have been passed a file
  18
+
  19
+		if (file_exists($json) and is_readable($json)) {
  20
+			return $this->parseFile($json);
  21
+		}
  22
+
  23
+		// not a file, parse the text
  24
+
  25
+		return $this->parseText($json);
  26
+	}
  27
+
  28
+	public function parseFile($filename)
  29
+	{
  30
+		print_r(file_get_contents($filename));
  31
+		if (! is_readable($filename)) {
  32
+			$this->parent->addError("unable to read $filename");
  33
+		}
  34
+
  35
+		$json = json_decode(file_get_contents($filename));
  36
+
  37
+		if (! is_array($json)) {
  38
+
  39
+			$this->parent->addError("unable to parse JSON in $filename: " . $this->errors[json_last_error()]);
  40
+
  41
+			return false;
  42
+		}
  43
+
  44
+		$this->parent->registerFileRead($filename, 'json');
  45
+
  46
+		return $this->setFromArray($json);
  47
+	}
  48
+
  49
+	public function parseText($text)
  50
+	{
  51
+		$json = json_decode($text);
  52
+
  53
+		return $this->setFromArray($json);
  54
+	}
  55
+
  56
+}
5  Parser/Parser.php
@@ -10,4 +10,9 @@ public function __construct($parent)
10 10
 	{
11 11
 		$this->parent = $parent;
12 12
 	}
  13
+
  14
+	protected function setFromArray($data) {
  15
+
  16
+		return $this->parent->setFromArray($data);
  17
+	}
13 18
 }
24  Parser/Yaml.php
@@ -6,43 +6,43 @@ class Yaml extends Parser
6 6
 {
7 7
 	public function parse($yaml)
8 8
 	{
9  
-
10  
-		echo "FJKDLFJLK";
11 9
 		// first check if we have been passed a file
12 10
 
13  
-		if (is_readable($yaml)) {
  11
+		if (file_exists($yaml) and is_readable($yaml)) {
14 12
 			return $this->parseFile($yaml);
15 13
 		}
16 14
 
  15
+		// not a file, parse the text
  16
+
17 17
 		return $this->parseText($yaml);
18 18
 	}
19 19
 
20  
-	public function parseFile($configFile)
  20
+	public function parseFile($filename)
21 21
 	{
22  
-
23 22
 		if (function_exists('yaml_parse_file'))  {
24 23
 
25  
-			$yaml = yaml_parse_file($configFile);
  24
+			$yaml = yaml_parse_file($filename);
26 25
 
27 26
 		} else {
28 27
 
29  
-			$yaml = \Spyc::YAMLLoad($configFile);
  28
+			$yaml = \Spyc::YAMLLoad($filename);
30 29
 		}
31 30
 
32  
-		if (! is_array($yaml))
33  
-			return false;
  31
+		if (! is_array($yaml)) {
34 32
 
35  
-		$this->parent->registerFileRead($configFile, 'yaml');
  33
+			return false;
  34
+		}
36 35
 
37  
-		return $this->parent->parseArray($yaml);
  36
+		$this->parent->registerFileRead($filename, 'yaml');
38 37
 
  38
+		return $this->setFromArray($yaml);
39 39
 	}
40 40
 
41 41
 	public function parseText($text)
42 42
 	{
43 43
 		$yaml = yaml_parse($text);
44 44
 
45  
-		print_r($yaml);
  45
+		return $this->setFromArray($yaml);
46 46
 	}
47 47
 
48 48
 }
10  Parser/test.php
... ...
@@ -1,10 +0,0 @@
1  
-<?php
2  
-
3  
-
4  
-require_once 'Config.php';
5  
-
6  
-$config = new \opensourceame\Config;
7  
-
8  
-$config->readYaml('test.yaml');
9  
-
10  
-
5  Parser/test.yaml
... ...
@@ -1,5 +0,0 @@
1  
-this:
2  
-  is:
3  
-    a:
4  
-      test:   yes
5  
-
16  test.php
... ...
@@ -1,16 +0,0 @@
1  
-<?php
2  
-
3  
-require_once 'Config.php';
4  
-
5  
-$yaml = <<<END
6  
-this:
7  
-  is:
8  
-    a:
9  
-      test:  yay
10  
-END;
11  
-
12  
-$config = new \opensourceame\Config;
13  
-
14  
-$config->readYaml($yaml);
15  
-
16  
-print_r($config);
8  tests/test.json
... ...
@@ -0,0 +1,8 @@
  1
+{
  2
+  "this": {
  3
+    "is": "Jason",
  4
+    "in": {
  5
+      "a": "file"
  6
+    }
  7
+  }
  8
+}
41  tests/test.php
... ...
@@ -0,0 +1,41 @@
  1
+<?php
  2
+
  3
+require_once '../Config.php';
  4
+
  5
+$yaml = <<<END
  6
+this:
  7
+  is:
  8
+    a:
  9
+      test:  yay
  10
+
  11
+list:
  12
+  - first
  13
+  - second
  14
+  - third
  15
+END;
  16
+
  17
+$json = <<<END
  18
+{
  19
+  "some": {
  20
+  	"json": "text"
  21
+  }
  22
+}
  23
+END;
  24
+
  25
+$config = new \opensourceame\Config;
  26
+
  27
+// read from YAML strings and files
  28
+$config->readYaml($yaml);
  29
+$config->readYaml('test.yaml');
  30
+
  31
+// read from JSON strings and files
  32
+$config->readJson($json);
  33
+$config->readJson('test.json');
  34
+
  35
+// read from the command line
  36
+$config->readCli();
  37
+
  38
+print_r($config);
  39
+
  40
+var_dump($config->get('this:is:a:test'));
  41
+var_dump($config->get('list:*'));
5  tests/test.yaml
... ...
@@ -0,0 +1,5 @@
  1
+lets:
  2
+  parse:
  3
+    a:
  4
+      yaml: file
  5
+

0 notes on commit 31d9eb6

Please sign in to comment.
Something went wrong with that request. Please try again.