Minor dependency updates
[yaffs-website] / vendor / symfony / translation / Dumper / FileDumper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation\Dumper;
13
14 use Symfony\Component\Translation\MessageCatalogue;
15
16 /**
17  * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
18  * Performs backup of already existing files.
19  *
20  * Options:
21  * - path (mandatory): the directory where the files should be saved
22  *
23  * @author Michel Salib <michelsalib@hotmail.com>
24  */
25 abstract class FileDumper implements DumperInterface
26 {
27     /**
28      * A template for the relative paths to files.
29      *
30      * @var string
31      */
32     protected $relativePathTemplate = '%domain%.%locale%.%extension%';
33
34     /**
35      * Make file backup before the dump.
36      *
37      * @var bool
38      */
39     private $backup = true;
40
41     /**
42      * Sets the template for the relative paths to files.
43      *
44      * @param string $relativePathTemplate A template for the relative paths to files
45      */
46     public function setRelativePathTemplate($relativePathTemplate)
47     {
48         $this->relativePathTemplate = $relativePathTemplate;
49     }
50
51     /**
52      * Sets backup flag.
53      *
54      * @param bool
55      */
56     public function setBackup($backup)
57     {
58         $this->backup = $backup;
59     }
60
61     /**
62      * {@inheritdoc}
63      */
64     public function dump(MessageCatalogue $messages, $options = array())
65     {
66         if (!array_key_exists('path', $options)) {
67             throw new \InvalidArgumentException('The file dumper needs a path option.');
68         }
69
70         // save a file for each domain
71         foreach ($messages->getDomains() as $domain) {
72             // backup
73             $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
74             if (file_exists($fullpath)) {
75                 if ($this->backup) {
76                     copy($fullpath, $fullpath.'~');
77                 }
78             } else {
79                 $directory = dirname($fullpath);
80                 if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
81                     throw new \RuntimeException(sprintf('Unable to create directory "%s".', $directory));
82                 }
83             }
84             // save file
85             file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
86         }
87     }
88
89     /**
90      * Transforms a domain of a message catalogue to its string representation.
91      *
92      * Override this function in child class if $options is used for message formatting.
93      *
94      * @param MessageCatalogue $messages
95      * @param string           $domain
96      * @param array            $options
97      *
98      * @return string representation
99      */
100     public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
101     {
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);
103
104         return $this->format($messages, $domain);
105     }
106
107     /**
108      * Transforms a domain of a message catalogue to its string representation.
109      *
110      * @param MessageCatalogue $messages
111      * @param string           $domain
112      *
113      * @return string representation
114      *
115      * @deprecated since version 2.8, to be removed in 3.0. Overwrite formatCatalogue() instead.
116      */
117     protected function format(MessageCatalogue $messages, $domain)
118     {
119         throw new \LogicException('The "FileDumper::format" method needs to be overwritten, you should implement either "format" or "formatCatalogue".');
120     }
121
122     /**
123      * Gets the file extension of the dumper.
124      *
125      * @return string file extension
126      */
127     abstract protected function getExtension();
128
129     /**
130      * Gets the relative file path using the template.
131      *
132      * @param string $domain The domain
133      * @param string $locale The locale
134      *
135      * @return string The relative file path
136      */
137     private function getRelativePath($domain, $locale)
138     {
139         return strtr($this->relativePathTemplate, array(
140             '%domain%' => $domain,
141             '%locale%' => $locale,
142             '%extension%' => $this->getExtension(),
143         ));
144     }
145 }