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;
9:
10: /**
11: * For managing the configuration options of a valid peg
12: * managed extension directory.
13: */
14: class Settings
15: {
16:
17: /**
18: * The configurations object used to manipulate the settings.
19: * @var \Peg\Custom\Config\Base
20: */
21: private static $backend;
22:
23: // Disable constructor
24: private function __construct(){}
25:
26: /**
27: * Sets the back-end or implementation that will be used to manage
28: * the retreival and storage of settings.
29: * @param \Peg\Custom\Config\Base $backend
30: */
31: public static function SetBackEnd(\Peg\Custom\Config\Base $backend)
32: {
33: self::$backend = $backend;
34: }
35:
36: /**
37: * Loads the configuration file on the given directory.
38: * @param string $directory
39: * @param string $file
40: */
41: public static function Load($directory, $file)
42: {
43: self::CheckBackend();
44:
45: self::$backend->Load($directory, $file);
46: }
47:
48: /**
49: * Gets the value of a specific option.
50: * @param string $option
51: * @return string|bool
52: */
53: public static function Get($option)
54: {
55: self::CheckBackend();
56:
57: return self::$backend->Get($option);
58: }
59:
60: /**
61: * Gets the value of a specific option inside a group.
62: * @param string $group Eg: parser
63: * @param string $option Eg: input_format
64: * @return string|bool
65: */
66: public static function GetGroupValue($group, $option)
67: {
68: self::CheckBackend();
69:
70: return self::$backend->GetGroupValue($group, $option);
71: }
72:
73: /**
74: * Get a comma separated list of authors.
75: * @return string
76: */
77: public static function GetAuthors()
78: {
79: self::CheckBackend();
80:
81: return self::$backend->Get("authors");
82: }
83:
84: /**
85: * Get a comma separated list of contributors.
86: * @return string
87: */
88: public static function GetContributors()
89: {
90: self::CheckBackend();
91:
92: return self::$backend->Get("contributors");
93: }
94:
95: /**
96: * Get the current extension name.
97: * @return string
98: */
99: public static function GetExtensionName()
100: {
101: self::CheckBackend();
102:
103: $name = self::$backend->Get("name");
104:
105: if(strlen($name) <= 0)
106: {
107: $dir_parts = explode(
108: "/",
109: str_replace("\\", "/", self::$backend->directory)
110: );
111:
112: $name = $dir_parts[count($dir_parts) - 1];
113: }
114:
115: return $name;
116: }
117:
118: /**
119: * Get the current extension version.
120: * @return string
121: */
122: public static function GetVersion()
123: {
124: self::CheckBackend();
125:
126: return self::$backend->Get("version");
127: }
128:
129: /**
130: * Modify or add a new option.
131: * @param string $option Option to add or modify.
132: * @param string $value Value of the option.
133: */
134: public static function Set($option, $value)
135: {
136: self::CheckBackend();
137:
138: self::$backend->Set($option, $value);
139: }
140:
141: /**
142: * Modify or add a new group with an option.
143: * @param string $group Name of group to modify or create.
144: * @param string $option Name of option to add or modify in the group.
145: * @param string $value Value of the option.
146: */
147: public static function SetGroupValue($group, $option, $value)
148: {
149: self::CheckBackend();
150:
151: self::$backend->SetGroupValue($group, $option, $value);
152: }
153:
154: /**
155: * Set the authors of the extension. This should be a comma
156: * seperated list with the names of the authors.
157: * @param string $authors
158: */
159: public static function SetAuthors($authors)
160: {
161: self::CheckBackend();
162:
163: $authors = trim($authors);
164: $authors = trim($authors, ",");
165:
166: self::$backend->Set("authors", $authors);
167: }
168:
169: /**
170: * Set the contributors of the extension. This should be a comma
171: * seperated list with the names of the contributors.
172: * @param string $contributors
173: */
174: public static function SetContributors($contributors)
175: {
176: self::CheckBackend();
177:
178: $contributors = trim($contributors);
179: $contributors = trim($contributors, ",");
180:
181: self::$backend->Set("contributors", $contributors);
182: }
183:
184: /**
185: * Set the extension name, which is used in some important parts of the
186: * code generator.
187: * @param string $name
188: */
189: public static function SetExtensionName($name)
190: {
191: self::CheckBackend();
192:
193: $name = trim($name);
194:
195: self::$backend->Set("name", $name);
196: }
197:
198: /**
199: * Sets the version of the extension which is used in some important
200: * parts of the code generator.
201: * @param string $number
202: */
203: public static function SetVersion($number)
204: {
205: self::CheckBackend();
206:
207: $number = trim($number);
208:
209: self::$backend->Set("version", $number);
210: }
211:
212: /**
213: * Helper called by all other methods to make sure that a backend
214: * was set before calling them.
215: * @throws \Exception
216: */
217: private static function CheckBackend()
218: {
219: if(!is_object(self::$backend))
220: throw new \Exception(
221: t("The back-end to manage the settings is not set.")
222: );
223: }
224:
225: }