| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\Exception\InvalidResourceException; |
| 15 |
use Symfony\Component\Translation\Exception\LogicException; |
| 16 |
use Symfony\Component\Yaml\Exception\ParseException; |
| 17 |
use Symfony\Component\Yaml\Parser as YamlParser; |
| 18 |
use Symfony\Component\Yaml\Yaml; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class YamlFileLoader extends FileLoader |
| 26 |
{ |
| 27 |
private YamlParser $yamlParser; |
| 28 |
|
| 29 |
protected function loadResource(string $resource): array |
| 30 |
{ |
| 31 |
if (!isset($this->yamlParser)) { |
| 32 |
if (!class_exists(YamlParser::class)) { |
| 33 |
throw new LogicException('Loading translations from the YAML format requires the Symfony Yaml component.'); |
| 34 |
} |
| 35 |
|
| 36 |
$this->yamlParser = new YamlParser(); |
| 37 |
} |
| 38 |
|
| 39 |
try { |
| 40 |
$messages = $this->yamlParser->parseFile($resource, Yaml::PARSE_CONSTANT); |
| 41 |
} catch (ParseException $e) { |
| 42 |
throw new InvalidResourceException(\sprintf('The file "%s" does not contain valid YAML: ', $resource).$e->getMessage(), 0, $e); |
| 43 |
} |
| 44 |
|
| 45 |
if (null !== $messages && !\is_array($messages)) { |
| 46 |
throw new InvalidResourceException(\sprintf('Unable to load file "%s".', $resource)); |
| 47 |
} |
| 48 |
|
| 49 |
return $messages ?: []; |
| 50 |
} |
| 51 |
} |