1: <?php
2: /**
3: * @author Jefferson González
4: * @license MIT
5: * @link http://github.com/peg-org/peg-custom Source code.
6: */
7:
8: namespace Peg\Custom\Config;
9:
10: /**
11: * Ini implementation to manage configuration files.
12: */
13: class INI extends \Peg\Custom\Config\Base
14: {
15:
16: /**
17: * Loads all configuration options of a given file and creates it if does
18: * not exists.
19: * @param string $directory
20: * @param string $configuration_file
21: */
22: public function Load($directory, $configuration_file)
23: {
24: $this->preferences = array();
25:
26: $this->directory = $directory;
27:
28: $this->file = $this->directory . "/$configuration_file";
29:
30: if(file_exists($this->file))
31: {
32: $this->preferences = parse_ini_file($this->file, true);
33: }
34: }
35:
36: /**
37: * Writes a configuration file using the settings array.
38: */
39: public function Write()
40: {
41: $content = "";
42:
43: foreach($this->preferences as $key => $data)
44: {
45: if(is_array($data))
46: {
47: $is_section = true;
48:
49: foreach($data as $dataKey => $dataValues)
50: {
51: if(is_long($dataKey))
52: {
53: $is_section = false;
54: break;
55: }
56: }
57:
58: $content .= "\n";
59:
60: //Write global array value
61: if(!$is_section)
62: {
63: foreach($data as $dataKey => $dataValue)
64: {
65: $content .= $key . '[] = "' . $dataValue . '"' . "\n";
66: }
67: }
68:
69: //Write section
70: else
71: {
72: $content .= "[" . $key . "]\n";
73:
74: foreach($data as $dataKey => $dataValue)
75: {
76: if(is_array($dataValue))
77: {
78: foreach($dataValue as $dataInnerValue)
79: {
80: $content .= $dataKey . '[] = "' . $dataInnerValue . '"' . "\n";
81: }
82: }
83: else
84: {
85: $content .= $dataKey . ' = "' . $dataValue . '"' . "\n";
86: }
87: }
88: }
89:
90: $content .= "\n";
91: }
92:
93: //Write global value
94: else
95: {
96: $content .= $key . ' = "' . $data . '"' . "\n";
97: }
98: }
99:
100: file_put_contents($this->file, $content);
101: }
102:
103: }