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\Dumper\DumperInterface; 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: * TranslationWriter writes translation messages. Chris@0: * Chris@0: * @author Michel Salib Chris@0: */ Chris@14: class TranslationWriter implements TranslationWriterInterface Chris@0: { Chris@17: private $dumpers = []; 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@14: * @param MessageCatalogue $catalogue The message catalogue to write 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@17: public function write(MessageCatalogue $catalogue, $format, $options = []) 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@14: Chris@14: /** Chris@14: * Writes translation from the catalogue according to the selected format. Chris@14: * Chris@14: * @param MessageCatalogue $catalogue The message catalogue to write Chris@14: * @param string $format The format to use to dump the messages Chris@14: * @param array $options Options that are passed to the dumper Chris@14: * Chris@14: * @throws InvalidArgumentException Chris@14: * Chris@14: * @deprecated since 3.4 will be removed in 4.0. Use write instead. Chris@14: */ Chris@17: public function writeTranslations(MessageCatalogue $catalogue, $format, $options = []) Chris@14: { Chris@17: @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0. Use write() instead.', __METHOD__), E_USER_DEPRECATED); Chris@14: $this->write($catalogue, $format, $options); Chris@14: } Chris@0: }