1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\DI;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: abstract class CompilerExtension extends Nette\Object
19: {
20:
21: protected $compiler;
22:
23:
24: protected $name;
25:
26:
27: protected $config = array();
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: $extra = implode(", $name.", array_keys($extra));
71: throw new Nette\InvalidStateException("Unknown configuration option $name.$extra.");
72: }
73: return Config\Helpers::merge($config, $expected);
74: }
75:
76:
77: 78: 79:
80: public function getContainerBuilder()
81: {
82: return $this->compiler->getContainerBuilder();
83: }
84:
85:
86: 87: 88: 89: 90:
91: public function loadFromFile($file)
92: {
93: $loader = new Config\Loader;
94: $res = $loader->load($file);
95: $this->compiler->addDependencies($loader->getDependencies());
96: return $res;
97: }
98:
99:
100: 101: 102: 103: 104:
105: public function prefix($id)
106: {
107: return substr_replace($id, $this->name . '.', substr($id, 0, 1) === '@' ? 1 : 0, 0);
108: }
109:
110:
111: 112: 113: 114:
115: public function loadConfiguration()
116: {
117: }
118:
119:
120: 121: 122: 123:
124: public function beforeCompile()
125: {
126: }
127:
128:
129: 130: 131: 132:
133: public function afterCompile(Nette\PhpGenerator\ClassType $class)
134: {
135: }
136:
137: }
138: