|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* For licensing information, please see the LICENSE file accompanied with this file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Gerard van Helden <[email protected]> |
|
6
|
|
|
* @copyright 2012 Gerard van Helden <http://melp.nl> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Zicht\Tool\Container; |
|
10
|
|
|
|
|
11
|
|
|
use Zicht\Tool\Script\Buffer; |
|
12
|
|
|
use Zicht\Tool\PluginInterface; |
|
13
|
|
|
use Zicht\Util\Mutex; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Compiler to compile the entire container into PHP code. |
|
17
|
|
|
*/ |
|
18
|
|
|
class ContainerCompiler |
|
19
|
|
|
{ |
|
20
|
|
|
private $configTree; |
|
21
|
|
|
private $plugins; |
|
22
|
|
|
private $file; |
|
23
|
|
|
private $code; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Construct the compiler |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $configTree |
|
30
|
|
|
* @param PluginInterface[] $plugins |
|
31
|
|
|
* @param null $file |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($configTree, $plugins, $file) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->configTree = $configTree; |
|
36
|
|
|
$this->plugins = $plugins; |
|
37
|
|
|
$this->file = $file; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Writes the code to a temporary file and returns the resulting Container object. |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \LogicException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getContainer() |
|
49
|
|
|
{ |
|
50
|
|
|
$mutex = new Mutex($this->file . '.lock', true); |
|
51
|
|
|
|
|
52
|
|
|
$ret = $mutex->run( |
|
53
|
|
|
function () { |
|
54
|
|
|
if ($this->needsRecompile()) { |
|
55
|
|
|
file_put_contents($this->file, $this->compileContainerCode()); |
|
56
|
|
|
} |
|
57
|
|
|
return include $this->file; |
|
58
|
|
|
} |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
if (!($ret instanceof Container)) { |
|
62
|
|
|
throw new \LogicException("The container must be returned by the compiler"); |
|
63
|
|
|
} |
|
64
|
|
|
foreach ($this->plugins as $plugin) { |
|
65
|
|
|
$ret->addPlugin($plugin); |
|
66
|
|
|
} |
|
67
|
|
|
return $ret; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add a plugin |
|
73
|
|
|
* |
|
74
|
|
|
* @param \Zicht\Tool\PluginInterface $p |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function addPlugin(PluginInterface $p) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->plugins[] = $p; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Crude check for whether a recompile is needed. |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function needsRecompile() |
|
89
|
|
|
{ |
|
90
|
|
|
clearstatcache(); |
|
91
|
|
|
return !is_file($this->file) || ( |
|
92
|
|
|
(!empty($this->configTree['z']['sources']) |
|
93
|
|
|
&& max(array_map('filemtime', $this->configTree['z']['sources'])) >= filemtime($this->file)) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the code for initializing the container. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function compileContainerCode() |
|
104
|
|
|
{ |
|
105
|
|
|
$builder = new ContainerBuilder($this->configTree); |
|
106
|
|
|
foreach ($this->plugins as $name => $plugin) { |
|
107
|
|
|
$plugin->setContainerBuilder($builder); |
|
108
|
|
|
} |
|
109
|
|
|
$containerNode = $builder->build(); |
|
110
|
|
|
$buffer = new Buffer(); |
|
111
|
|
|
$buffer->write('<?php')->eol(); |
|
112
|
|
|
$containerNode->compile($buffer); |
|
113
|
|
|
$buffer->writeln('return $z;'); |
|
114
|
|
|
$code = $buffer->getResult(); |
|
115
|
|
|
return $code; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.