YamlFormatter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Framework\File\Formatter
  5. *
  6. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Framework\File\Formatter;
  10. use Grav\Framework\File\Interfaces\FileFormatterInterface;
  11. use Symfony\Component\Yaml\Exception\DumpException;
  12. use Symfony\Component\Yaml\Exception\ParseException;
  13. use Symfony\Component\Yaml\Yaml as YamlParser;
  14. use RocketTheme\Toolbox\Compat\Yaml\Yaml as FallbackYamlParser;
  15. class YamlFormatter extends AbstractFormatter
  16. {
  17. public function __construct(array $config = [])
  18. {
  19. $config += [
  20. 'file_extension' => '.yaml',
  21. 'inline' => 5,
  22. 'indent' => 2,
  23. 'native' => true,
  24. 'compat' => true
  25. ];
  26. parent::__construct($config);
  27. }
  28. /**
  29. * @return int
  30. */
  31. public function getInlineOption(): int
  32. {
  33. return $this->getConfig('inline');
  34. }
  35. /**
  36. * @return int
  37. */
  38. public function getIndentOption(): int
  39. {
  40. return $this->getConfig('indent');
  41. }
  42. /**
  43. * @return bool
  44. */
  45. public function useNativeDecoder(): bool
  46. {
  47. return $this->getConfig('native');
  48. }
  49. /**
  50. * @return bool
  51. */
  52. public function useCompatibleDecoder(): bool
  53. {
  54. return $this->getConfig('compat');
  55. }
  56. /**
  57. * {@inheritdoc}
  58. * @see FileFormatterInterface::encode()
  59. */
  60. public function encode($data, $inline = null, $indent = null): string
  61. {
  62. try {
  63. return YamlParser::dump(
  64. $data,
  65. $inline ? (int) $inline : $this->getInlineOption(),
  66. $indent ? (int) $indent : $this->getIndentOption(),
  67. YamlParser::DUMP_EXCEPTION_ON_INVALID_TYPE
  68. );
  69. } catch (DumpException $e) {
  70. throw new \RuntimeException('Encoding YAML failed: ' . $e->getMessage(), 0, $e);
  71. }
  72. }
  73. /**
  74. * {@inheritdoc}
  75. * @see FileFormatterInterface::decode()
  76. */
  77. public function decode($data): array
  78. {
  79. // Try native PECL YAML PHP extension first if available.
  80. if (\function_exists('yaml_parse') && $this->useNativeDecoder()) {
  81. // Safely decode YAML.
  82. $saved = @ini_get('yaml.decode_php');
  83. @ini_set('yaml.decode_php', '0');
  84. $decoded = @yaml_parse($data);
  85. @ini_set('yaml.decode_php', $saved);
  86. if ($decoded !== false) {
  87. return (array) $decoded;
  88. }
  89. }
  90. try {
  91. return (array) YamlParser::parse($data);
  92. } catch (ParseException $e) {
  93. if ($this->useCompatibleDecoder()) {
  94. return (array) FallbackYamlParser::parse($data);
  95. }
  96. throw new \RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
  97. }
  98. }
  99. }