1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Config;
13:
14: use Nette,
15: Nette\Utils\Validators;
16:
17:
18:
19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Compiler extends Nette\Object
29: {
30:
31: private $extensions = array();
32:
33:
34: private $container;
35:
36:
37: private $config;
38:
39:
40: private static $reserved = array('services' => 1, 'factories' => 1, 'parameters' => 1);
41:
42:
43:
44: 45: 46: 47:
48: public function addExtension($name, CompilerExtension $extension)
49: {
50: if (isset(self::$reserved[$name])) {
51: throw new Nette\InvalidArgumentException("Name '$name' is reserved.");
52: }
53: $this->extensions[$name] = $extension->setCompiler($this, $name);
54: return $this;
55: }
56:
57:
58:
59: 60: 61:
62: public function getExtensions()
63: {
64: return $this->extensions;
65: }
66:
67:
68:
69: 70: 71:
72: public function getContainerBuilder()
73: {
74: return $this->container;
75: }
76:
77:
78:
79: 80: 81: 82:
83: public function getConfig()
84: {
85: return $this->config;
86: }
87:
88:
89:
90: 91: 92:
93: public function compile(array $config, $className, $parentName)
94: {
95: $this->config = $config;
96: $this->container = new Nette\DI\ContainerBuilder;
97: $this->processParameters();
98: $this->processExtensions();
99: $this->processServices();
100: return $this->generateCode($className, $parentName);
101: }
102:
103:
104:
105: public function processParameters()
106: {
107: if (isset($this->config['parameters'])) {
108: $this->container->parameters = $this->config['parameters'];
109: }
110: }
111:
112:
113:
114: public function processExtensions()
115: {
116: if ($extra = array_diff_key($this->config, self::$reserved, $this->extensions)) {
117: $extra = implode("', '", array_keys($extra));
118: throw new Nette\InvalidStateException("Found sections '$extra' in configuration, but corresponding extensions are missing.");
119: }
120:
121: foreach ($this->extensions as $name => $extension) {
122: $extension->loadConfiguration();
123: }
124: }
125:
126:
127:
128: public function processServices()
129: {
130: $this->parseServices($this->container, $this->config);
131:
132: foreach ($this->extensions as $name => $extension) {
133: $this->container->addDefinition($name)
134: ->setClass('Nette\DI\NestedAccessor', array('@container', $name))
135: ->setAutowired(FALSE);
136:
137: if (isset($this->config[$name])) {
138: $this->parseServices($this->container, $this->config[$name], $name);
139: }
140: }
141:
142: foreach ($this->container->getDefinitions() as $name => $def) {
143: $factory = $name . 'Factory';
144: if (!$def->shared && !$def->internal && !$this->container->hasDefinition($factory)) {
145: $this->container->addDefinition($factory)
146: ->setClass('Nette\Callback', array('@container', 'create' . ucfirst($name)))
147: ->setAutowired(FALSE)
148: ->tags = $def->tags;
149: }
150: }
151: }
152:
153:
154:
155: public function generateCode($className, $parentName)
156: {
157: foreach ($this->extensions as $extension) {
158: $extension->beforeCompile();
159: }
160:
161: $classes[] = $class = $this->container->generateClass($parentName);
162: $class->setName($className)
163: ->addMethod('initialize');
164:
165: foreach ($this->extensions as $extension) {
166: $extension->afterCompile($class);
167: }
168:
169: $defs = $this->container->getDefinitions();
170: ksort($defs);
171: foreach (array_reverse($defs, TRUE) as $name => $def) {
172: if ($def->class === 'Nette\DI\NestedAccessor' && ($list = preg_grep('#^'.$name.'_#i', array_keys($defs)))) {
173: $def->class = $className . '_' . $name;
174: $class->documents = preg_replace("#\S+(?= \\$$name$)#", $def->class, $class->documents);
175: $classes[] = $accessor = new Nette\Utils\PhpGenerator\ClassType($def->class);
176: foreach ($list as $item) {
177: $short = substr($item, strlen($name) + 1);
178: $accessor->addDocument($defs[$item]->shared
179: ? "@property {$defs[$item]->class} \$$short"
180: : "@method {$defs[$item]->class} create" . ucfirst("$short()"));
181: }
182: }
183: }
184:
185: return implode("\n\n\n", $classes);
186: }
187:
188:
189:
190:
191:
192:
193:
194: 195: 196: 197:
198: public static function parseServices(Nette\DI\ContainerBuilder $container, array $config, $namespace = NULL)
199: {
200: $services = isset($config['services']) ? $config['services'] : array();
201: $factories = isset($config['factories']) ? $config['factories'] : array();
202: if ($tmp = array_intersect_key($services, $factories)) {
203: $tmp = implode("', '", array_keys($tmp));
204: throw new Nette\DI\ServiceCreationException("It is not allowed to use services and factories with the same names: '$tmp'.");
205: }
206:
207: $all = $services + $factories;
208: uasort($all, function($a, $b) {
209: return strcmp(Helpers::isInheriting($a), Helpers::isInheriting($b));
210: });
211:
212: foreach ($all as $name => $def) {
213: $shared = array_key_exists($name, $config['services']);
214: $name = ($namespace ? $namespace . '_' : '') . $name;
215:
216: if (($parent = Helpers::takeParent($def)) && $parent !== $name) {
217: $container->removeDefinition($name);
218: $definition = $container->addDefinition($name);
219: if ($parent !== Helpers::OVERWRITE) {
220: foreach ($container->getDefinition($parent) as $k => $v) {
221: $definition->$k = $v;
222: }
223: }
224: } elseif ($container->hasDefinition($name)) {
225: $definition = $container->getDefinition($name);
226: if ($definition->shared !== $shared) {
227: throw new Nette\DI\ServiceCreationException("It is not allowed to use service and factory with the same name '$name'.");
228: }
229: } else {
230: $definition = $container->addDefinition($name);
231: }
232: try {
233: static::parseService($definition, $def, $shared);
234: } catch (\Exception $e) {
235: throw new Nette\DI\ServiceCreationException("Service '$name': " . $e->getMessage(), NULL, $e);
236: }
237: }
238: }
239:
240:
241:
242: 243: 244: 245:
246: public static function parseService(Nette\DI\ServiceDefinition $definition, $config, $shared = TRUE)
247: {
248: if ($config === NULL) {
249: return;
250: } elseif (!is_array($config)) {
251: $config = array('class' => NULL, 'factory' => $config);
252: }
253:
254: $known = $shared
255: ? array('class', 'factory', 'arguments', 'setup', 'autowired', 'run', 'tags')
256: : array('class', 'factory', 'arguments', 'setup', 'tags', 'internal', 'parameters');
257:
258: if ($error = array_diff(array_keys($config), $known)) {
259: throw new Nette\InvalidStateException("Unknown key '" . implode("', '", $error) . "' in definition of service.");
260: }
261:
262: $arguments = array();
263: if (array_key_exists('arguments', $config)) {
264: Validators::assertField($config, 'arguments', 'array');
265: $arguments = self::filterArguments($config['arguments']);
266: $definition->setArguments($arguments);
267: }
268:
269: if (array_key_exists('class', $config) || array_key_exists('factory', $config)) {
270: $definition->class = NULL;
271: $definition->factory = NULL;
272: }
273:
274: if (array_key_exists('class', $config)) {
275: Validators::assertField($config, 'class', 'string|stdClass|null');
276: if ($config['class'] instanceof \stdClass) {
277: $definition->setClass($config['class']->value, self::filterArguments($config['class']->attributes));
278: } else {
279: $definition->setClass($config['class'], $arguments);
280: }
281: }
282:
283: if (array_key_exists('factory', $config)) {
284: Validators::assertField($config, 'factory', 'callable|stdClass|null');
285: if ($config['factory'] instanceof \stdClass) {
286: $definition->setFactory($config['factory']->value, self::filterArguments($config['factory']->attributes));
287: } else {
288: $definition->setFactory($config['factory'], $arguments);
289: }
290: }
291:
292: if (isset($config['setup'])) {
293: if (Helpers::takeParent($config['setup'])) {
294: $definition->setup = array();
295: }
296: Validators::assertField($config, 'setup', 'list');
297: foreach ($config['setup'] as $id => $setup) {
298: Validators::assert($setup, 'callable|stdClass', "setup item #$id");
299: if ($setup instanceof \stdClass) {
300: Validators::assert($setup->value, 'callable', "setup item #$id");
301: if (strpos(is_array($setup->value) ? implode('', $setup->value) : $setup->value, '$') === FALSE) {
302: $definition->addSetup($setup->value, self::filterArguments($setup->attributes));
303: } else {
304: Validators::assert($setup->attributes, 'list:1', "setup arguments for '" . callback($setup->value) . "'");
305: $definition->addSetup($setup->value, $setup->attributes[0]);
306: }
307: } else {
308: $definition->addSetup($setup);
309: }
310: }
311: }
312:
313: $definition->setShared($shared);
314: if (isset($config['parameters'])) {
315: Validators::assertField($config, 'parameters', 'array');
316: $definition->setParameters($config['parameters']);
317: }
318:
319: if (isset($config['autowired'])) {
320: Validators::assertField($config, 'autowired', 'bool|string');
321: $definition->setAutowired($config['autowired']);
322: }
323:
324: if (isset($config['internal'])) {
325: Validators::assertField($config, 'internal', 'bool');
326: $definition->setInternal($config['internal']);
327: }
328:
329: if (isset($config['run'])) {
330: $config['tags']['run'] = (bool) $config['run'];
331: }
332:
333: if (isset($config['tags'])) {
334: Validators::assertField($config, 'tags', 'array');
335: if (Helpers::takeParent($config['tags'])) {
336: $definition->tags = array();
337: }
338: foreach ($config['tags'] as $tag => $attrs) {
339: if (is_int($tag) && is_string($attrs)) {
340: $definition->addTag($attrs);
341: } else {
342: $definition->addTag($tag, $attrs);
343: }
344: }
345: }
346: }
347:
348:
349:
350: private static function filterArguments(array $args)
351: {
352: foreach ($args as $k => $v) {
353: if ($v === '...') {
354: unset($args[$k]);
355: } elseif ($v instanceof \stdClass && isset($v->value, $v->attributes)) {
356: $args[$k] = new Nette\DI\Statement($v->value, self::filterArguments($v->attributes));
357: }
358: }
359: return $args;
360: }
361:
362: }
363: