This repository was archived by the owner on Jan 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.php
More file actions
152 lines (134 loc) · 3.74 KB
/
Repository.php
File metadata and controls
152 lines (134 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Kasseler\Component\Config;
use Kasseler\Component\Config\Reader\AbstractFileReader;
use Kasseler\Component\Config\Writer\AbstractFileWriter;
class Repository
{
private $repository;
private $delimiter = '.';
private $config = [];
private $change = [];
private $reader;
private $writer;
/**
* @param AbstractFileReader $reader
* @param AbstractFileWriter $writer
* @param $repository
*/
public function __construct(AbstractFileReader $reader, AbstractFileWriter $writer, $repository)
{
$this->reader = $reader;
$this->writer = $writer;
$this->repository = $repository;
}
/**
* @param $array
*
* @return array
*/
private function explodeTree($array)
{
$splitRE = '/' . preg_quote($this->delimiter, '/') . '/';
$returnArr = [];
foreach ($array as $key => $val) {
$parts = preg_split($splitRE, $key, -1, PREG_SPLIT_NO_EMPTY);
$leafPart = array_pop($parts);
$parentArr = &$returnArr;
foreach ($parts as $part) {
if (!isset($parentArr[$part])) {
$parentArr[$part] = [];
} elseif (!is_array($parentArr[$part])) {
$parentArr[$part] = [];
}
$parentArr = &$parentArr[$part];
}
if (empty($parentArr[$leafPart])) {
$parentArr[$leafPart] = $val;
}
}
return $returnArr;
}
/**
* @param $key
*
* @return array
* @throws \Exception
*/
private function getParametrs($key)
{
$segs = explode($this->delimiter, $key);
if (count($segs) < 2) {
throw new \Exception("Incorrect config key");
}
$name = array_shift($segs);
return [$name, $segs];
}
/**
* @param $key
* @param $value
*
* @return $this
* @throws \Exception
*/
public function set($key, $value)
{
list($name) = $this->getParametrs($key);
$this->change[] = $name;
$this->config = array_merge_recursive($this->config, $this->explodeTree([$key => $value]));
return $this;
}
/**
* @param $key
* @param null $default
*
* @return null
* @throws \Exception
*/
public function get($key, $default = null)
{
list($name, $segs) = $this->getParametrs($key);
if (!isset($this->config[$name])) {
$this->read($name);
}
$root = $this->config[$name];
foreach($segs as $name) {
if(isset($root[$name])) {
$root = $root[$name];
} else {
if($default == null){
throw new \Exception("Key '{$name}' in [ {$key} ] not found");
} else {
return $default;
}
}
}
return $root;
}
/**
* @param $name
*/
public function read($name)
{
$this->config[$name] = $this->reader->parse($this->repository.$name);
}
/**
* @param null $name
*
* @throws \Exception
*/
public function write($name = null)
{
if ($name == null) {
foreach ($this->change as $config) {
if (isset($this->config[$config])) {
$this->writer->dump($this->repository.$config, $config, $this->config[$config]);
} else {
throw new \Exception("Config '{$config}' not found");
}
}
$this->change = [];
} else {
$this->writer->dump($this->repository.$name, $name, $this->config[$name]);
}
}
}