Total Complexity | 9 |
Total Lines | 59 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
10 | class Compiler |
||
11 | { |
||
12 | /** |
||
13 | * @var Reflection |
||
14 | */ |
||
15 | private $reflection; |
||
16 | private $classes = []; |
||
17 | private $variadic = []; |
||
18 | |||
19 | 9 | public function __construct(Reflection $reflection) |
|
20 | { |
||
21 | 9 | $this->reflection = $reflection; |
|
22 | 9 | } |
|
23 | |||
24 | 9 | public function autowire(string $name): self |
|
25 | { |
||
26 | 9 | if (!$this->reflection->isInstantiable($name)) { |
|
27 | 3 | return $this; |
|
28 | } |
||
29 | 6 | if ($this->reflection->isVariadic($name)) { |
|
30 | 3 | $this->variadic[] = $name; |
|
31 | } |
||
32 | 6 | $this->classes[$name] = $this->reflection->getDependencies($name); |
|
33 | 6 | return $this; |
|
34 | } |
||
35 | |||
36 | 9 | public function compile(): string |
|
43 | ];"; |
||
44 | } |
||
45 | |||
46 | 9 | private function compileClasses(): string |
|
47 | { |
||
48 | 9 | $compiledContainers = ''; |
|
49 | 9 | foreach ($this->classes as $name => $dependencies) { |
|
50 | 6 | $class = $this->getClass($name); |
|
51 | 6 | $dependenciesString = str_replace("\n", "\n ", var_export($dependencies, true)); |
|
52 | $compiledContainers .= <<<TAG |
||
53 | |||
54 | 6 | '$name' => new $class( |
|
55 | 6 | '$name', |
|
56 | 6 | ...$dependenciesString |
|
57 | ), |
||
58 | TAG; |
||
59 | } |
||
60 | 9 | return $compiledContainers; |
|
61 | } |
||
62 | |||
63 | 6 | private function getClass($name): string |
|
69 | } |
||
70 | } |
||
71 |