SymfonyComponentTranslationDumperFileDumper

FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).

Defined (1)

The class is defined in the following location(s).

/vendor/symfony/translation/Dumper/FileDumper.php  
  1. abstract class FileDumper implements DumperInterface 
  2. /** 
  3. * A template for the relative paths to files. 
  4. * @var string 
  5. */ 
  6. protected $relativePathTemplate = '%domain%.%locale%.%extension%'; 
  7.  
  8. /** 
  9. * Make file backup before the dump. 
  10. * @var bool 
  11. */ 
  12. private $backup = true; 
  13.  
  14. /** 
  15. * Sets the template for the relative paths to files. 
  16. * @param string $relativePathTemplate A template for the relative paths to files 
  17. */ 
  18. public function setRelativePathTemplate($relativePathTemplate) 
  19. $this->relativePathTemplate = $relativePathTemplate; 
  20.  
  21. /** 
  22. * Sets backup flag. 
  23. * @param bool 
  24. */ 
  25. public function setBackup($backup) 
  26. $this->backup = $backup; 
  27.  
  28. /** 
  29. * {@inheritdoc} 
  30. */ 
  31. public function dump(MessageCatalogue $messages, $options = array()) 
  32. if (!array_key_exists('path', $options)) { 
  33. throw new \InvalidArgumentException('The file dumper needs a path option.'); 
  34.  
  35. // save a file for each domain 
  36. foreach ($messages->getDomains() as $domain) { 
  37. // backup 
  38. $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale()); 
  39. if (file_exists($fullpath)) { 
  40. if ($this->backup) { 
  41. @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); 
  42. copy($fullpath, $fullpath.'~'); 
  43. } else { 
  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. // save file 
  48. file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options)); 
  49.  
  50. /** 
  51. * Transforms a domain of a message catalogue to its string representation. 
  52. * @param MessageCatalogue $messages 
  53. * @param string $domain 
  54. * @param array $options 
  55. * @return string representation 
  56. */ 
  57. abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()); 
  58.  
  59. /** 
  60. * Gets the file extension of the dumper. 
  61. * @return string file extension 
  62. */ 
  63. abstract protected function getExtension(); 
  64.  
  65. /** 
  66. * Gets the relative file path using the template. 
  67. * @param string $domain The domain 
  68. * @param string $locale The locale 
  69. * @return string The relative file path 
  70. */ 
  71. private function getRelativePath($domain, $locale) 
  72. return strtr($this->relativePathTemplate, array( 
  73. '%domain%' => $domain,  
  74. '%locale%' => $locale,  
  75. '%extension%' => $this->getExtension(),  
  76. ));