Yaml.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Config
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Config_Writer
  23. */
  24. #require_once 'Zend/Config/Writer/FileAbstract.php';
  25. /**
  26. * @see Zend_Config_Yaml
  27. */
  28. #require_once 'Zend/Config/Yaml.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Config
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Config_Writer_Yaml extends Zend_Config_Writer_FileAbstract
  36. {
  37. /**
  38. * What to call when we need to decode some YAML?
  39. *
  40. * @var callable
  41. */
  42. protected $_yamlEncoder = array('Zend_Config_Writer_Yaml', 'encode');
  43. /**
  44. * Get callback for decoding YAML
  45. *
  46. * @return callable
  47. */
  48. public function getYamlEncoder()
  49. {
  50. return $this->_yamlEncoder;
  51. }
  52. /**
  53. * Set callback for decoding YAML
  54. *
  55. * @param callable $yamlEncoder the decoder to set
  56. * @return Zend_Config_Yaml
  57. */
  58. public function setYamlEncoder($yamlEncoder)
  59. {
  60. if (!is_callable($yamlEncoder)) {
  61. #require_once 'Zend/Config/Exception.php';
  62. throw new Zend_Config_Exception('Invalid parameter to setYamlEncoder - must be callable');
  63. }
  64. $this->_yamlEncoder = $yamlEncoder;
  65. return $this;
  66. }
  67. /**
  68. * Render a Zend_Config into a YAML config string.
  69. *
  70. * @since 1.10
  71. * @return string
  72. */
  73. public function render()
  74. {
  75. $data = $this->_config->toArray();
  76. $sectionName = $this->_config->getSectionName();
  77. $extends = $this->_config->getExtends();
  78. if (is_string($sectionName)) {
  79. $data = array($sectionName => $data);
  80. }
  81. foreach ($extends as $section => $parentSection) {
  82. $data[$section][Zend_Config_Yaml::EXTENDS_NAME] = $parentSection;
  83. }
  84. // Ensure that each "extends" section actually exists
  85. foreach ($data as $section => $sectionData) {
  86. if (is_array($sectionData) && isset($sectionData[Zend_Config_Yaml::EXTENDS_NAME])) {
  87. $sectionExtends = $sectionData[Zend_Config_Yaml::EXTENDS_NAME];
  88. if (!isset($data[$sectionExtends])) {
  89. // Remove "extends" declaration if section does not exist
  90. unset($data[$section][Zend_Config_Yaml::EXTENDS_NAME]);
  91. }
  92. }
  93. }
  94. return call_user_func($this->getYamlEncoder(), $data);
  95. }
  96. /**
  97. * Very dumb YAML encoder
  98. *
  99. * Until we have Zend_Yaml...
  100. *
  101. * @param array $data YAML data
  102. * @return string
  103. */
  104. public static function encode($data)
  105. {
  106. return self::_encodeYaml(0, $data);
  107. }
  108. /**
  109. * Service function for encoding YAML
  110. *
  111. * @param int $indent Current indent level
  112. * @param array $data Data to encode
  113. * @return string
  114. */
  115. protected static function _encodeYaml($indent, $data)
  116. {
  117. reset($data);
  118. $result = "";
  119. $numeric = is_numeric(key($data));
  120. foreach($data as $key => $value) {
  121. if(is_array($value)) {
  122. $encoded = "\n".self::_encodeYaml($indent+1, $value);
  123. } else {
  124. $encoded = (string)$value."\n";
  125. }
  126. $result .= str_repeat(" ", $indent).($numeric?"- ":"$key: ").$encoded;
  127. }
  128. return $result;
  129. }
  130. }