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