PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Core/Db/Fixture/Reader/Yaml.php

https://bitbucket.org/Yuri_m/clean-zfext-by-yuri-git
PHP | 162 lines | 92 code | 17 blank | 53 comment | 22 complexity | 8555ee03261d82fe2c06602ba5fc85e3 MD5 | raw file
  1. <?php
  2. /**
  3. * Read data from Yaml
  4. *
  5. * @category Core
  6. * @package Core_Migration
  7. * @subpackage Core_Migration_Reader
  8. * @author V.Leontiev
  9. *
  10. * @version $Id$
  11. */
  12. class Core_Migration_Reader_Yaml extends Core_Migration_Reader_Abstract
  13. {
  14. /**
  15. * Load yaml data from the file
  16. *
  17. * @author V.Leontiev
  18. * @param string $yaml
  19. * @param array $options
  20. * @throws Zend_Exception
  21. */
  22. public function __construct($yaml, $options = null)
  23. {
  24. if (empty($yaml) || !file_exists($yaml)) {
  25. throw new Zend_Exception('Filename is not set');
  26. }
  27. $yaml = file_get_contents($yaml);
  28. $data = self::decode($yaml);
  29. if (null === $data) {
  30. throw new Zend_Exception("Error parsing YAML data");
  31. }
  32. parent::__construct($data);
  33. }
  34. /**
  35. * Very dumb YAML parser
  36. *
  37. * Until we have Zend_Yaml...
  38. *
  39. * @param string $yaml YAML source
  40. * @return array Decoded data
  41. */
  42. public static function decode($yaml)
  43. {
  44. $lines = explode("\n", $yaml);
  45. reset($lines);
  46. return self::_decodeYaml(0, $lines);
  47. }
  48. /**
  49. * Service function to decode YAML
  50. *
  51. * @param int $currentIndent Current indent level
  52. * @param array $lines YAML lines
  53. * @return array|string
  54. */
  55. protected static function _decodeYaml($currentIndent, &$lines)
  56. {
  57. $config = array();
  58. $inIndent = false;
  59. while (list($n, $line) = each($lines)) {
  60. $lineno = $n + 1;
  61. $line = rtrim(preg_replace("/#.*$/", "", $line));
  62. if (strlen($line) == 0) {
  63. continue;
  64. }
  65. $indent = strspn($line, " ");
  66. // line without the spaces
  67. $line = trim($line);
  68. if (strlen($line) == 0) {
  69. continue;
  70. }
  71. if ($indent < $currentIndent) {
  72. // this level is done
  73. prev($lines);
  74. return $config;
  75. }
  76. if (!$inIndent) {
  77. $currentIndent = $indent;
  78. $inIndent = true;
  79. }
  80. if (preg_match("/(\w+):\s*(.*)/", $line, $m)) {
  81. // key: value
  82. if (strlen($m[2])) {
  83. // simple key: value
  84. $value = rtrim(preg_replace("/#.*$/", "", $m[2]));
  85. // Check for booleans and constants
  86. if (preg_match('/^(t(rue)?|on|y(es)?)$/i', $value)) {
  87. $value = true;
  88. } elseif (preg_match('/^(f(alse)?|off|n(o)?)$/i', $value)) {
  89. $value = false;
  90. } else {
  91. // test for constants
  92. $value = self::_replaceConstants($value);
  93. }
  94. } else {
  95. // key: and then values on new lines
  96. $value = self::_decodeYaml($currentIndent + 1, $lines);
  97. if (is_array($value) && !count($value)) {
  98. $value = "";
  99. }
  100. }
  101. $config[$m[1]] = $value;
  102. } elseif ($line[0] == "-") {
  103. // item in the list:
  104. // - FOO
  105. if (strlen($line) > 2) {
  106. $config[] = substr($line, 2);
  107. } else {
  108. $config[] = self::_decodeYaml($currentIndent + 1, $lines);
  109. }
  110. } else {
  111. throw new Zend_Exception(sprintf(
  112. 'Error parsing YAML at line %d - unsupported syntax: "%s"',
  113. $lineno, $line
  114. ));
  115. }
  116. }
  117. return $config;
  118. }
  119. /**
  120. * Replace any constants referenced in a string with their values
  121. *
  122. * @param string $value
  123. * @return string
  124. */
  125. protected static function _replaceConstants($value)
  126. {
  127. foreach (self::_getConstants() as $constant) {
  128. if (strstr($value, $constant)) {
  129. $value = str_replace($constant, constant($constant), $value);
  130. }
  131. }
  132. return $value;
  133. }
  134. /**
  135. * Get (reverse) sorted list of defined constant names
  136. *
  137. * @return array
  138. */
  139. protected static function _getConstants()
  140. {
  141. $constants = array_keys(get_defined_constants());
  142. rsort($constants, SORT_STRING);
  143. return $constants;
  144. }
  145. }