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