JavaProperties.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Config\Reader;
  10. use Zend\Config\Exception;
  11. /**
  12. * Java-style properties config reader.
  13. */
  14. class JavaProperties implements ReaderInterface
  15. {
  16. /**
  17. * Directory of the Java-style properties file
  18. *
  19. * @var string
  20. */
  21. protected $directory;
  22. /**
  23. * fromFile(): defined by Reader interface.
  24. *
  25. * @see ReaderInterface::fromFile()
  26. * @param string $filename
  27. * @return array
  28. * @throws Exception\RuntimeException if the file cannot be read
  29. */
  30. public function fromFile($filename)
  31. {
  32. if (!is_file($filename) || !is_readable($filename)) {
  33. throw new Exception\RuntimeException(sprintf(
  34. "File '%s' doesn't exist or not readable",
  35. $filename
  36. ));
  37. }
  38. $this->directory = dirname($filename);
  39. $config = $this->parse(file_get_contents($filename));
  40. return $this->process($config);
  41. }
  42. /**
  43. * fromString(): defined by Reader interface.
  44. *
  45. * @see ReaderInterface::fromString()
  46. * @param string $string
  47. * @return array
  48. * @throws Exception\RuntimeException if an @include key is found
  49. */
  50. public function fromString($string)
  51. {
  52. if (empty($string)) {
  53. return array();
  54. }
  55. $this->directory = null;
  56. $config = $this->parse($string);
  57. return $this->process($config);
  58. }
  59. /**
  60. * Process the array for @include
  61. *
  62. * @param array $data
  63. * @return array
  64. * @throws Exception\RuntimeException if an @include key is found
  65. */
  66. protected function process(array $data)
  67. {
  68. foreach ($data as $key => $value) {
  69. if (trim($key) === '@include') {
  70. if ($this->directory === null) {
  71. throw new Exception\RuntimeException('Cannot process @include statement for a string');
  72. }
  73. $reader = clone $this;
  74. unset($data[$key]);
  75. $data = array_replace_recursive($data, $reader->fromFile($this->directory . '/' . $value));
  76. }
  77. }
  78. return $data;
  79. }
  80. /**
  81. * Parse Java-style properties string
  82. *
  83. * @todo Support use of the equals sign "key=value" as key-value delimiter
  84. * @todo Ignore whitespace that precedes text past the first line of multiline values
  85. *
  86. * @param string $string
  87. * @return array
  88. */
  89. protected function parse($string)
  90. {
  91. $result = array();
  92. $lines = explode("\n", $string);
  93. $key = "";
  94. $isWaitingOtherLine = false;
  95. foreach ($lines as $i => $line) {
  96. // Ignore empty lines and commented lines
  97. if (empty($line)
  98. || (!$isWaitingOtherLine && strpos($line, "#") === 0)
  99. || (!$isWaitingOtherLine && strpos($line, "!") === 0)) {
  100. continue;
  101. }
  102. // Add a new key-value pair or append value to a previous pair
  103. if (!$isWaitingOtherLine) {
  104. $key = substr($line, 0, strpos($line, ':'));
  105. $value = substr($line, strpos($line, ':') + 1, strlen($line));
  106. } else {
  107. $value .= $line;
  108. }
  109. // Check if ends with single '\' (indicating another line is expected)
  110. if (strrpos($value, "\\") === strlen($value) - strlen("\\")) {
  111. $value = substr($value, 0, strlen($value) - 1);
  112. $isWaitingOtherLine = true;
  113. } else {
  114. $isWaitingOtherLine = false;
  115. }
  116. $result[$key] = stripslashes($value);
  117. unset($lines[$i]);
  118. }
  119. return $result;
  120. }
  121. }