1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\DI;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: abstract class CompilerExtension
17: {
18: use Nette\SmartObject;
19:
20:
21: protected $compiler;
22:
23:
24: protected $name;
25:
26:
27: protected $config = [];
28:
29:
30: public function setCompiler(Compiler $compiler, $name)
31: {
32: $this->compiler = $compiler;
33: $this->name = $name;
34: return $this;
35: }
36:
37:
38: public function setConfig(array $config)
39: {
40: $this->config = $config;
41: return $this;
42: }
43:
44:
45: 46: 47: 48:
49: public function getConfig()
50: {
51: if (func_num_args()) {
52: return Config\Helpers::merge($this->config, $this->getContainerBuilder()->expand(func_get_arg(0)));
53: }
54: return $this->config;
55: }
56:
57:
58: 59: 60: 61: 62:
63: public function validateConfig(array $expected, array $config = NULL, $name = NULL)
64: {
65: if (func_num_args() === 1) {
66: return $this->config = $this->validateConfig($expected, $this->config);
67: }
68: if ($extra = array_diff_key((array) $config, $expected)) {
69: $name = $name ?: $this->name;
70: $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($expected), key($extra));
71: $extra = $hint ? key($extra) : implode(", $name.", array_keys($extra));
72: throw new Nette\InvalidStateException("Unknown configuration option $name.$extra" . ($hint ? ", did you mean $name.$hint?" : '.'));
73: }
74: return Config\Helpers::merge($config, $expected);
75: }
76:
77:
78: 79: 80:
81: public function getContainerBuilder()
82: {
83: return $this->compiler->getContainerBuilder();
84: }
85:
86:
87: 88: 89: 90: 91:
92: public function loadFromFile($file)
93: {
94: $loader = new Config\Loader;
95: $res = $loader->load($file);
96: $this->compiler->addDependencies($loader->getDependencies());
97: return $res;
98: }
99:
100:
101: 102: 103: 104: 105:
106: public function prefix($id)
107: {
108: return substr_replace($id, $this->name . '.', substr($id, 0, 1) === '@' ? 1 : 0, 0);
109: }
110:
111:
112: 113: 114: 115:
116: public function loadConfiguration()
117: {
118: }
119:
120:
121: 122: 123: 124:
125: public function beforeCompile()
126: {
127: }
128:
129:
130: 131: 132: 133:
134: public function afterCompile(Nette\PhpGenerator\ClassType $class)
135: {
136: }
137:
138: }
139: