Drupal investigation

FileDumper.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * Override this function in child class if $options is used for message formatting.
  83. *
  84. * @param MessageCatalogue $messages
  85. * @param string $domain
  86. * @param array $options
  87. *
  88. * @return string representation
  89. */
  90. public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
  91. {
  92. @trigger_error('The '.__METHOD__.' method will replace the format method in 3.0. You should overwrite it instead of overwriting format instead.', E_USER_DEPRECATED);
  93. return $this->format($messages, $domain);
  94. }
  95. /**
  96. * Transforms a domain of a message catalogue to its string representation.
  97. *
  98. * @param MessageCatalogue $messages
  99. * @param string $domain
  100. *
  101. * @return string representation
  102. *
  103. * @deprecated since version 2.8, to be removed in 3.0. Overwrite formatCatalogue() instead.
  104. */
  105. protected function format(MessageCatalogue $messages, $domain)
  106. {
  107. throw new \LogicException('The "FileDumper::format" method needs to be overwritten, you should implement either "format" or "formatCatalogue".');
  108. }
  109. /**
  110. * Gets the file extension of the dumper.
  111. *
  112. * @return string file extension
  113. */
  114. abstract protected function getExtension();
  115. /**
  116. * Gets the relative file path using the template.
  117. *
  118. * @param string $domain The domain
  119. * @param string $locale The locale
  120. *
  121. * @return string The relative file path
  122. */
  123. private function getRelativePath($domain, $locale)
  124. {
  125. return strtr($this->relativePathTemplate, array(
  126. '%domain%' => $domain,
  127. '%locale%' => $locale,
  128. '%extension%' => $this->getExtension(),
  129. ));
  130. }
  131. }