2015-08-18 00:00:26 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Translation\Writer ;
use Symfony\Component\Translation\Dumper\DumperInterface ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\Translation\Exception\InvalidArgumentException ;
use Symfony\Component\Translation\Exception\RuntimeException ;
use Symfony\Component\Translation\MessageCatalogue ;
2015-08-18 00:00:26 +00:00
/**
* TranslationWriter writes translation messages .
*
* @ author Michel Salib < michelsalib @ hotmail . com >
*/
2018-11-23 12:29:20 +00:00
class TranslationWriter implements TranslationWriterInterface
2015-08-18 00:00:26 +00:00
{
private $dumpers = array ();
/**
* Adds a dumper to the writer .
*
* @ param string $format The format of the dumper
* @ param DumperInterface $dumper The dumper
*/
public function addDumper ( $format , DumperInterface $dumper )
{
$this -> dumpers [ $format ] = $dumper ;
}
/**
* Disables dumper backup .
*/
public function disableBackup ()
{
foreach ( $this -> dumpers as $dumper ) {
2016-04-20 16:56:34 +00:00
if ( method_exists ( $dumper , 'setBackup' )) {
$dumper -> setBackup ( false );
}
2015-08-18 00:00:26 +00:00
}
}
/**
* Obtains the list of supported formats .
*
* @ return array
*/
public function getFormats ()
{
return array_keys ( $this -> dumpers );
}
/**
* Writes translation from the catalogue according to the selected format .
*
2018-11-23 12:29:20 +00:00
* @ param MessageCatalogue $catalogue The message catalogue to write
2015-08-18 00:00:26 +00:00
* @ param string $format The format to use to dump the messages
* @ param array $options Options that are passed to the dumper
*
2018-11-23 12:29:20 +00:00
* @ throws InvalidArgumentException
2015-08-18 00:00:26 +00:00
*/
2018-11-23 12:29:20 +00:00
public function write ( MessageCatalogue $catalogue , $format , $options = array ())
2015-08-18 00:00:26 +00:00
{
if ( ! isset ( $this -> dumpers [ $format ])) {
2018-11-23 12:29:20 +00:00
throw new InvalidArgumentException ( sprintf ( 'There is no dumper associated with format "%s".' , $format ));
2015-08-18 00:00:26 +00:00
}
// get the right dumper
$dumper = $this -> dumpers [ $format ];
2016-04-20 16:56:34 +00:00
if ( isset ( $options [ 'path' ]) && ! is_dir ( $options [ 'path' ]) && !@ mkdir ( $options [ 'path' ], 0777 , true ) && ! is_dir ( $options [ 'path' ])) {
2018-11-23 12:29:20 +00:00
throw new RuntimeException ( sprintf ( 'Translation Writer was not able to create directory "%s"' , $options [ 'path' ]));
2015-08-18 00:00:26 +00:00
}
// save
$dumper -> dump ( $catalogue , $options );
}
2018-11-23 12:29:20 +00:00
/**
* Writes translation from the catalogue according to the selected format .
*
* @ param MessageCatalogue $catalogue The message catalogue to write
* @ param string $format The format to use to dump the messages
* @ param array $options Options that are passed to the dumper
*
* @ throws InvalidArgumentException
*
* @ deprecated since 3.4 will be removed in 4.0 . Use write instead .
*/
public function writeTranslations ( MessageCatalogue $catalogue , $format , $options = array ())
{
@ 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 );
$this -> write ( $catalogue , $format , $options );
}
2015-08-18 00:00:26 +00:00
}