YamlFile.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace RocketTheme\Toolbox\File;
  3. use Symfony\Component\Yaml\Exception\DumpException;
  4. use Symfony\Component\Yaml\Exception\ParseException;
  5. use Symfony\Component\Yaml\Yaml as YamlParser;
  6. use RocketTheme\Toolbox\Compat\Yaml\Yaml as FallbackYamlParser;
  7. /**
  8. * Implements YAML File reader.
  9. *
  10. * @package RocketTheme\Toolbox\File
  11. * @author RocketTheme
  12. * @license MIT
  13. */
  14. class YamlFile extends File
  15. {
  16. /**
  17. * @var array|File[]
  18. */
  19. static protected $instances = [];
  20. static protected $globalSettings = [
  21. 'compat' => true,
  22. 'native' => true
  23. ];
  24. /**
  25. * Set/get settings.
  26. *
  27. * @param array $settings
  28. * @return array
  29. */
  30. public static function globalSettings(array $settings = null)
  31. {
  32. if ($settings !== null) {
  33. static::$globalSettings = $settings;
  34. }
  35. return static::$globalSettings;
  36. }
  37. /**
  38. * Constructor.
  39. */
  40. protected function __construct()
  41. {
  42. parent::__construct();
  43. $this->extension = '.yaml';
  44. }
  45. /**
  46. * Set/get settings.
  47. *
  48. * @param array $settings
  49. * @return array
  50. */
  51. public function settings(array $settings = null)
  52. {
  53. if ($settings !== null) {
  54. $this->settings = $settings;
  55. }
  56. return $this->settings + static::$globalSettings;
  57. }
  58. /**
  59. * Get setting.
  60. *
  61. * @param string $setting
  62. * @param mixed $default
  63. * @return mixed
  64. */
  65. public function setting($setting, $default = null)
  66. {
  67. $value = parent::setting($setting);
  68. if (null === $value) {
  69. $value = isset(static::$globalSettings[$setting]) ? static::$globalSettings[$setting] : $default;
  70. }
  71. return $value;
  72. }
  73. /**
  74. * Check contents and make sure it is in correct format.
  75. *
  76. * @param array $var
  77. * @return array
  78. */
  79. protected function check($var)
  80. {
  81. return (array) $var;
  82. }
  83. /**
  84. * Encode contents into RAW string.
  85. *
  86. * @param array $var
  87. * @return string
  88. * @throws DumpException
  89. */
  90. protected function encode($var)
  91. {
  92. return (string) YamlParser::dump($var, $this->setting('inline', 5), $this->setting('indent', 2), true, false);
  93. }
  94. /**
  95. * Decode RAW string into contents.
  96. *
  97. * @param string $var
  98. * @return array mixed
  99. * @throws ParseException
  100. */
  101. protected function decode($var)
  102. {
  103. // Try native PECL YAML PHP extension first if available.
  104. if ($this->setting('native', true) && function_exists('yaml_parse')) {
  105. // Safely decode YAML.
  106. $saved = @ini_get('yaml.decode_php');
  107. @ini_set('yaml.decode_php', 0);
  108. $data = @yaml_parse($var);
  109. @ini_set('yaml.decode_php', $saved);
  110. if ($data !== false) {
  111. return (array) $data;
  112. }
  113. }
  114. try {
  115. return (array) YamlParser::parse($var);
  116. } catch (ParseException $e) {
  117. if ($this->setting('compat', true)) {
  118. return (array) FallbackYamlParser::parse($var);
  119. }
  120. throw $e;
  121. }
  122. }
  123. }