fe85e5dee906d6185c162370d6f8726be3b8e1f2
[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\Exception\InvalidArgumentException;
15 use Symfony\Component\Translation\Exception\RuntimeException;
16 use Symfony\Component\Translation\MessageCatalogue;
17
18 /**
19  * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
20  * Performs backup of already existing files.
21  *
22  * Options:
23  * - path (mandatory): the directory where the files should be saved
24  *
25  * @author Michel Salib <michelsalib@hotmail.com>
26  */
27 abstract class FileDumper implements DumperInterface
28 {
29     /**
30      * A template for the relative paths to files.
31      *
32      * @var string
33      */
34     protected $relativePathTemplate = '%domain%.%locale%.%extension%';
35
36     /**
37      * Make file backup before the dump.
38      *
39      * @var bool
40      */
41     private $backup = true;
42
43     /**
44      * Sets the template for the relative paths to files.
45      *
46      * @param string $relativePathTemplate A template for the relative paths to files
47      */
48     public function setRelativePathTemplate($relativePathTemplate)
49     {
50         $this->relativePathTemplate = $relativePathTemplate;
51     }
52
53     /**
54      * Sets backup flag.
55      *
56      * @param bool
57      */
58     public function setBackup($backup)
59     {
60         $this->backup = $backup;
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     public function dump(MessageCatalogue $messages, $options = array())
67     {
68         if (!array_key_exists('path', $options)) {
69             throw new InvalidArgumentException('The file dumper needs a path option.');
70         }
71
72         // save a file for each domain
73         foreach ($messages->getDomains() as $domain) {
74             // backup
75             $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
76             if (file_exists($fullpath)) {
77                 if ($this->backup) {
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.'~');
80                 }
81             } else {
82                 $directory = \dirname($fullpath);
83                 if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
84                     throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
85                 }
86             }
87             // save file
88             file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
89         }
90     }
91
92     /**
93      * Transforms a domain of a message catalogue to its string representation.
94      *
95      * @param MessageCatalogue $messages
96      * @param string           $domain
97      * @param array            $options
98      *
99      * @return string representation
100      */
101     abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array());
102
103     /**
104      * Gets the file extension of the dumper.
105      *
106      * @return string file extension
107      */
108     abstract protected function getExtension();
109
110     /**
111      * Gets the relative file path using the template.
112      *
113      * @param string $domain The domain
114      * @param string $locale The locale
115      *
116      * @return string The relative file path
117      */
118     private function getRelativePath($domain, $locale)
119     {
120         return strtr($this->relativePathTemplate, array(
121             '%domain%' => $domain,
122             '%locale%' => $locale,
123             '%extension%' => $this->getExtension(),
124         ));
125     }
126 }