Xml.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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-2013 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 XMLReader;
  11. use Zend\Config\Exception;
  12. /**
  13. * XML config reader.
  14. */
  15. class Xml implements ReaderInterface
  16. {
  17. /**
  18. * XML Reader instance.
  19. *
  20. * @var XMLReader
  21. */
  22. protected $reader;
  23. /**
  24. * Directory of the file to process.
  25. *
  26. * @var string
  27. */
  28. protected $directory;
  29. /**
  30. * Nodes to handle as plain text.
  31. *
  32. * @var array
  33. */
  34. protected $textNodes = array(
  35. XMLReader::TEXT,
  36. XMLReader::CDATA,
  37. XMLReader::WHITESPACE,
  38. XMLReader::SIGNIFICANT_WHITESPACE
  39. );
  40. /**
  41. * fromFile(): defined by Reader interface.
  42. *
  43. * @see ReaderInterface::fromFile()
  44. * @param string $filename
  45. * @return array
  46. * @throws Exception\RuntimeException
  47. */
  48. public function fromFile($filename)
  49. {
  50. if (!is_file($filename) || !is_readable($filename)) {
  51. throw new Exception\RuntimeException(sprintf(
  52. "File '%s' doesn't exist or not readable",
  53. $filename
  54. ));
  55. }
  56. $this->reader = new XMLReader();
  57. $this->reader->open($filename, null, LIBXML_XINCLUDE);
  58. $this->directory = dirname($filename);
  59. set_error_handler(
  60. function ($error, $message = '', $file = '', $line = 0) use ($filename) {
  61. throw new Exception\RuntimeException(sprintf(
  62. 'Error reading XML file "%s": %s',
  63. $filename, $message
  64. ), $error);
  65. }, E_WARNING
  66. );
  67. $return = $this->process();
  68. restore_error_handler();
  69. return $return;
  70. }
  71. /**
  72. * fromString(): defined by Reader interface.
  73. *
  74. * @see ReaderInterface::fromString()
  75. * @param string $string
  76. * @return array|bool
  77. * @throws Exception\RuntimeException
  78. */
  79. public function fromString($string)
  80. {
  81. if (empty($string)) {
  82. return array();
  83. }
  84. $this->reader = new XMLReader();
  85. $this->reader->xml($string, null, LIBXML_XINCLUDE);
  86. $this->directory = null;
  87. set_error_handler(
  88. function ($error, $message = '', $file = '', $line = 0) {
  89. throw new Exception\RuntimeException(sprintf(
  90. 'Error reading XML string: %s',
  91. $message
  92. ), $error);
  93. }, E_WARNING
  94. );
  95. $return = $this->process();
  96. restore_error_handler();
  97. return $return;
  98. }
  99. /**
  100. * Process data from the created XMLReader.
  101. *
  102. * @return array
  103. */
  104. protected function process()
  105. {
  106. return $this->processNextElement();
  107. }
  108. /**
  109. * Process the next inner element.
  110. *
  111. * @return mixed
  112. */
  113. protected function processNextElement()
  114. {
  115. $children = array();
  116. $text = '';
  117. while ($this->reader->read()) {
  118. if ($this->reader->nodeType === XMLReader::ELEMENT) {
  119. if ($this->reader->depth === 0) {
  120. return $this->processNextElement();
  121. }
  122. $attributes = $this->getAttributes();
  123. $name = $this->reader->name;
  124. if ($this->reader->isEmptyElement) {
  125. $child = array();
  126. } else {
  127. $child = $this->processNextElement();
  128. }
  129. if ($attributes) {
  130. if (!is_array($child)) {
  131. $child = array();
  132. }
  133. $child = array_merge($child, $attributes);
  134. }
  135. if (isset($children[$name])) {
  136. if (!is_array($children[$name]) || !array_key_exists(0, $children[$name])) {
  137. $children[$name] = array($children[$name]);
  138. }
  139. $children[$name][] = $child;
  140. } else {
  141. $children[$name] = $child;
  142. }
  143. } elseif ($this->reader->nodeType === XMLReader::END_ELEMENT) {
  144. break;
  145. } elseif (in_array($this->reader->nodeType, $this->textNodes)) {
  146. $text .= $this->reader->value;
  147. }
  148. }
  149. return $children ?: $text;
  150. }
  151. /**
  152. * Get all attributes on the current node.
  153. *
  154. * @return array
  155. */
  156. protected function getAttributes()
  157. {
  158. $attributes = array();
  159. if ($this->reader->hasAttributes) {
  160. while ($this->reader->moveToNextAttribute()) {
  161. $attributes[$this->reader->localName] = $this->reader->value;
  162. }
  163. $this->reader->moveToElement();
  164. }
  165. return $attributes;
  166. }
  167. }