1 | <?php |
||
50 | class Properties extends HashMap implements PropertiesInterface |
||
51 | { |
||
52 | |||
53 | /** |
||
54 | * This member is TRUE if the sections should be parsed, else FALSE |
||
55 | * |
||
56 | * @var boolean |
||
57 | */ |
||
58 | protected $sections = false; |
||
59 | |||
60 | /** |
||
61 | * The default constructor. |
||
62 | * |
||
63 | * @param \AppserverIo\Properties\Properties $defaults The properties we want want to use for initialization |
||
64 | */ |
||
65 | 11 | public function __construct(Properties $defaults = null) |
|
75 | |||
76 | /** |
||
77 | * Factory method. |
||
78 | * |
||
79 | * @param \AppserverIo\Properties\Properties $defaults Default properties to initialize the new ones with |
||
80 | * |
||
81 | * @return \AppserverIo\Properties\Properties The initialized properties |
||
82 | */ |
||
83 | 8 | public static function create(Properties $defaults = null) |
|
87 | |||
88 | /** |
||
89 | * Reads a property list (key and element pairs) from the passed file. |
||
90 | * |
||
91 | * @param string $file The path and the name of the file to load the properties from |
||
92 | * @param boolean $sections Has to be TRUE to parse the sections |
||
93 | * @param integer $scannerMode Can either be INI_SCANNER_NORMAL (default) or INI_SCANNER_RAW, if INI_SCANNER_RAW is supplied, then option values will not be parsed. |
||
94 | * |
||
95 | * @return \AppserverIo\Properties\Properties The initialized properties |
||
96 | * @throws \AppserverIo\Properties\PropertyFileParseException Is thrown if an error occurs while parsing the property file |
||
97 | * @throws \AppserverIo\Properties\PropertyFileNotFoundException Is thrown if the property file passed as parameter does not exist in the include path |
||
98 | * @link http://php.net/parse_ini_string |
||
99 | */ |
||
100 | 7 | public function load($file, $sections = false, $scannerMode = INI_SCANNER_RAW) |
|
101 | { |
||
102 | // try to load the file content |
||
103 | 7 | $content = @file_get_contents($file, FILE_USE_INCLUDE_PATH); |
|
104 | // check if file has successfully been loaded |
||
105 | 7 | if (! $content) { |
|
106 | // throw an exception if the file can not be found in the include path |
||
107 | 1 | throw new PropertyFileNotFoundException(sprintf('File %s not found in include path', $file)); |
|
108 | } |
||
109 | // parse the file content |
||
110 | 6 | $properties = parse_ini_string($content, $this->sections = $sections, $scannerMode); |
|
111 | // check if property file was parsed successfully |
||
112 | 6 | if ($properties == false) { |
|
113 | // throw an exception if an error occurs |
||
114 | 1 | throw new PropertyFileParseException(sprintf('File %s can not be parsed as property file', $file)); |
|
115 | } |
||
116 | // set the found values |
||
117 | 5 | $this->items = $properties; |
|
118 | // return the initialized properties |
||
119 | 5 | return $this; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Stores the properties in the property file. This method is NOT using the include path for storing the file. |
||
124 | * |
||
125 | * @param string $file The path and the name of the file to store the properties to |
||
126 | * |
||
127 | * @return void |
||
128 | * |
||
129 | * @throws \AppserverIo\Properties\PropertyFileStoreException Is thrown if the file could not be written |
||
130 | * @todo Actually only properties without sections will be stored, if a section is specified, then it will be ignored |
||
|
|||
131 | */ |
||
132 | 2 | public function store($file) |
|
150 | |||
151 | /** |
||
152 | * Searches for the property with the specified key in this property list. |
||
153 | * |
||
154 | * @param string $key Holds the key of the value to return |
||
155 | * @param string $section Holds a string with the section name to return the key for (only matters if sections is set to TRUE) |
||
156 | * |
||
157 | * @return string Holds the value of the passed key |
||
158 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key, or, if sections are TRUE, the passed section is NULL |
||
159 | */ |
||
160 | 6 | public function getProperty($key, $section = null) |
|
196 | |||
197 | /** |
||
198 | * Calls the HashMap method add. |
||
199 | * |
||
200 | * @param string $key Holds the key of the value to return |
||
201 | * @param mixed $value Holds the value to add to the properties |
||
202 | * @param string $section Holds a string with the section name to return the key for (only matters if sections is set to TRUE) |
||
203 | * |
||
204 | * @return void |
||
205 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key, or, if sections are TRUE, the passed section is NULL |
||
206 | */ |
||
207 | 5 | public function setProperty($key, $value, $section = null) |
|
233 | |||
234 | /** |
||
235 | * Returns all properties with their keys as a string. |
||
236 | * |
||
237 | * @return string String with all key -> properties pairs |
||
238 | */ |
||
239 | public function __toString() |
||
261 | |||
262 | /** |
||
263 | * Returns all properties with their keys as a String. |
||
264 | * |
||
265 | * @return \AppserverIo\Lang\String String with all key -> properties pairs |
||
266 | */ |
||
267 | public function toString() |
||
271 | |||
272 | /** |
||
273 | * Merges the passed properties into the actual instance. If override |
||
274 | * flag is set to TRUE, existing properties will be overwritten. |
||
275 | * |
||
276 | * @param \AppserverIo\Properties\PropertiesInterface $properties The properties to merge |
||
277 | * @param boolean $override TRUE if existing properties have to be overwritten, else FALSE |
||
278 | * |
||
279 | * @return void |
||
280 | */ |
||
281 | 2 | public function mergeProperties(PropertiesInterface $properties, $override = false) |
|
282 | { |
||
283 | // iterate over the keys of the passed properties and add thm, or replace existing ones |
||
284 | 2 | foreach ($properties as $key => $value) { |
|
285 | 2 | if ($this->exists($key) === false || ($this->exists($key) === true && $override === true)) { |
|
286 | 2 | $this->setProperty($key, $value); |
|
287 | 2 | } |
|
288 | 2 | } |
|
289 | 2 | } |
|
290 | |||
291 | /** |
||
292 | * Returns all key values as an array. |
||
293 | * |
||
294 | * @return array The keys as array values |
||
295 | */ |
||
296 | 3 | public function getKeys() |
|
312 | } |
||
313 |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.