FileDumper.php 3.4 KB

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