PageRenderTime 85ms CodeModel.GetById 29ms RepoModel.GetById 5ms app.codeStats 0ms

/vendor/phing/phing/classes/phing/tasks/ext/inifile/IniFileTask.php

https://gitlab.com/Isaki/le331.fr
PHP | 355 lines | 217 code | 21 blank | 117 comment | 49 complexity | 419d6d00946ec636c2680786af2f18b6 MD5 | raw file
  1. <?php
  2. /**
  3. * INI file modification task for Phing, the PHP build tool.
  4. *
  5. * Based on http://ant-contrib.sourceforge.net/tasks/tasks/inifile.html
  6. *
  7. * PHP version 5
  8. *
  9. * @category Tasks
  10. * @package phing.tasks.ext
  11. * @author Ken Guest <kguest@php.net>
  12. * @license LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
  13. * @link http://www.phing.info/
  14. */
  15. require_once 'IniFileSet.php';
  16. require_once 'IniFileRemove.php';
  17. require_once 'IniFileConfig.php';
  18. /**
  19. * InifileTask
  20. *
  21. * @category Tasks
  22. * @package phing.tasks.ext
  23. * @author Ken Guest <ken@linux.ie>
  24. * @license LGPL (see http://www.gnu.org/licenses/lgpl.html)
  25. * @link InifileTask.php
  26. */
  27. class InifileTask extends Task
  28. {
  29. /**
  30. * Source file
  31. *
  32. * @var string|null
  33. */
  34. protected $source = null;
  35. /**
  36. * Dest file
  37. *
  38. * @var string|null
  39. */
  40. protected $dest = null;
  41. /**
  42. * Whether to halt phing on error.
  43. *
  44. * @var bool
  45. */
  46. protected $haltonerror = false;
  47. /**
  48. * Sets
  49. *
  50. * @var array
  51. */
  52. protected $sets = array();
  53. /**
  54. * Removals
  55. *
  56. * @var array
  57. */
  58. protected $removals = array();
  59. /**
  60. * IniFileConfig instance
  61. *
  62. * @var IniFileConfig
  63. */
  64. protected $ini = null;
  65. /**
  66. * Taskname for logger
  67. * @var string
  68. */
  69. protected $taskName = 'IniFile';
  70. /**
  71. * Check file to be read from
  72. *
  73. * @param string $readFile Filename
  74. *
  75. * @return void
  76. */
  77. public function checkReadFile($readFile)
  78. {
  79. if (is_null($readFile)) {
  80. return false;
  81. }
  82. if (!file_exists($readFile)) {
  83. $msg = "$readFile does not exist.";
  84. if ($this->haltonerror) {
  85. throw new BuildException($msg);
  86. }
  87. $this->log($msg, Project::MSG_ERR);
  88. return false;
  89. }
  90. if (!is_readable($readFile)) {
  91. $msg = "$readFile is not readable.";
  92. if ($this->haltonerror) {
  93. throw new BuildException($msg);
  94. }
  95. $this->log($msg, Project::MSG_ERR);
  96. return false;
  97. }
  98. $this->ini->read($readFile);
  99. $this->log("Read from $readFile");
  100. return true;
  101. }
  102. /**
  103. * Check file to write to
  104. *
  105. * @param string $writeFile Filename
  106. *
  107. * @return void
  108. */
  109. public function checkWriteFile($writeFile)
  110. {
  111. if (file_exists($writeFile) && !is_writable($writeFile)) {
  112. $msg = "$writeFile is not writable";
  113. if ($this->haltonerror) {
  114. throw new BuildException($msg);
  115. }
  116. $this->log($msg, Project::MSG_ERR);
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * The main entry point method.
  123. *
  124. * @return void
  125. */
  126. public function main()
  127. {
  128. $this->ini = new IniFileConfig();
  129. $readFile = null;
  130. $writeFile = null;
  131. if (!is_null($this->source) && is_null($this->dest)) {
  132. $readFile = $this->source;
  133. } elseif (!is_null($this->dest) && is_null($this->source)) {
  134. $readFile = $this->dest;
  135. } else {
  136. $readFile = $this->source;
  137. }
  138. if (!is_null($this->dest)) {
  139. $writeFile = $this->dest;
  140. } elseif (!is_null($this->source)) {
  141. $writeFile = $this->source;
  142. } else {
  143. $writeFile = $this->dest;
  144. }
  145. if ($readFile === null && $writeFile === null) {
  146. $msg = "Neither source nor dest is set";
  147. if ($this->haltonerror) {
  148. throw new BuildException($msg);
  149. }
  150. $this->log($msg, Project::MSG_ERR);
  151. return;
  152. }
  153. if (!$this->checkReadFile($readFile)) {
  154. return;
  155. }
  156. if (!$this->checkWriteFile($writeFile)) {
  157. return;
  158. }
  159. $this->enumerateSets();
  160. $this->enumerateRemoves();
  161. try {
  162. $this->ini->write($writeFile);
  163. $this->log("Wrote to $writeFile");
  164. } catch (Exception $ex) {
  165. $msg = $ex->getMessage();
  166. if ($this->haltonerror) {
  167. throw new BuildException($msg);
  168. }
  169. $this->log($msg, Project::MSG_ERR);
  170. }
  171. }
  172. /**
  173. * Work through all Set commands.
  174. *
  175. * @return void
  176. */
  177. public function enumerateSets()
  178. {
  179. foreach ($this->sets as $set) {
  180. $value = $set->getValue();
  181. $key = $set->getProperty();
  182. $section = $set->getSection();
  183. $operation = $set->getOperation();
  184. if ($value !== null) {
  185. try {
  186. $this->ini->set($section, $key, $value);
  187. $this->log(
  188. "[$section] $key set to $value",
  189. Project::MSG_DEBUG
  190. );
  191. } catch (Exception $ex) {
  192. $this->log(
  193. "Error setting value for section '" . $section .
  194. "', key '" . $key ."'"
  195. );
  196. $this->log($ex->getMessage(), Project::MSG_DEBUG);
  197. }
  198. } elseif ($operation !== null) {
  199. $v = $this->ini->get($section, $key);
  200. // value might be wrapped in quotes with a semicolon at the end
  201. if (!is_numeric($v)) {
  202. if (preg_match('/^"(\d*)";?$/', $v, $match)) {
  203. $v = $match[1];
  204. } elseif (preg_match("/^'(\d*)';?$/", $v, $match)) {
  205. $v = $match[1];
  206. } else {
  207. $this->log(
  208. "Value $v is not numeric. Skipping $operation operation."
  209. );
  210. continue;
  211. }
  212. }
  213. if ($operation == '+') {
  214. ++$v;
  215. } elseif ($operation == '-') {
  216. --$v;
  217. } else {
  218. if (($operation != '-') && ($operation != '+')) {
  219. $msg = "Unrecognised operation $operation";
  220. if ($this->haltonerror) {
  221. throw new BuildException($msg);
  222. }
  223. $this->log($msg, Project::MSG_ERR);
  224. }
  225. }
  226. try {
  227. $this->ini->set($section, $key, $v);
  228. $this->log(
  229. "[$section] $key set to $v",
  230. Project::MSG_DEBUG
  231. );
  232. } catch (Exception $ex) {
  233. $this->log(
  234. "Error setting value for section '" . $section .
  235. "', key '" . $key ."'"
  236. );
  237. $this->log($ex->getMessage(), Project::MSG_DEBUG);
  238. }
  239. } else {
  240. $this->log(
  241. "Set: value and operation are both not set",
  242. Project::MSG_ERR
  243. );
  244. }
  245. }
  246. }
  247. /**
  248. * Work through all Remove commands.
  249. *
  250. * @return void
  251. */
  252. public function enumerateRemoves()
  253. {
  254. foreach ($this->removals as $remove) {
  255. $key = $remove->getProperty();
  256. $section = $remove->getSection();
  257. if ($section == '') {
  258. $this->log(
  259. "Remove: section must be set",
  260. Project::MSG_ERR
  261. );
  262. continue;
  263. }
  264. $this->ini->remove($section, $key);
  265. if (($section != '') && ($key != '')) {
  266. $this->log(
  267. "$key in section [$section] has been removed.",
  268. Project::MSG_DEBUG
  269. );
  270. } elseif (($section != '') && ($key == '')) {
  271. $this->log("[$section] has been removed.", Project::MSG_DEBUG);
  272. }
  273. }
  274. }
  275. /**
  276. * Set Source property
  277. *
  278. * @param string $source Name of originating ini file to parse
  279. *
  280. * @return void
  281. */
  282. public function setSource($source)
  283. {
  284. $this->source = $source;
  285. }
  286. /**
  287. * Set Dest property
  288. *
  289. * @param string $dest Destination filename to write ini contents to.
  290. *
  291. * @return void
  292. */
  293. public function setDest($dest)
  294. {
  295. $this->dest = $dest;
  296. }
  297. /**
  298. * Set haltonerror attribute.
  299. *
  300. * @param string $halt 'yes', or '1' to halt.
  301. *
  302. * @return void
  303. */
  304. public function setHaltonerror($halt)
  305. {
  306. $doHalt = false;
  307. if (strtolower($halt) == 'yes' || $halt == 1) {
  308. $doHalt = true;
  309. }
  310. $this->haltonerror = $doHalt;
  311. }
  312. /**
  313. * Create a Set method
  314. *
  315. * @return IniFileSet
  316. */
  317. public function createSet()
  318. {
  319. $set = new IniFileSet();
  320. $this->sets[] = $set;
  321. return $set;
  322. }
  323. /**
  324. * Create a Remove method
  325. *
  326. * @return IniFileRemove
  327. */
  328. public function createRemove()
  329. {
  330. $remove = new IniFileRemove();
  331. $this->removals[] = $remove;
  332. return $remove;
  333. }
  334. }