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: * Base structure for managing configuration files. Every configuration manager
12: * should implement this class.
13: */
14: abstract class Base
15: {
16:
17: /**
18: * All preferences stored on a configuration file.
19: *
20: * Format of this array for single options:
21: * $settings["option"] = "value"
22: *
23: * Format of this array for a group of options:
24: * $settings["group"] = array(
25: * "option" => "value",
26: * "option2" => "value2"
27: * )
28: * @var array
29: */
30: protected $preferences;
31:
32: /**
33: * Directory of configuration file.
34: * @var string
35: */
36: public $directory;
37:
38: /**
39: * Full path of configuration file.
40: * @var string
41: */
42: public $file;
43:
44: /**
45: * Loads all configuration options of a given file and creates it if does
46: * not exists.
47: * @param string $directory
48: * @param string $configuration_file
49: */
50: abstract public function Load($directory, $configuration_file);
51:
52: /**
53: * Writes a configuration file using the settings array.
54: */
55: abstract public function Write();
56:
57: /**
58: * Get an option value.
59: * @param string $option
60: * @return boolean
61: */
62: public function Get($option)
63: {
64: if(isset($this->preferences[$option]))
65: {
66: return $this->preferences[$option];
67: }
68:
69: return false;
70: }
71:
72: /**
73: * Get all preferences.
74: * @return array
75: */
76: public function GetAll()
77: {
78: return $this->preferences;
79: }
80:
81: /**
82: * Get the value of an option on a specific configuration group.
83: * @param string $group
84: * @param string $option
85: * @return boolean|string
86: */
87: public function GetGroupValue($group, $option)
88: {
89: if(isset($this->preferences[$group]))
90: {
91: if(isset($this->preferences[$group][$option]))
92: {
93: return $this->preferences[$group][$option];
94: }
95: }
96:
97: return false;
98: }
99:
100: /**
101: * Modifies or adds an option value and writes it to the configuration
102: * file immediately.
103: * @param type $option
104: * @param type $value
105: */
106: public function Set($option, $value)
107: {
108: $this->preferences[$option] = $value;
109:
110: $this->Write();
111: }
112:
113: /**
114: * Edits or creates a new group and option and writes it to the
115: * configuration file immediately.
116: * @param string $group
117: * @param string $option
118: * @param string $value
119: */
120: public function SetGroupValue($group, $option, $value)
121: {
122: $this->preferences[$group][$option] = $value;
123:
124: $this->Write();
125: }
126:
127: }