2015-08-17 17:00:26 -07: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\Loader ;
use Symfony\Component\Translation\Exception\InvalidResourceException ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\Translation\Exception\LogicException ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\Yaml\Exception\ParseException ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\Yaml\Parser as YamlParser ;
2015-08-17 17:00:26 -07:00
/**
* YamlFileLoader loads translations from Yaml files .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
2016-04-20 09:56:34 -07:00
class YamlFileLoader extends FileLoader
2015-08-17 17:00:26 -07:00
{
private $yamlParser ;
/**
* { @ inheritdoc }
*/
2016-04-20 09:56:34 -07:00
protected function loadResource ( $resource )
2015-08-17 17:00:26 -07:00
{
if ( null === $this -> yamlParser ) {
2016-04-20 09:56:34 -07:00
if ( ! class_exists ( 'Symfony\Component\Yaml\Parser' )) {
2018-11-23 12:29:20 +00:00
throw new LogicException ( 'Loading translations from the YAML format requires the Symfony Yaml component.' );
2016-04-20 09:56:34 -07:00
}
2015-08-17 17:00:26 -07:00
$this -> yamlParser = new YamlParser ();
}
2018-11-23 12:29:20 +00:00
$prevErrorHandler = set_error_handler ( function ( $level , $message , $script , $line ) use ( $resource , & $prevErrorHandler ) {
$message = E_USER_DEPRECATED === $level ? preg_replace ( '/ on line \d+/' , ' in "' . $resource . '"$0' , $message ) : $message ;
return $prevErrorHandler ? $prevErrorHandler ( $level , $message , $script , $line ) : false ;
});
2015-08-17 17:00:26 -07:00
try {
2018-11-23 12:29:20 +00:00
$messages = $this -> yamlParser -> parseFile ( $resource );
2015-08-17 17:00:26 -07:00
} catch ( ParseException $e ) {
throw new InvalidResourceException ( sprintf ( 'Error parsing YAML, invalid file "%s"' , $resource ), 0 , $e );
2018-11-23 12:29:20 +00:00
} finally {
restore_error_handler ();
2015-08-17 17:00:26 -07:00
}
2016-04-20 09:56:34 -07:00
return $messages ;
2015-08-17 17:00:26 -07:00
}
}