FileDumper.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Dumper;
  11. use Symfony\Component\Translation\MessageCatalogue;
  12. /**
  13. * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
  14. * Performs backup of already existing files.
  15. *
  16. * Options:
  17. * - path (mandatory): the directory where the files should be saved
  18. *
  19. * @author Michel Salib <michelsalib@hotmail.com>
  20. */
  21. abstract class FileDumper implements DumperInterface
  22. {
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function dump(MessageCatalogue $messages, $options = array())
  27. {
  28. if (!array_key_exists('path', $options)) {
  29. throw new \InvalidArgumentException('The file dumper need a path options.');
  30. }
  31. // save a file for each domain
  32. foreach ($messages->getDomains() as $domain) {
  33. $file = $domain.'.'.$messages->getLocale().'.'.$this->getExtension();
  34. // backup
  35. $fullpath = $options['path'].'/'.$file;
  36. if (file_exists($fullpath)) {
  37. copy($fullpath, $fullpath.'~');
  38. }
  39. // save file
  40. file_put_contents($fullpath, $this->format($messages, $domain));
  41. }
  42. }
  43. /**
  44. * Transforms a domain of a message catalogue to its string representation.
  45. *
  46. * @param MessageCatalogue $messages
  47. * @param string $domain
  48. *
  49. * @return string representation
  50. */
  51. abstract protected function format(MessageCatalogue $messages, $domain);
  52. /**
  53. * Gets the file extension of the dumper.
  54. *
  55. * @return string file extension
  56. */
  57. abstract protected function getExtension();
  58. }