Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Translation\Dumper; Chris@0: Chris@0: use Symfony\Component\Translation\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\Translation\Exception\RuntimeException; Chris@17: use Symfony\Component\Translation\MessageCatalogue; Chris@0: Chris@0: /** Chris@0: * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s). Chris@0: * Performs backup of already existing files. Chris@0: * Chris@0: * Options: Chris@0: * - path (mandatory): the directory where the files should be saved Chris@0: * Chris@0: * @author Michel Salib Chris@0: */ Chris@0: abstract class FileDumper implements DumperInterface Chris@0: { Chris@0: /** Chris@0: * A template for the relative paths to files. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $relativePathTemplate = '%domain%.%locale%.%extension%'; Chris@0: Chris@0: /** Chris@0: * Make file backup before the dump. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: private $backup = true; Chris@0: Chris@0: /** Chris@0: * Sets the template for the relative paths to files. Chris@0: * Chris@0: * @param string $relativePathTemplate A template for the relative paths to files Chris@0: */ Chris@0: public function setRelativePathTemplate($relativePathTemplate) Chris@0: { Chris@0: $this->relativePathTemplate = $relativePathTemplate; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets backup flag. Chris@0: * Chris@17: * @param bool $backup Chris@0: */ Chris@0: public function setBackup($backup) Chris@0: { Chris@0: $this->backup = $backup; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function dump(MessageCatalogue $messages, $options = []) Chris@0: { Chris@18: if (!\array_key_exists('path', $options)) { Chris@0: throw new InvalidArgumentException('The file dumper needs a path option.'); Chris@0: } Chris@0: Chris@0: // save a file for each domain Chris@0: foreach ($messages->getDomains() as $domain) { Chris@0: // backup Chris@0: $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale()); Chris@0: if (file_exists($fullpath)) { Chris@0: if ($this->backup) { Chris@14: @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); Chris@0: copy($fullpath, $fullpath.'~'); Chris@0: } Chris@0: } else { Chris@17: $directory = \dirname($fullpath); Chris@0: if (!file_exists($directory) && !@mkdir($directory, 0777, true)) { Chris@0: throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory)); Chris@0: } Chris@0: } Chris@0: // save file Chris@0: file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Transforms a domain of a message catalogue to its string representation. Chris@0: * Chris@0: * @param MessageCatalogue $messages Chris@0: * @param string $domain Chris@0: * @param array $options Chris@0: * Chris@0: * @return string representation Chris@0: */ Chris@17: abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = []); Chris@0: Chris@0: /** Chris@0: * Gets the file extension of the dumper. Chris@0: * Chris@0: * @return string file extension Chris@0: */ Chris@0: abstract protected function getExtension(); Chris@0: Chris@0: /** Chris@0: * Gets the relative file path using the template. Chris@0: * Chris@0: * @param string $domain The domain Chris@0: * @param string $locale The locale Chris@0: * Chris@0: * @return string The relative file path Chris@0: */ Chris@0: private function getRelativePath($domain, $locale) Chris@0: { Chris@17: return strtr($this->relativePathTemplate, [ Chris@0: '%domain%' => $domain, Chris@0: '%locale%' => $locale, Chris@0: '%extension%' => $this->getExtension(), Chris@17: ]); Chris@0: } Chris@0: }