Yaml.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * YAML config reader.
  13. */
  14. class Yaml implements ReaderInterface
  15. {
  16. /**
  17. * Directory of the YAML file
  18. *
  19. * @var string
  20. */
  21. protected $directory;
  22. /**
  23. * YAML decoder callback
  24. *
  25. * @var callable
  26. */
  27. protected $yamlDecoder;
  28. /**
  29. * Constructor
  30. *
  31. * @param callable $yamlDecoder
  32. */
  33. public function __construct($yamlDecoder = null)
  34. {
  35. if ($yamlDecoder !== null) {
  36. $this->setYamlDecoder($yamlDecoder);
  37. } else {
  38. if (function_exists('yaml_parse')) {
  39. $this->setYamlDecoder('yaml_parse');
  40. }
  41. }
  42. }
  43. /**
  44. * Set callback for decoding YAML
  45. *
  46. * @param string|callable $yamlDecoder the decoder to set
  47. * @return Yaml
  48. * @throws Exception\RuntimeException
  49. */
  50. public function setYamlDecoder($yamlDecoder)
  51. {
  52. if (!is_callable($yamlDecoder)) {
  53. throw new Exception\RuntimeException(
  54. 'Invalid parameter to setYamlDecoder() - must be callable'
  55. );
  56. }
  57. $this->yamlDecoder = $yamlDecoder;
  58. return $this;
  59. }
  60. /**
  61. * Get callback for decoding YAML
  62. *
  63. * @return callable
  64. */
  65. public function getYamlDecoder()
  66. {
  67. return $this->yamlDecoder;
  68. }
  69. /**
  70. * fromFile(): defined by Reader interface.
  71. *
  72. * @see ReaderInterface::fromFile()
  73. * @param string $filename
  74. * @return array
  75. * @throws Exception\RuntimeException
  76. */
  77. public function fromFile($filename)
  78. {
  79. if (!is_file($filename) || !is_readable($filename)) {
  80. throw new Exception\RuntimeException(sprintf(
  81. "File '%s' doesn't exist or not readable",
  82. $filename
  83. ));
  84. }
  85. if (null === $this->getYamlDecoder()) {
  86. throw new Exception\RuntimeException("You didn't specify a Yaml callback decoder");
  87. }
  88. $this->directory = dirname($filename);
  89. $config = call_user_func($this->getYamlDecoder(), file_get_contents($filename));
  90. if (null === $config) {
  91. throw new Exception\RuntimeException("Error parsing YAML data");
  92. }
  93. return $this->process($config);
  94. }
  95. /**
  96. * fromString(): defined by Reader interface.
  97. *
  98. * @see ReaderInterface::fromString()
  99. * @param string $string
  100. * @return array|bool
  101. * @throws Exception\RuntimeException
  102. */
  103. public function fromString($string)
  104. {
  105. if (null === $this->getYamlDecoder()) {
  106. throw new Exception\RuntimeException("You didn't specify a Yaml callback decoder");
  107. }
  108. if (empty($string)) {
  109. return array();
  110. }
  111. $this->directory = null;
  112. $config = call_user_func($this->getYamlDecoder(), $string);
  113. if (null === $config) {
  114. throw new Exception\RuntimeException("Error parsing YAML data");
  115. }
  116. return $this->process($config);
  117. }
  118. /**
  119. * Process the array for @include
  120. *
  121. * @param array $data
  122. * @return array
  123. * @throws Exception\RuntimeException
  124. */
  125. protected function process(array $data)
  126. {
  127. foreach ($data as $key => $value) {
  128. if (is_array($value)) {
  129. $data[$key] = $this->process($value);
  130. }
  131. if (trim($key) === '@include') {
  132. if ($this->directory === null) {
  133. throw new Exception\RuntimeException('Cannot process @include statement for a json string');
  134. }
  135. $reader = clone $this;
  136. unset($data[$key]);
  137. $data = array_replace_recursive($data, $reader->fromFile($this->directory . '/' . $value));
  138. }
  139. }
  140. return $data;
  141. }
  142. }