PageRenderTime 44ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/buildscripts/phing/classes/phing/filters/util/IniFileTokenReader.php

http://prado3.googlecode.com/
PHP | 97 lines | 45 code | 11 blank | 41 comment | 7 complexity | d65d0f9e43f3a63817ad5299e387ae94 MD5 | raw file
Possible License(s): Apache-2.0, IPL-1.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * $Id: e709765b4c0c1be330183f462ab527fa8354b555 $
  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/types/TokenReader.php';
  22. include_once 'phing/system/io/IOException.php';
  23. include_once 'phing/filters/ReplaceTokens.php'; // For class Token
  24. /**
  25. * Class that allows reading tokens from INI files.
  26. *
  27. * @author Manuel Holtgewe
  28. * @version $Id$
  29. * @package phing.filters.util
  30. */
  31. class IniFileTokenReader extends TokenReader {
  32. /**
  33. * Holds the path to the INI file that is to be read.
  34. * @var object Reference to a PhingFile Object representing
  35. * the path to the INI file.
  36. */
  37. private $file = null;
  38. /**
  39. * @var string Sets the section to load from the INI file.
  40. * if omitted, all sections are loaded.
  41. */
  42. private $section = null;
  43. /**
  44. * Reads the next token from the INI file
  45. *
  46. * @throws IOException On error
  47. * @return Token
  48. */
  49. function readToken() {
  50. if ($this->file === null) {
  51. throw new BuildException("No File set for IniFileTokenReader");
  52. }
  53. static $tokens = null;
  54. if ($tokens === null) {
  55. $tokens = array();
  56. $arr = parse_ini_file($this->file->getAbsolutePath(), true);
  57. if ($this->section === null) {
  58. foreach ($arr as $sec_name => $values) {
  59. foreach($arr[$sec_name] as $key => $value) {
  60. $tok = new Token;
  61. $tok->setKey($key);
  62. $tok->setValue($value);
  63. $tokens[] = $tok;
  64. }
  65. }
  66. } else if (isset($arr[$this->section])) {
  67. foreach ($arr[$this->section] as $key => $value) {
  68. $tok = new Token;
  69. $tok->setKey($key);
  70. $tok->setValue($value);
  71. $tokens[] = $tok;
  72. }
  73. }
  74. }
  75. if (count($tokens) > 0) {
  76. return array_pop($tokens);
  77. } else
  78. return null;
  79. }
  80. function setFile(PhingFile $file) {
  81. $this->file = $file;
  82. }
  83. function setSection($str) {
  84. $this->section = (string) $str;
  85. }
  86. }