Просмотр файла vendor/symfony/translation/Writer/TranslationWriter.php

Размер файла: 2.53Kb
  1. <?php
  2.  
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11.  
  12. namespace Symfony\Component\Translation\Writer;
  13.  
  14. use Symfony\Component\Translation\MessageCatalogue;
  15. use Symfony\Component\Translation\Dumper\DumperInterface;
  16. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  17. use Symfony\Component\Translation\Exception\RuntimeException;
  18.  
  19. /**
  20. * TranslationWriter writes translation messages.
  21. *
  22. * @author Michel Salib <michelsalib@hotmail.com>
  23. */
  24. class TranslationWriter
  25. {
  26. /**
  27. * Dumpers used for export.
  28. *
  29. * @var array
  30. */
  31. private $dumpers = array();
  32.  
  33. /**
  34. * Adds a dumper to the writer.
  35. *
  36. * @param string $format The format of the dumper
  37. * @param DumperInterface $dumper The dumper
  38. */
  39. public function addDumper($format, DumperInterface $dumper)
  40. {
  41. $this->dumpers[$format] = $dumper;
  42. }
  43.  
  44. /**
  45. * Disables dumper backup.
  46. */
  47. public function disableBackup()
  48. {
  49. foreach ($this->dumpers as $dumper) {
  50. if (method_exists($dumper, 'setBackup')) {
  51. $dumper->setBackup(false);
  52. }
  53. }
  54. }
  55.  
  56. /**
  57. * Obtains the list of supported formats.
  58. *
  59. * @return array
  60. */
  61. public function getFormats()
  62. {
  63. return array_keys($this->dumpers);
  64. }
  65.  
  66. /**
  67. * Writes translation from the catalogue according to the selected format.
  68. *
  69. * @param MessageCatalogue $catalogue The message catalogue to dump
  70. * @param string $format The format to use to dump the messages
  71. * @param array $options Options that are passed to the dumper
  72. *
  73. * @throws InvalidArgumentException
  74. */
  75. public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
  76. {
  77. if (!isset($this->dumpers[$format])) {
  78. throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
  79. }
  80.  
  81. // get the right dumper
  82. $dumper = $this->dumpers[$format];
  83.  
  84. if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
  85. throw new RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path']));
  86. }
  87.  
  88. // save
  89. $dumper->dump($catalogue, $options);
  90. }
  91. }