Completed
Pull Request — master (#3)
by Jacob
02:17
created

ServiceLoaderManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace As3\Bundle\ModlrBundle\DependencyInjection;
4
5
use As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader\ServiceLoaderInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
8
/**
9
 * Loads services for the Modlr bundle
10
 *
11
 * @author  Jacob Bare <[email protected]>
12
 */
13
class ServiceLoaderManager
14
{
15
    /**
16
     * @var ContainerBuilder
17
     */
18
    private $container;
19
20
    /**
21
     * @var ServiceLoaderInterface[]
22
     */
23
    private $loaders = [];
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param   ContainerBuilder    $container
29
     */
30
    public function __construct(ContainerBuilder $container)
31
    {
32
        $this->container = $container;
33
        $this->initLoaders();
34
    }
35
36
    /**
37
     * Adds a service loader.
38
     *
39
     * @param   ServiceLoaderInterface  $loader
40
     * @return  self
41
     */
42
    public function addLoader(ServiceLoaderInterface $loader)
43
    {
44
        $this->loaders[] = $loader;
45
        return $this;
46
    }
47
48
    /**
49
     * Initializes all service loaders.
50
     *
51
     * @return  self
52
     */
53
    private function initLoaders()
54
    {
55
        $namespace = sprintf('%s\\ServiceLoader', __NAMESPACE__);
56
        $classes = ['MetadataCache', 'MetadataDrivers', 'MetadataFactory', 'Persisters', 'Rest', 'SearchClients'];
57
58
        foreach ($classes as $class) {
59
            $fqcn = sprintf('%s\\%s', $namespace, $class);
60
            $this->addLoader(new $fqcn);
61
        }
62
        return $this;
63
    }
64
65
    /**
66
     * Loads services from all loaders.
67
     *
68
     * @param   array   $config
69
     * @return  self
70
     */
71
    public function loadServices(array $config)
72
    {
73
        foreach ($this->loaders as $loader) {
74
            $loader->load($config, $this->container);
75
        }
76
        return $this;
77
    }
78
}
79