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\Writer; Chris@0: Chris@0: use Symfony\Component\Translation\MessageCatalogue; Chris@0: use Symfony\Component\Translation\Dumper\DumperInterface; Chris@0: use Symfony\Component\Translation\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\Translation\Exception\RuntimeException; Chris@0: Chris@0: /** Chris@0: * TranslationWriter writes translation messages. Chris@0: * Chris@0: * @author Michel Salib Chris@0: */ Chris@0: class TranslationWriter Chris@0: { Chris@0: /** Chris@0: * Dumpers used for export. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: private $dumpers = array(); Chris@0: Chris@0: /** Chris@0: * Adds a dumper to the writer. Chris@0: * Chris@0: * @param string $format The format of the dumper Chris@0: * @param DumperInterface $dumper The dumper Chris@0: */ Chris@0: public function addDumper($format, DumperInterface $dumper) Chris@0: { Chris@0: $this->dumpers[$format] = $dumper; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Disables dumper backup. Chris@0: */ Chris@0: public function disableBackup() Chris@0: { Chris@0: foreach ($this->dumpers as $dumper) { Chris@0: if (method_exists($dumper, 'setBackup')) { Chris@0: $dumper->setBackup(false); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Obtains the list of supported formats. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getFormats() Chris@0: { Chris@0: return array_keys($this->dumpers); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes translation from the catalogue according to the selected format. Chris@0: * Chris@0: * @param MessageCatalogue $catalogue The message catalogue to dump Chris@0: * @param string $format The format to use to dump the messages Chris@0: * @param array $options Options that are passed to the dumper Chris@0: * Chris@0: * @throws InvalidArgumentException Chris@0: */ Chris@0: public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array()) Chris@0: { Chris@0: if (!isset($this->dumpers[$format])) { Chris@0: throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format)); Chris@0: } Chris@0: Chris@0: // get the right dumper Chris@0: $dumper = $this->dumpers[$format]; Chris@0: Chris@0: if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) { Chris@0: throw new RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path'])); Chris@0: } Chris@0: Chris@0: // save Chris@0: $dumper->dump($catalogue, $options); Chris@0: } Chris@0: }