FileDumper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\InvalidArgumentException;
  12. use Symfony\Component\Translation\Exception\RuntimeException;
  13. use Symfony\Component\Translation\MessageCatalogue;
  14. /**
  15. * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
  16. *
  17. * Options:
  18. * - path (mandatory): the directory where the files should be saved
  19. *
  20. * @author Michel Salib <michelsalib@hotmail.com>
  21. */
  22. abstract class FileDumper implements DumperInterface
  23. {
  24. /**
  25. * A template for the relative paths to files.
  26. *
  27. * @var string
  28. */
  29. protected $relativePathTemplate = '%domain%.%locale%.%extension%';
  30. /**
  31. * Sets the template for the relative paths to files.
  32. *
  33. * @param string $relativePathTemplate A template for the relative paths to files
  34. */
  35. public function setRelativePathTemplate($relativePathTemplate)
  36. {
  37. $this->relativePathTemplate = $relativePathTemplate;
  38. }
  39. /**
  40. * Sets backup flag.
  41. *
  42. * @param bool $backup
  43. *
  44. * @deprecated since Symfony 4.1
  45. */
  46. public function setBackup($backup)
  47. {
  48. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED);
  49. if (false !== $backup) {
  50. throw new \LogicException('The backup feature is no longer supported.');
  51. }
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function dump(MessageCatalogue $messages, $options = [])
  57. {
  58. if (!\array_key_exists('path', $options)) {
  59. throw new InvalidArgumentException('The file dumper needs a path option.');
  60. }
  61. $hasMessageFormatter = class_exists(\MessageFormatter::class);
  62. // save a file for each domain
  63. foreach ($messages->getDomains() as $domain) {
  64. if ($hasMessageFormatter) {
  65. $defaultDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
  66. $altDomain = $domain;
  67. } else {
  68. $defaultDomain = $domain;
  69. $altDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
  70. }
  71. $defaultPath = $options['path'].'/'.$this->getRelativePath($defaultDomain, $messages->getLocale());
  72. $altPath = $options['path'].'/'.$this->getRelativePath($altDomain, $messages->getLocale());
  73. if (!file_exists($defaultPath) && file_exists($altPath)) {
  74. [$defaultPath, $altPath] = [$altPath, $defaultPath];
  75. }
  76. if (!file_exists($defaultPath)) {
  77. $directory = \dirname($defaultPath);
  78. if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
  79. throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
  80. }
  81. }
  82. if (file_exists($altPath)) {
  83. // clear alternative translation file
  84. file_put_contents($altPath, $this->formatCatalogue(new MessageCatalogue($messages->getLocale()), $altDomain, $options));
  85. }
  86. file_put_contents($defaultPath, $this->formatCatalogue($messages, $domain, $options));
  87. }
  88. }
  89. /**
  90. * Transforms a domain of a message catalogue to its string representation.
  91. *
  92. * @param string $domain
  93. *
  94. * @return string representation
  95. */
  96. abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = []);
  97. /**
  98. * Gets the file extension of the dumper.
  99. *
  100. * @return string file extension
  101. */
  102. abstract protected function getExtension();
  103. /**
  104. * Gets the relative file path using the template.
  105. */
  106. private function getRelativePath(string $domain, string $locale): string
  107. {
  108. return strtr($this->relativePathTemplate, [
  109. '%domain%' => $domain,
  110. '%locale%' => $locale,
  111. '%extension%' => $this->getExtension(),
  112. ]);
  113. }
  114. }