YamlFileLoader.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Loader;
  11. use Symfony\Component\Translation\Exception\InvalidResourceException;
  12. use Symfony\Component\Yaml\Parser as YamlParser;
  13. use Symfony\Component\Yaml\Exception\ParseException;
  14. /**
  15. * YamlFileLoader loads translations from Yaml files.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class YamlFileLoader extends FileLoader
  20. {
  21. private $yamlParser;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function loadResource($resource)
  26. {
  27. if (null === $this->yamlParser) {
  28. if (!class_exists('Symfony\Component\Yaml\Parser')) {
  29. throw new \LogicException('Loading translations from the YAML format requires the Symfony Yaml component.');
  30. }
  31. $this->yamlParser = new YamlParser();
  32. }
  33. try {
  34. $messages = $this->yamlParser->parse(file_get_contents($resource));
  35. } catch (ParseException $e) {
  36. throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e);
  37. }
  38. return $messages;
  39. }
  40. }