PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phing/phing/classes/phing/system/util/Properties.php

https://gitlab.com/Isaki/le331.fr
PHP | 378 lines | 181 code | 41 blank | 156 comment | 20 complexity | 4300cbc93448efb3532fc2d672baf1fd MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: 67e878eee0cfcff63ea6621ad0e475e76eb6d5a6 $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/system/io/PhingFile.php';
  22. include_once 'phing/system/io/FileWriter.php';
  23. /**
  24. * Convenience class for reading and writing property files.
  25. *
  26. * FIXME
  27. * - Add support for arrays (separated by ',')
  28. *
  29. * @package phing.system.util
  30. * @version $Id: 67e878eee0cfcff63ea6621ad0e475e76eb6d5a6 $
  31. */
  32. class Properties
  33. {
  34. private $properties = array();
  35. /**
  36. * @var PhingFile
  37. */
  38. private $file = null;
  39. /**
  40. * Constructor
  41. *
  42. * @param array $properties
  43. */
  44. public function __construct($properties = null)
  45. {
  46. if (is_array($properties)) {
  47. foreach ($properties as $key => $value) {
  48. $this->setProperty($key, $value);
  49. }
  50. }
  51. }
  52. /**
  53. * Load properties from a file.
  54. *
  55. * @param PhingFile $file
  56. * @return void
  57. * @throws IOException - if unable to read file.
  58. */
  59. public function load(PhingFile $file)
  60. {
  61. if ($file->canRead()) {
  62. $this->parse($file->getPath(), false);
  63. $this->file = $file;
  64. } else {
  65. throw new IOException("Can not read file " . $file->getPath());
  66. }
  67. }
  68. /**
  69. * Replaces parse_ini_file() or better_parse_ini_file().
  70. * Saves a step since we don't have to parse and then check return value
  71. * before throwing an error or setting class properties.
  72. *
  73. * @param string $filePath
  74. * @throws IOException
  75. * @internal param bool $processSections Whether to honor [SectionName] sections in INI file.
  76. * @return array Properties loaded from file (no prop replacements done yet).
  77. */
  78. protected function parse($filePath)
  79. {
  80. // load() already made sure that file is readable
  81. // but we'll double check that when reading the file into
  82. // an array
  83. if (($lines = @file($filePath)) === false) {
  84. throw new IOException("Unable to parse contents of $filePath");
  85. }
  86. // concatenate lines ending with backslash
  87. $linesCount = count($lines);
  88. for ($i = 0; $i < $linesCount; $i++) {
  89. if (substr($lines[$i], -2, 1) === '\\') {
  90. $lines[$i + 1] = substr($lines[$i], 0, -2) . ltrim($lines[$i + 1]);
  91. $lines[$i] = '';
  92. }
  93. }
  94. $this->properties = array();
  95. $sec_name = "";
  96. foreach ($lines as $line) {
  97. // strip comments and leading/trailing spaces
  98. $line = trim(preg_replace("/\s+[;#]\s.+$/", "", $line));
  99. if (empty($line) || $line[0] == ';' || $line[0] == '#') {
  100. continue;
  101. }
  102. $pos = strpos($line, '=');
  103. $property = trim(substr($line, 0, $pos));
  104. $value = trim(substr($line, $pos + 1));
  105. $this->properties[$property] = $this->inVal($value);
  106. } // for each line
  107. }
  108. /**
  109. * Process values when being read in from properties file.
  110. * does things like convert "true" => true
  111. * @param string $val Trimmed value.
  112. * @return mixed The new property value (may be boolean, etc.)
  113. */
  114. protected function inVal($val)
  115. {
  116. if ($val === "true") {
  117. $val = true;
  118. } elseif ($val === "false") {
  119. $val = false;
  120. }
  121. return $val;
  122. }
  123. /**
  124. * Process values when being written out to properties file.
  125. * does things like convert true => "true"
  126. * @param mixed $val The property value (may be boolean, etc.)
  127. * @return string
  128. */
  129. protected function outVal($val)
  130. {
  131. if ($val === true) {
  132. $val = "true";
  133. } elseif ($val === false) {
  134. $val = "false";
  135. }
  136. return $val;
  137. }
  138. /**
  139. * Create string representation that can be written to file and would be loadable using load() method.
  140. *
  141. * Essentially this function creates a string representation of properties that is ready to
  142. * write back out to a properties file. This is used by store() method.
  143. *
  144. * @return string
  145. */
  146. public function toString()
  147. {
  148. $buf = "";
  149. foreach ($this->properties as $key => $item) {
  150. $buf .= $key . "=" . $this->outVal($item) . PHP_EOL;
  151. }
  152. return $buf;
  153. }
  154. /**
  155. * Stores current properties to specified file.
  156. *
  157. * @param PhingFile $file File to create/overwrite with properties.
  158. * @param string $header Header text that will be placed (within comments) at the top of properties file.
  159. * @return void
  160. * @throws IOException - on error writing properties file.
  161. */
  162. public function store(PhingFile $file = null, $header = null)
  163. {
  164. if ($file == null) {
  165. $file = $this->file;
  166. }
  167. if ($file == null) {
  168. throw new IOException("Unable to write to empty filename");
  169. }
  170. // stores the properties in this object in the file denoted
  171. // if file is not given and the properties were loaded from a
  172. // file prior, this method stores them in the file used by load()
  173. try {
  174. $fw = new FileWriter($file);
  175. if ($header !== null) {
  176. $fw->write("# " . $header . PHP_EOL);
  177. }
  178. $fw->write($this->toString());
  179. $fw->close();
  180. } catch (IOException $e) {
  181. throw new IOException("Error writing property file: " . $e->getMessage());
  182. }
  183. }
  184. public function storeOutputStream(OutputStream $os, $comments)
  185. {
  186. $this->_storeOutputStream(new BufferedWriter(new OutputStreamWriter($os)), $comments);
  187. }
  188. private function _storeOutputStream(BufferedWriter $bw, $comments)
  189. {
  190. if ($comments != null) {
  191. self::writeComments($bw, $comments);
  192. }
  193. $bw->write("#" . gmdate('D, d M Y H:i:s', time()) . ' GMT');
  194. $bw->newLine();
  195. foreach ($this->getProperties() as $key => $value) {
  196. $bw->write($key . "=" . $value);
  197. $bw->newLine();
  198. }
  199. $bw->flush();
  200. }
  201. private static function writeComments(BufferedWriter $bw, $comments)
  202. {
  203. $rows = explode("\n", $comments);
  204. $bw->write("#" . PHP_EOL);
  205. foreach ($rows as $row) {
  206. $bw->write(sprintf("#%s%s", trim($row), PHP_EOL));
  207. }
  208. $bw->write("#");
  209. $bw->newLine();
  210. }
  211. /**
  212. * Returns copy of internal properties hash.
  213. * Mostly for performance reasons, property hashes are often
  214. * preferable to passing around objects.
  215. *
  216. * @return array
  217. */
  218. public function getProperties()
  219. {
  220. return $this->properties;
  221. }
  222. /**
  223. * Get value for specified property.
  224. * This is the same as get() method.
  225. *
  226. * @param string $prop The property name (key).
  227. * @return mixed
  228. * @see get()
  229. */
  230. public function getProperty($prop)
  231. {
  232. if (!isset($this->properties[$prop])) {
  233. return null;
  234. }
  235. return $this->properties[$prop];
  236. }
  237. /**
  238. * Get value for specified property.
  239. * This function exists to provide a hashtable-like interface for
  240. * properties.
  241. *
  242. * @param string $prop The property name (key).
  243. * @return mixed
  244. * @see getProperty()
  245. */
  246. public function get($prop)
  247. {
  248. if (!isset($this->properties[$prop])) {
  249. return null;
  250. }
  251. return $this->properties[$prop];
  252. }
  253. /**
  254. * Set the value for a property.
  255. *
  256. * @param string $key
  257. * @param mixed $value
  258. * @return mixed Old property value or NULL if none was set.
  259. */
  260. public function setProperty($key, $value)
  261. {
  262. $oldValue = null;
  263. if (isset($this->properties[$key])) {
  264. $oldValue = $this->properties[$key];
  265. }
  266. $this->properties[$key] = $value;
  267. return $oldValue;
  268. }
  269. /**
  270. * Set the value for a property.
  271. * This function exists to provide hashtable-lie
  272. * interface for properties.
  273. *
  274. * @param string $key
  275. * @param mixed $value
  276. * @return mixed
  277. */
  278. public function put($key, $value)
  279. {
  280. return $this->setProperty($key, $value);
  281. }
  282. /**
  283. * Appends a value to a property if it already exists with a delimiter
  284. *
  285. * If the property does not, it just adds it.
  286. *
  287. * @param string $key
  288. * @param mixed $value
  289. * @param string $delimiter
  290. */
  291. public function append($key, $value, $delimiter = ',')
  292. {
  293. $newValue = $value;
  294. if (isset($this->properties[$key]) && !empty($this->properties[$key])) {
  295. $newValue = $this->properties[$key] . $delimiter . $value;
  296. }
  297. $this->properties[$key] = $newValue;
  298. }
  299. /**
  300. * Same as keys() function, returns an array of property names.
  301. * @return array
  302. */
  303. public function propertyNames()
  304. {
  305. return $this->keys();
  306. }
  307. /**
  308. * Whether loaded properties array contains specified property name.
  309. * @param $key
  310. * @return boolean
  311. */
  312. public function containsKey($key)
  313. {
  314. return isset($this->properties[$key]);
  315. }
  316. /**
  317. * Returns properties keys.
  318. * Use this for foreach () {} iterations, as this is
  319. * faster than looping through property values.
  320. * @return array
  321. */
  322. public function keys()
  323. {
  324. return array_keys($this->properties);
  325. }
  326. /**
  327. * Whether properties list is empty.
  328. * @return boolean
  329. */
  330. public function isEmpty()
  331. {
  332. return empty($this->properties);
  333. }
  334. }