FileDumper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. protected string $relativePathTemplate = '%domain%.%locale%.%extension%';
  28. /**
  29. * Sets the template for the relative paths to files.
  30. */
  31. public function setRelativePathTemplate(string $relativePathTemplate): void
  32. {
  33. $this->relativePathTemplate = $relativePathTemplate;
  34. }
  35. public function dump(MessageCatalogue $messages, array $options = []): void
  36. {
  37. if (!\array_key_exists('path', $options)) {
  38. throw new InvalidArgumentException('The file dumper needs a path option.');
  39. }
  40. // save a file for each domain
  41. foreach ($messages->getDomains() as $domain) {
  42. $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
  43. if (!file_exists($fullpath)) {
  44. $directory = \dirname($fullpath);
  45. if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
  46. throw new RuntimeException(\sprintf('Unable to create directory "%s".', $directory));
  47. }
  48. }
  49. $intlDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
  50. $intlMessages = $messages->all($intlDomain);
  51. if ($intlMessages) {
  52. $intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale());
  53. file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options));
  54. $messages->replace([], $intlDomain);
  55. try {
  56. if ($messages->all($domain)) {
  57. file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
  58. }
  59. continue;
  60. } finally {
  61. $messages->replace($intlMessages, $intlDomain);
  62. }
  63. }
  64. file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
  65. }
  66. }
  67. /**
  68. * Transforms a domain of a message catalogue to its string representation.
  69. */
  70. abstract public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string;
  71. /**
  72. * Gets the file extension of the dumper.
  73. */
  74. abstract protected function getExtension(): string;
  75. /**
  76. * Gets the relative file path using the template.
  77. */
  78. private function getRelativePath(string $domain, string $locale): string
  79. {
  80. return strtr($this->relativePathTemplate, [
  81. '%domain%' => $domain,
  82. '%locale%' => $locale,
  83. '%extension%' => $this->getExtension(),
  84. ]);
  85. }
  86. }