4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Dumper;
14 use Symfony\Component\Translation\Exception\InvalidArgumentException;
15 use Symfony\Component\Translation\Exception\RuntimeException;
16 use Symfony\Component\Translation\MessageCatalogue;
19 * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
20 * Performs backup of already existing files.
23 * - path (mandatory): the directory where the files should be saved
25 * @author Michel Salib <michelsalib@hotmail.com>
27 abstract class FileDumper implements DumperInterface
30 * A template for the relative paths to files.
34 protected $relativePathTemplate = '%domain%.%locale%.%extension%';
37 * Make file backup before the dump.
41 private $backup = true;
44 * Sets the template for the relative paths to files.
46 * @param string $relativePathTemplate A template for the relative paths to files
48 public function setRelativePathTemplate($relativePathTemplate)
50 $this->relativePathTemplate = $relativePathTemplate;
58 public function setBackup($backup)
60 $this->backup = $backup;
66 public function dump(MessageCatalogue $messages, $options = array())
68 if (!array_key_exists('path', $options)) {
69 throw new InvalidArgumentException('The file dumper needs a path option.');
72 // save a file for each domain
73 foreach ($messages->getDomains() as $domain) {
75 $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
76 if (file_exists($fullpath)) {
78 @trigger_error('Creating a backup while dumping a message catalogue is deprecated since Symfony 3.1 and will be removed in 4.0. Use TranslationWriter::disableBackup() to disable the backup.', E_USER_DEPRECATED);
79 copy($fullpath, $fullpath.'~');
82 $directory = \dirname($fullpath);
83 if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
84 throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
88 file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
93 * Transforms a domain of a message catalogue to its string representation.
95 * @param MessageCatalogue $messages
96 * @param string $domain
97 * @param array $options
99 * @return string representation
101 abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array());
104 * Gets the file extension of the dumper.
106 * @return string file extension
108 abstract protected function getExtension();
111 * Gets the relative file path using the template.
113 * @param string $domain The domain
114 * @param string $locale The locale
116 * @return string The relative file path
118 private function getRelativePath($domain, $locale)
120 return strtr($this->relativePathTemplate, array(
121 '%domain%' => $domain,
122 '%locale%' => $locale,
123 '%extension%' => $this->getExtension(),