AbstractExtensionProvider::loadExtensions()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
ccs 0
cts 21
cp 0
rs 8.5806
cc 4
eloc 16
nc 4
nop 0
crap 20
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\Provider;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder;
15
use Fidry\LaravelYaml\DependencyInjection\Extension\ExtensionInterface;
16
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
17
18
/**
19
 * @author Théo FIDRY <[email protected]>
20
 */
21
abstract class AbstractExtensionProvider extends IlluminateServiceProvider implements ProviderInterface
22
{
23
    /**
24
     * @var ExtensionInterface[]
25
     */
26
    private $extensions = [];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    final public function register()
32
    {
33
        $this->loadExtensions();
34
35
        $container = new ContainerBuilder();
36
        foreach ($this->extensions as $extension) {
37
            $extension->load($container);
38
        }
39
40
        $container->build($this->app);
41
    }
42
43
    /**
44
     * Loads all the extensions registered by the user.
45
     */
46
    private function loadExtensions()
47
    {
48
        $extensionsClassNames = array_flip($this->getExtensions());
49
50
        foreach ($extensionsClassNames as $extensionClassName => $null) {
51
            if (false === class_exists($extensionClassName)) {
52
                throw new \InvalidArgumentException(
53
                    sprintf(
54
                        'Unable to load extension "%s": extension not found',
55
                        $extensionClassName
56
                    )
57
                );
58
            }
59
60
            $extension = new $extensionClassName();
61
            if (false === $extension instanceof ExtensionInterface) {
62
                throw new \LogicException(
63
                    sprintf(
64
                        'Extension "%s" must implement the interface "%s"',
65
                        get_class($extension),
66
                        ExtensionInterface::class
67
                    )
68
                );
69
            }
70
71
            $this->extensions[] = $extension;
72
        }
73
    }
74
}
75