Nav apraksta

FileDumper.php 3.7KB

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