|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Compiler; |
|
6
|
|
|
|
|
7
|
|
|
use Ray\Di\AbstractModule; |
|
8
|
|
|
use Ray\Di\Container; |
|
9
|
|
|
use Ray\Di\Injector; |
|
10
|
|
|
use Ray\Di\InjectorInterface; |
|
11
|
|
|
use Ray\Di\Name; |
|
12
|
|
|
|
|
13
|
|
|
final class DiCompiler implements InjectorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $scriptDir; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Container |
|
22
|
|
|
*/ |
|
23
|
|
|
private $container; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var DependencyCode |
|
27
|
|
|
*/ |
|
28
|
|
|
private $dependencyCompiler; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Injector |
|
32
|
|
|
*/ |
|
33
|
|
|
private $injector; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var null|AbstractModule |
|
37
|
|
|
*/ |
|
38
|
|
|
private $module; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var DependencySaver |
|
42
|
|
|
*/ |
|
43
|
|
|
private $dependencySaver; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var FilePutContents |
|
47
|
|
|
*/ |
|
48
|
|
|
private $filePutContents; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct(AbstractModule $module = null, string $scriptDir = '') |
|
51
|
|
|
{ |
|
52
|
|
|
$this->scriptDir = $scriptDir ?: \sys_get_temp_dir(); |
|
53
|
|
|
$this->container = $module ? $module->getContainer() : new Container; |
|
54
|
|
|
$this->injector = new Injector($module, $scriptDir); |
|
55
|
|
|
$this->dependencyCompiler = new DependencyCode($this->container); |
|
56
|
|
|
$this->module = $module; |
|
57
|
|
|
$this->dependencySaver = new DependencySaver($scriptDir); |
|
58
|
|
|
$this->filePutContents = new FilePutContents; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getInstance($interface, $name = Name::ANY) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->compile(); |
|
67
|
|
|
|
|
68
|
|
|
return (new ScriptInjector($this->scriptDir))->getInstance($interface, $name); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Compile all dependencies in container |
|
73
|
|
|
*/ |
|
74
|
|
|
public function compile() : void |
|
75
|
|
|
{ |
|
76
|
|
|
$container = $this->container->getContainer(); |
|
77
|
|
|
foreach ($container as $dependencyIndex => $dependency) { |
|
78
|
|
|
$code = $this->dependencyCompiler->getCode($dependency); |
|
79
|
|
|
($this->dependencySaver)($dependencyIndex, $code); |
|
80
|
|
|
} |
|
81
|
|
|
$this->savePointcuts($this->container); |
|
82
|
|
|
($this->filePutContents)($this->scriptDir . ScriptInjector::MODULE, \serialize($this->module)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function dumpGraph() : void |
|
86
|
|
|
{ |
|
87
|
|
|
$dumper = new GraphDumper($this->container, $this->scriptDir); |
|
88
|
|
|
$dumper(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function savePointcuts(Container $container) : void |
|
92
|
|
|
{ |
|
93
|
|
|
$ref = (new \ReflectionProperty($container, 'pointcuts')); |
|
94
|
|
|
$ref->setAccessible(true); |
|
95
|
|
|
$pointcuts = $ref->getValue($container); |
|
96
|
|
|
($this->filePutContents)($this->scriptDir . ScriptInjector::AOP, \serialize($pointcuts)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|