Passed
Push — master ( 5dce54...909d6e )
by Paweł
12:04 queued 12s
created

ContainerLoader::addDefinitions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace pjpawel\LightApi\Container;
4
5
use pjpawel\LightApi\Container\Definition\AliasDefinition;
6
use pjpawel\LightApi\Container\Definition\ClassDefinition;
7
use pjpawel\LightApi\Container\Definition\Definition;
8
use pjpawel\LightApi\Container\Definition\InterfaceDefinition;
9
use pjpawel\LightApi\Exception\ProgrammerException;
10
use Psr\Container\ContainerInterface;
11
12
class ContainerLoader implements ContainerInterface
13
{
14
15
    use ContainerTrait;
0 ignored issues
show
introduced by
The trait pjpawel\LightApi\Container\ContainerTrait requires some properties which are not provided by pjpawel\LightApi\Container\ContainerLoader: $name, $className, $arguments
Loading history...
16
17
    /**
18
     * @var array<string,Definition>
19
     */
20
    public array $definitions;
21
22
    /**
23
     * @param array<string, string|array> $config
24
     * @throws \Exception
25
     */
26
    public function createDefinitionsFromConfig(array $config): void
27
    {
28
        foreach ($config as $name => $value) {
29
            if (is_string($value) && str_starts_with($value, '@')) {
30
                $this->definitions[$name] = new AliasDefinition($name, substr($value, 1));
31
            } elseif (class_exists($name)) {
32
                $this->definitions[$name] = new ClassDefinition($name, $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string; however, parameter $arguments of pjpawel\LightApi\Contain...finition::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
                $this->definitions[$name] = new ClassDefinition($name, /** @scrutinizer ignore-type */ $value);
Loading history...
33
            } elseif (interface_exists($name)) {
34
                $this->definitions[$name] = new InterfaceDefinition($name, $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $className of pjpawel\LightApi\Contain...finition::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
                $this->definitions[$name] = new InterfaceDefinition($name, /** @scrutinizer ignore-type */ $value);
Loading history...
35
            } else {
36
                throw new ProgrammerException('Invalid container definition for name ' . $name);
37
            }
38
        }
39
    }
40
41
    /**
42
     * You must use name and args. Optionally you can provide object
43
     *
44
     * @param array $definition
45
     */
46
    public function add(array $definition): void
47
    {
48
        $newDefinition = new ClassDefinition($definition['name'], $definition['args'] ?? []);
49
        $newDefinition->object = $definition['object'] ?? null;
50
        $this->definitions[$definition['name']] = $newDefinition;
51
    }
52
53
    /**
54
     * @param array<string,Definition> $definitions
55
     * @return void
56
     */
57
    public function addDefinitions(array $definitions): void
58
    {
59
        foreach ($definitions as $id => $definition) {
60
            $this->definitions[$id] = $definition;
61
        }
62
    }
63
64
    /**
65
     * @param string[] $ids
66
     * @return Definition[]
67
     * @throws \pjpawel\LightApi\Container\ContainerNotFoundException
68
     */
69
    public function getDefinitions(array $ids): array
70
    {
71
        /** @var Definition[] $definitions */
72
        $definitions = [];
73
        foreach ($ids as $id) {
74
            if (!isset($this->definitions[$id])) {
75
                throw new ContainerNotFoundException();
76
            }
77
            $definitions[] = $this->definitions[$id];
78
        }
79
        return $definitions;
80
    }
81
82
    /**
83
     * @param array<string,string> $services
84
     * @return void
85
     */
86
    public function prepareContainerLocator(array $services): void
87
    {
88
        foreach ($services as $internalId => $serviceId) {
89
            $this->definitions[$internalId] = new AliasDefinition($internalId, $serviceId);
90
        }
91
    }
92
}