Completed
Push — master ( e9d1ee...cb5c09 )
by Théo
06:38
created

DefaultExtension::loadResourceIfExist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
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\DependencyInjection\Extension;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder;
15
use Fidry\LaravelYaml\FileLoader\FileLoaderInterface;
16
use Fidry\LaravelYaml\FileLoader\Yaml\YamlFileLoader;
17
use Illuminate\Support\Facades\App;
18
use Symfony\Component\Config\FileLocator;
19
20
/**
21
 * @author Théo FIDRY <[email protected]>
22
 */
23
final class DefaultExtension implements ExtensionInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function load(ContainerBuilder $container)
29
    {
30
        $rootDir = new FileLocator(resource_path('providers'));
31
        $loader = new YamlFileLoader($container, $rootDir);
32
33
        $this
34
            ->loadResourceIfExist($loader, 'parameters.yml')
35
            ->loadResourceIfExist($loader, 'services.yml')
36
            ->loadResourceIfExist($loader, sprintf('parameters_%s.yml', App::environment()))
37
        ;
38
    }
39
40
    /**
41
     * @param FileLoaderInterface $loader
42
     * @param string              $resource
43
     *
44
     * @return $this
45
     */
46
    private function loadResourceIfExist(FileLoaderInterface $loader, $resource)
47
    {
48
        try {
49
            $loader->load($resource);
50
        } catch (\InvalidArgumentException $exception) {
51
            // Ignore error as is an optional file
52
        }
53
54
        return $this;
55
    }
56
}
57