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: $extra = implode(", $name.", array_keys($extra));
69: throw new Nette\InvalidStateException("Unknown configuration option $name.$extra.");
70: }
71: return Config\Helpers::merge($config, $expected);
72: }
73:
74:
75: 76: 77:
78: public function getContainerBuilder()
79: {
80: return $this->compiler->getContainerBuilder();
81: }
82:
83:
84: 85: 86: 87: 88:
89: public function loadFromFile($file)
90: {
91: $loader = new Config\Loader;
92: $res = $loader->load($file);
93: $this->compiler->addDependencies($loader->getDependencies());
94: return $res;
95: }
96:
97:
98: 99: 100: 101: 102:
103: public function prefix($id)
104: {
105: return substr_replace($id, $this->name . '.', substr($id, 0, 1) === '@' ? 1 : 0, 0);
106: }
107:
108:
109: 110: 111: 112:
113: public function loadConfiguration()
114: {
115: }
116:
117:
118: 119: 120: 121:
122: public function beforeCompile()
123: {
124: }
125:
126:
127: 128: 129: 130:
131: public function afterCompile(Nette\PhpGenerator\ClassType $class)
132: {
133: }
134:
135: }
136: