Completed
Pull Request — master (#12)
by Théo
06:24 queued 03:54
created

YamlFileLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 2
b 1
f 1
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
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 Fidry\LaravelYaml\FileLoader\Yaml;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder;
15
use Fidry\LaravelYaml\Exception\FileLoader\Exception;
16
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
17
use Fidry\LaravelYaml\FileLoader\FileLoaderInterface;
18
use Fidry\LaravelYaml\FileLoader\Parser\ParserInterface;
19
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\DefinitionsParser;
20
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\ImportsParser;
21
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\ParametersParser;
22
use Symfony\Component\Config\FileLocatorInterface;
23
use Symfony\Component\Yaml\Exception\ParseException;
24
use Symfony\Component\Yaml\Parser as YamlParser;
25
26
/**
27
 * This loader is able to load YAML files. Parsed values are interpreted and added to the {@see ContainerBuilder} to be
28
 * loaded to the Application later on.
29
 *
30
 * @author Théo FIDRY <[email protected]>
31
 */
32
final class YamlFileLoader implements FileLoaderInterface
33
{
34
    /**
35
     * @var ContainerBuilder
36
     */
37
    private $container;
38
39
    /**
40
     * @var ParserInterface
41
     */
42
    private $definitionsParser;
43
44
    /**
45
     * @var FileLocatorInterface
46
     */
47
    private $fileLocator;
48
49
    /**
50
     * @var ParserInterface
51
     */
52
    private $importsParser;
53
54
    /**
55
     * @var ParserInterface
56
     */
57
    private $parametersParser;
58
59
    /**
60
     * @var YamlSingleFileLoader
61
     */
62
    private $singleFileLoader;
63
64 10
    public function __construct(
65
        ContainerBuilder $container,
66
        FileLocatorInterface $fileLocator,
67
        ParserInterface $definitionsParser = null,
68
        ParserInterface $parametersParser = null,
69
        ParserInterface $importsParser = null,
70
        YamlParser $yamlParser = null,
71
        YamlValidator $yamlValidator = null
72
    ) {
73 10
        $this->container = $container;
74 10
        $this->fileLocator = $fileLocator;
75
76 10
        $this->definitionsParser = (null === $definitionsParser) ? new DefinitionsParser() : $definitionsParser;
77 10
        $this->parametersParser = (null === $parametersParser) ? new ParametersParser() : $parametersParser;
78 10
        $this->importsParser = (null === $importsParser) ? new ImportsParser() : $importsParser;
79
80 10
        $yamlParser = (null === $yamlParser) ? new YamlParser() : $yamlParser;
81 10
        $yamlValidator = (null === $yamlValidator) ? new YamlValidator() : $yamlValidator;
82 10
        $this->singleFileLoader = new YamlSingleFileLoader($yamlParser, $yamlValidator);
83 10
    }
84
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @param string $resource file name
89
     *
90
     * @example
91
     *  ::load('services.yml')
92
     *
93
     * @throws InvalidArgumentException
94
     * @throws Exception
95
     * @return $this
96
     */
97 8
    public function load($resource)
98
    {
99
        /* @var string|null $path */
100 8
        $path = $this->fileLocator->locate($resource, null, true);
101 8
        $content = $this->singleFileLoader->loadFile($path);
102
103 4
        $imports = $this->importsParser->parse($this->container, $content, $resource);
104 4
        foreach ($imports as $importedResource) {
105 2
            $this->load($importedResource);
106 4
        }
107 4
        $this->parametersParser->parse($this->container, $content, $resource);
108 4
        $this->definitionsParser->parse($this->container, $content, $resource);
109
110 4
        return $this;
111
    }
112
}
113