PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-config/src/Reader/JavaProperties.php

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