1: <?php
2: /**
3: * This file is part of the Autarky package.
4: *
5: * (c) Andreas Lutro <anlutro@gmail.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:
11: namespace Autarky\Config\Loaders;
12:
13: use Symfony\Component\Yaml\Parser;
14: use Symfony\Component\Yaml\Exception\ParseException;
15:
16: use Autarky\Config\LoadException;
17: use Autarky\Config\LoaderInterface;
18: use Autarky\Files\LockingFilesystem;
19:
20: /**
21: * YAML/YML config file loader.
22: */
23: class YamlFileLoader implements LoaderInterface
24: {
25: /**
26: * The symfony YAML parser instance.
27: *
28: * @var Parser
29: */
30: protected $parser;
31:
32: /**
33: * The filesystem instance.
34: *
35: * @var LockingFilesystem
36: */
37: protected $filesys;
38:
39: /**
40: * @param Parser $parser
41: */
42: public function __construct(Parser $parser)
43: {
44: $this->parser = $parser;
45: $this->filesys = new LockingFilesystem;
46: }
47:
48: /**
49: * {@inheritdoc}
50: */
51: public function load($path)
52: {
53: $yaml = $this->filesys->read($path);
54:
55: try {
56: return $this->parser->parse($yaml);
57: } catch (ParseException $e) {
58: throw new LoadException($e->getMessage(), $e->getCode(), $e);
59: }
60: }
61: }
62: