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\MessageCatalogue;
17 * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
18 * Performs backup of already existing files.
21 * - path (mandatory): the directory where the files should be saved
23 * @author Michel Salib <michelsalib@hotmail.com>
25 abstract class FileDumper implements DumperInterface
28 * A template for the relative paths to files.
32 protected $relativePathTemplate = '%domain%.%locale%.%extension%';
35 * Make file backup before the dump.
39 private $backup = true;
42 * Sets the template for the relative paths to files.
44 * @param string $relativePathTemplate A template for the relative paths to files
46 public function setRelativePathTemplate($relativePathTemplate)
48 $this->relativePathTemplate = $relativePathTemplate;
56 public function setBackup($backup)
58 $this->backup = $backup;
64 public function dump(MessageCatalogue $messages, $options = array())
66 if (!array_key_exists('path', $options)) {
67 throw new \InvalidArgumentException('The file dumper needs a path option.');
70 // save a file for each domain
71 foreach ($messages->getDomains() as $domain) {
73 $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
74 if (file_exists($fullpath)) {
76 copy($fullpath, $fullpath.'~');
79 $directory = dirname($fullpath);
80 if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
81 throw new \RuntimeException(sprintf('Unable to create directory "%s".', $directory));
85 file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
90 * Transforms a domain of a message catalogue to its string representation.
92 * Override this function in child class if $options is used for message formatting.
94 * @param MessageCatalogue $messages
95 * @param string $domain
96 * @param array $options
98 * @return string representation
100 public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
102 @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);
104 return $this->format($messages, $domain);
108 * Transforms a domain of a message catalogue to its string representation.
110 * @param MessageCatalogue $messages
111 * @param string $domain
113 * @return string representation
115 * @deprecated since version 2.8, to be removed in 3.0. Overwrite formatCatalogue() instead.
117 protected function format(MessageCatalogue $messages, $domain)
119 throw new \LogicException('The "FileDumper::format" method needs to be overwritten, you should implement either "format" or "formatCatalogue".');
123 * Gets the file extension of the dumper.
125 * @return string file extension
127 abstract protected function getExtension();
130 * Gets the relative file path using the template.
132 * @param string $domain The domain
133 * @param string $locale The locale
135 * @return string The relative file path
137 private function getRelativePath($domain, $locale)
139 return strtr($this->relativePathTemplate, array(
140 '%domain%' => $domain,
141 '%locale%' => $locale,
142 '%extension%' => $this->getExtension(),