Passed
Push — master ( ba4ca1...142814 )
by Evgeniy
58s queued 11s
created

Compiler::compileAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI;
6
7
use Cekta\DI\Loader\Alias;
8
use Cekta\DI\Loader\Factory;
9
use Cekta\DI\Loader\FactoryVariadic;
10
11
class Compiler
12
{
13
    /**
14
     * @var Reflection
15
     */
16
    private $reflection;
17
    private $classes = [];
18
    private $alias = [];
19
    private $variadic = [];
20
21 15
    public function __construct(Reflection $reflection)
22
    {
23 15
        $this->reflection = $reflection;
24 15
    }
25
26 9
    public function autowire(string $name): self
27
    {
28 9
        if (!$this->reflection->isInstantiable($name)) {
29 3
            return $this;
30
        }
31 6
        if ($this->reflection->isVariadic($name)) {
32 3
            $this->variadic[] = true;
33
        }
34 6
        return $this->registerClass(
35 6
            $name,
36 6
            ...$this->reflection->getDependencies($name)
37
        );
38
    }
39
40 9
    public function registerClass(string $name, string ...$dependencies): self
41
    {
42 9
        $this->classes[$name] = $dependencies;
43 9
        return $this;
44
    }
45
46 3
    public function registerInterface(string $name, string $implementation): self
47
    {
48 3
        $this->alias[$name] = $implementation;
49 3
        return $this;
50
    }
51
52 15
    public function compile(): string
53
    {
54 5
        return "<?php
55
56
declare(strict_types=1);
57
58 15
return [{$this->compileAlias()}{$this->compileClasses()}
59
];";
60
    }
61
62 15
    private function compileAlias(): string
63
    {
64 15
        $compiledContainers = '';
65 15
        $class = Alias::class;
66 15
        foreach ($this->alias as $name => $implementation) {
67 3
            $compiledContainers .= "\n    '$name' => new $class('$implementation'),";
68
        }
69 15
        return $compiledContainers;
70
    }
71
72 15
    private function compileClasses(): string
73
    {
74 15
        $compiledContainers = '';
75 15
        foreach ($this->classes as $name => $dependencies) {
76 9
            $class = $this->getClass($name);
77
            $compiledContainers .= <<<TAG
78
79 9
    '$name' => new $class(
80 9
        '$name',{$this->getDependenciesString(...$dependencies)}
81
    ),
82
TAG;
83
        }
84 15
        return $compiledContainers;
85
    }
86
87 9
    private function getClass($name): string
88
    {
89 9
        if (in_array($name, $this->variadic)) {
90 3
            return FactoryVariadic::class;
91
        }
92 6
        return Factory::class;
93
    }
94
95 9
    private function getDependenciesString(string ...$dependencies): string
96
    {
97 9
        $result = '';
98 9
        foreach ($dependencies as $dependency) {
99 9
            $result .= PHP_EOL . "        '$dependency',";
100
        }
101 9
        return $result;
102
    }
103
}
104