Completed
Push — master ( fb6bbb...5c4144 )
by Théo
7s
created

AbstractExtension::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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 Symfony\Component\Config\FileLocator;
18
19
/**
20
 * @author Théo FIDRY <[email protected]>
21
 */
22
abstract class AbstractExtension implements ExtensionInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @return YamlFileLoader
28
     */
29
    public function load(ContainerBuilder $container)
30
    {
31
        $resourcePath = (function_exists('resource_path'))
32
            ? resource_path('providers')
33
            : app('path').DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'providers'
34
        ;
35
36
        $rootDir = new FileLocator($resourcePath);
37
        $loader = new YamlFileLoader($container, $rootDir);
38
39
        return $loader;
40
    }
41
42
    /**
43
     * @param FileLoaderInterface $loader
44
     * @param string              $resource
45
     *
46
     * @return $this
47
     */
48
    protected function loadResourceIfExist(FileLoaderInterface $loader, $resource)
49
    {
50
        try {
51
            $loader->load($resource);
52
        } catch (\InvalidArgumentException $exception) {
53
            // Ignore error as is an optional file
54
        }
55
56
        return $this;
57
    }
58
}
59