Completed
Branch develop (9f43d2)
by Filipe
05:14
created

ContainerBuilder::checkDefinitions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 9
nc 3
nop 1
crap 4
1
<?php
2
3
/**
4
 * This file is part of slick/di package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Di;
11
12
use Interop\Container\ContainerInterface as InteropContainer;
13
use Slick\Di\Exception\InvalidDefinitionsPathException;
14
15
/**
16
 * Container Builder
17
 *
18
 * @package Slick\Di
19
 * @author  Filipe Silva <[email protected]>
20
*/
21
final class ContainerBuilder implements ContainerAwareInterface
22
{
23
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    /**
30
     * @var array|string
31
     */
32
    private $definitions;
33
34
    public function __construct($definitions = [])
35
    {
36
        $this->definitions = $definitions;
37
    }
38
39
    /**
40
     * Get current container
41
     *
42
     * If no container was created a new, empty container will be created.
43
     *
44
     * @return ContainerInterface|Container
45
     */
46
    public function getContainer()
47
    {
48
        if (!$this->container) {
49
            $this->setContainer(new Container());
50
        }
51
        $this->hydrateContainer($this->definitions);
52
        return $this->container;
53
    }
54
55
56
    /**
57
     * Set the dependency container
58 8
     *
59
     * @param InteropContainer $container
60 8
     *
61 8
     * @return ContainerBuilder
62 8
     */
63
    public function setContainer(InteropContainer $container)
64
    {
65
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
$container is of type object<Interop\Container\ContainerInterface>, but the property $container was declared to be of type object<Slick\Di\ContainerInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
66
        return $this;
67
    }
68
69 6
    /**
70
     * Hydrates the container with provided definitions
71 6
     *
72 6
     * @param string|array $definitions
73 6
     */
74 6
    protected function hydrateContainer($definitions)
75 6
    {
76
        if (! is_array($definitions)) {
77
            $this->hydrateFromFile($definitions);
78
            return;
79
        }
80
81 6
        foreach ($definitions as $name => $definition) {
82
            $this->container->register($name, $definition);
83 6
        }
84
    }
85 6
86 6
    /**
87 6
     * Hydrate the container with definitions from provided file
88 6
     *
89 6
     * @param $definitions
90
     */
91 6
    protected function hydrateFromFile($definitions)
92 2
    {
93 2
        if (! is_file($definitions)) {
94 6
            $this->hydrateFromDirectory($definitions);
95 6
            return;
96
        }
97
98
        $services = require $definitions;
99
        $this->hydrateContainer($services);
100
    }
101
102
    protected function hydrateFromDirectory($definitions)
103 8
    {
104
        try {
105 8
            $directory = new \RecursiveDirectoryIterator($definitions);
106 8
        } catch (\Exception $caught) {
107
            throw new InvalidDefinitionsPathException(
108
                'Provided definitions path is not valid or is not found. ' .
109 8
                'Could not create container. Please check '.$definitions
110 8
            );
111 8
        }
112
113
        $iterator = new \RecursiveIteratorIterator($directory);
114 2
        $phpFiles = new \RegexIterator($iterator, '/.*\.php$/i');
115
        foreach ($phpFiles as $phpFile) {
116
            $this->hydrateFromFile($phpFile);
117 2
        }
118
    }
119
}
120