FileDumper.php 3.8 KB

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