Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / translation / Loader / YamlFileLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
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\Parser as YamlParser;
17 use Symfony\Component\Yaml\Exception\ParseException;
18
19 /**
20  * YamlFileLoader loads translations from Yaml files.
21  *
22  * @author Fabien Potencier <fabien@symfony.com>
23  */
24 class YamlFileLoader extends FileLoader
25 {
26     private $yamlParser;
27
28     /**
29      * {@inheritdoc}
30      */
31     protected function loadResource($resource)
32     {
33         if (null === $this->yamlParser) {
34             if (!class_exists('Symfony\Component\Yaml\Parser')) {
35                 throw new LogicException('Loading translations from the YAML format requires the Symfony Yaml component.');
36             }
37
38             $this->yamlParser = new YamlParser();
39         }
40
41         $prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($resource, &$prevErrorHandler) {
42             $message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$resource.'"$0', $message) : $message;
43
44             return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
45         });
46
47         try {
48             $messages = $this->yamlParser->parseFile($resource);
49         } catch (ParseException $e) {
50             throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e);
51         } finally {
52             restore_error_handler();
53         }
54
55         return $messages;
56     }
57 }