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: 31: 32:
33: public function setCompiler(Compiler $compiler, $name)
34: {
35: $this->compiler = $compiler;
36: $this->name = $name;
37: return $this;
38: }
39:
40:
41: 42: 43:
44: public function setConfig(array $config)
45: {
46: $this->config = $config;
47: return $this;
48: }
49:
50:
51: 52: 53: 54:
55: public function getConfig()
56: {
57: if (func_num_args()) {
58: return Config\Helpers::merge($this->config, $this->getContainerBuilder()->expand(func_get_arg(0)));
59: }
60: return $this->config;
61: }
62:
63:
64: 65: 66: 67: 68:
69: public function validateConfig(array $expected, array $config = null, $name = null)
70: {
71: if (func_num_args() === 1) {
72: return $this->config = $this->validateConfig($expected, $this->config);
73: }
74: if ($extra = array_diff_key((array) $config, $expected)) {
75: $name = $name ?: $this->name;
76: $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($expected), key($extra));
77: $extra = $hint ? key($extra) : implode(", $name.", array_keys($extra));
78: throw new Nette\InvalidStateException("Unknown configuration option $name.$extra" . ($hint ? ", did you mean $name.$hint?" : '.'));
79: }
80: return Config\Helpers::merge($config, $expected);
81: }
82:
83:
84: 85: 86:
87: public function getContainerBuilder()
88: {
89: return $this->compiler->getContainerBuilder();
90: }
91:
92:
93: 94: 95: 96: 97:
98: public function loadFromFile($file)
99: {
100: $loader = new Config\Loader;
101: $res = $loader->load($file);
102: $this->compiler->addDependencies($loader->getDependencies());
103: return $res;
104: }
105:
106:
107: 108: 109: 110: 111:
112: public function prefix($id)
113: {
114: return substr_replace($id, $this->name . '.', substr($id, 0, 1) === '@' ? 1 : 0, 0);
115: }
116:
117:
118: 119: 120: 121:
122: public function loadConfiguration()
123: {
124: }
125:
126:
127: 128: 129: 130:
131: public function beforeCompile()
132: {
133: }
134:
135:
136: 137: 138: 139:
140: public function afterCompile(Nette\PhpGenerator\ClassType $class)
141: {
142: }
143: }
144: