YamlFileDumper.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Dumper;
  11. use Symfony\Component\Translation\Exception\LogicException;
  12. use Symfony\Component\Translation\MessageCatalogue;
  13. use Symfony\Component\Translation\Util\ArrayConverter;
  14. use Symfony\Component\Yaml\Yaml;
  15. /**
  16. * YamlFileDumper generates yaml files from a message catalogue.
  17. *
  18. * @author Michel Salib <michelsalib@hotmail.com>
  19. */
  20. class YamlFileDumper extends FileDumper
  21. {
  22. private $extension;
  23. public function __construct(string $extension = 'yml')
  24. {
  25. $this->extension = $extension;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
  31. {
  32. if (!class_exists('Symfony\Component\Yaml\Yaml')) {
  33. throw new LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');
  34. }
  35. $data = $messages->all($domain);
  36. if (isset($options['as_tree']) && $options['as_tree']) {
  37. $data = ArrayConverter::expandToTree($data);
  38. }
  39. if (isset($options['inline']) && ($inline = (int) $options['inline']) > 0) {
  40. return Yaml::dump($data, $inline);
  41. }
  42. return Yaml::dump($data);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function getExtension()
  48. {
  49. return $this->extension;
  50. }
  51. }