Nessuna descrizione

TranslationWriter.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Writer;
  11. use Symfony\Component\Translation\Dumper\DumperInterface;
  12. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  13. use Symfony\Component\Translation\Exception\RuntimeException;
  14. use Symfony\Component\Translation\MessageCatalogue;
  15. /**
  16. * TranslationWriter writes translation messages.
  17. *
  18. * @author Michel Salib <michelsalib@hotmail.com>
  19. */
  20. class TranslationWriter implements TranslationWriterInterface
  21. {
  22. private $dumpers = [];
  23. /**
  24. * Adds a dumper to the writer.
  25. *
  26. * @param string $format The format of the dumper
  27. */
  28. public function addDumper($format, DumperInterface $dumper)
  29. {
  30. $this->dumpers[$format] = $dumper;
  31. }
  32. /**
  33. * Disables dumper backup.
  34. *
  35. * @deprecated since Symfony 4.1
  36. */
  37. public function disableBackup()
  38. {
  39. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1.', __METHOD__), \E_USER_DEPRECATED);
  40. foreach ($this->dumpers as $dumper) {
  41. if (method_exists($dumper, 'setBackup')) {
  42. $dumper->setBackup(false);
  43. }
  44. }
  45. }
  46. /**
  47. * Obtains the list of supported formats.
  48. *
  49. * @return array
  50. */
  51. public function getFormats()
  52. {
  53. return array_keys($this->dumpers);
  54. }
  55. /**
  56. * Writes translation from the catalogue according to the selected format.
  57. *
  58. * @param string $format The format to use to dump the messages
  59. * @param array $options Options that are passed to the dumper
  60. *
  61. * @throws InvalidArgumentException
  62. */
  63. public function write(MessageCatalogue $catalogue, $format, $options = [])
  64. {
  65. if (!isset($this->dumpers[$format])) {
  66. throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
  67. }
  68. // get the right dumper
  69. $dumper = $this->dumpers[$format];
  70. if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
  71. throw new RuntimeException(sprintf('Translation Writer was not able to create directory "%s".', $options['path']));
  72. }
  73. // save
  74. $dumper->dump($catalogue, $options);
  75. }
  76. }