1: <?php
2:
3: 4: 5: 6: 7: 8:
9:
10: namespace PhpOptions\Types;
11:
12: require_once __DIR__ . '/FileType.php';
13:
14: 15: 16: 17: 18:
19: class InifileType extends FileType
20: {
21:
22: 23: 24: 25: 26:
27: private $sections = TRUE;
28:
29: 30: 31: 32: 33:
34: private $delimiters = ';|';
35:
36:
37: 38: 39: 40: 41: 42:
43: public function __construct($settings = array())
44: {
45: parent::__construct($settings);
46: if ($this->settingsHasFlag('notSections', $settings))
47: {
48: $this->sections = FALSE;
49: }
50: }
51:
52:
53: 54: 55: 56: 57: 58: 59:
60: protected function useFilter($value)
61: {
62: $file = parent::useFilter($value);
63: $content = parse_ini_file($file, $this->sections);
64: if ($this->sections)
65: {
66: $content = $this->mergeParent($content);
67: $contentTmp = array();
68: foreach ($content as $section => $values)
69: {
70: $contentTmp[$section] = $this->makeSubArray($values);
71: }
72: $content = $contentTmp;
73: }
74: else
75: {
76: $content = $this->makeSubArray($content);
77: }
78: return $content;
79: }
80:
81:
82: 83: 84: 85: 86: 87: 88:
89: private function mergeParent($content)
90: {
91: $contentTmp = array();
92: foreach ($content as $section => $values)
93: {
94: $position = strpos($section, '<');
95: if ($position !== FALSE)
96: {
97: $child = trim(substr($section, 0, $position - 1));
98: $parent = trim(substr($section, $position + 1));
99:
100: if ($child && $parent && isset($contentTmp[$parent]))
101: {
102: $section = $child;
103: $values = array_merge($contentTmp[$parent], $values);
104: }
105: }
106: $contentTmp[$section] = $values;
107: }
108:
109: return $contentTmp;
110: }
111:
112:
113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123:
124: private function makeSubArray($content)
125: {
126: $contentNew = array();
127: foreach ($content as $key => $value)
128: {
129: $keysNew = explode('.', $key);
130: $contentNewPointer = &$contentNew;
131: foreach ($keysNew as $keyNew)
132: {
133: if (!isset($contentNewPointer[$keyNew]))
134: {
135: $contentNewPointer[$keyNew] = array();
136: }
137: $contentNewPointer = &$contentNewPointer[$keyNew];
138: }
139:
140:
141: $value = preg_replace('/[' . $this->delimiters . ']+/', $this->delimiters[0], $value);
142: $value = explode($this->delimiters[0], $value);
143: $contentNewPointer = (count($value) == 1) ? $value[0] : $value;
144: }
145:
146: return $contentNew;
147: }
148:
149:
150: }
151: