1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class NConfigCompiler extends NObject
26: {
27:
28: private $extensions = array();
29:
30:
31: private $container;
32:
33:
34: private $config;
35:
36:
37: private static $reserved = array('services' => 1, 'factories' => 1, 'parameters' => 1);
38:
39:
40:
41: 42: 43: 44:
45: public function addExtension($name, NConfigCompilerExtension $extension)
46: {
47: if (isset(self::$reserved[$name])) {
48: throw new InvalidArgumentException("Name '$name' is reserved.");
49: }
50: $this->extensions[$name] = $extension->setCompiler($this, $name);
51: return $this;
52: }
53:
54:
55:
56: 57: 58:
59: public function getExtensions()
60: {
61: return $this->extensions;
62: }
63:
64:
65:
66: 67: 68:
69: public function getContainerBuilder()
70: {
71: return $this->container;
72: }
73:
74:
75:
76: 77: 78: 79:
80: public function getConfig()
81: {
82: return $this->config;
83: }
84:
85:
86:
87: 88: 89:
90: public function compile(array $config, $className, $parentName)
91: {
92: $this->config = $config;
93: $this->container = new NDIContainerBuilder;
94: $this->processParameters();
95: $this->processExtensions();
96: $this->processServices();
97: return $this->generateCode($className, $parentName);
98: }
99:
100:
101:
102: public function processParameters()
103: {
104: if (isset($this->config['parameters'])) {
105: $this->container->parameters = $this->config['parameters'];
106: }
107: }
108:
109:
110:
111: public function processExtensions()
112: {
113: foreach ($this->extensions as $name => $extension) {
114: $extension->loadConfiguration();
115: }
116:
117: if ($extra = array_diff_key($this->config, self::$reserved, $this->extensions)) {
118: $extra = implode("', '", array_keys($extra));
119: throw new InvalidStateException("Found sections '$extra' in configuration, but corresponding extensions are missing.");
120: }
121: }
122:
123:
124:
125: public function processServices()
126: {
127: $this->parseServices($this->container, $this->config);
128:
129: foreach ($this->extensions as $name => $extension) {
130: $this->container->addDefinition($name)
131: ->setClass('NDINestedAccessor', array('@container', $name))
132: ->setAutowired(FALSE);
133:
134: if (isset($this->config[$name])) {
135: $this->parseServices($this->container, $this->config[$name], $name);
136: }
137: }
138:
139: foreach ($this->container->getDefinitions() as $name => $def) {
140: $factory = $name . 'Factory';
141: if (!$def->shared && !$def->internal && !$this->container->hasDefinition($factory)) {
142: $this->container->addDefinition($factory)
143: ->setClass('NCallback', array('@container', NDIContainer::getMethodName($name, FALSE)))
144: ->setAutowired(FALSE)
145: ->tags = $def->tags;
146: }
147: }
148: }
149:
150:
151:
152: public function generateCode($className, $parentName)
153: {
154: foreach ($this->extensions as $extension) {
155: $extension->beforeCompile();
156: $this->container->addDependency(NClassReflection::from($extension)->getFileName());
157: }
158:
159: $classes[] = $class = $this->container->generateClass($parentName);
160: $class->setName($className)
161: ->addMethod('initialize');
162:
163: foreach ($this->extensions as $extension) {
164: $extension->afterCompile($class);
165: }
166:
167: $defs = $this->container->getDefinitions();
168: ksort($defs);
169: $list = array_keys($defs);
170: foreach (array_reverse($defs, TRUE) as $name => $def) {
171: if ($def->class === 'NDINestedAccessor' && ($found = preg_grep('#^'.$name.'\.#i', $list))) {
172: $list = array_diff($list, $found);
173: $def->class = $className . '_' . preg_replace('#\W+#', '_', $name);
174: $class->documents = preg_replace("#\S+(?= \\$$name$)#", $def->class, $class->documents);
175: $classes[] = $accessor = new NPhpClassType($def->class);
176: foreach ($found 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(NDIContainerBuilder $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 NServiceCreationException("It is not allowed to use services and factories with the same names: '$tmp'.");
205: }
206:
207: $all = $services + $factories;
208: uasort($all, create_function('$a, $b', '
209: return strcmp(NConfigHelpers::isInheriting($a), NConfigHelpers::isInheriting($b));
210: '));
211:
212: foreach ($all as $name => $def) {
213: $shared = array_key_exists($name, $services);
214: $name = ($namespace ? $namespace . '.' : '') . $name;
215:
216: if (($parent = NConfigHelpers::takeParent($def)) && $parent !== $name) {
217: $container->removeDefinition($name);
218: $definition = $container->addDefinition($name);
219: if ($parent !== NConfigHelpers::OVERWRITE) {
220: foreach ($container->getDefinition($parent) as $k => $v) {
221: $definition->$k = unserialize(serialize($v));
222: }
223: }
224: } elseif ($container->hasDefinition($name)) {
225: $definition = $container->getDefinition($name);
226: if ($definition->shared !== $shared) {
227: throw new NServiceCreationException("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: self::parseService($definition, $def, $shared);
234: } catch (Exception $e) {
235: throw new NServiceCreationException("Service '$name': " . $e->getMessage(), NULL, $e);
236: }
237: }
238: }
239:
240:
241:
242: 243: 244: 245:
246: public static function parseService(NDIServiceDefinition $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', 'autowired', 'tags', 'internal', 'parameters');
257:
258: if ($error = array_diff(array_keys($config), $known)) {
259: throw new InvalidStateException("Unknown key '" . implode("', '", $error) . "' in definition of service.");
260: }
261:
262: $arguments = array();
263: if (array_key_exists('arguments', $config)) {
264: NValidators::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: NValidators::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: NValidators::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 (NConfigHelpers::takeParent($config['setup'])) {
294: $definition->setup = array();
295: }
296: NValidators::assertField($config, 'setup', 'list');
297: foreach ($config['setup'] as $id => $setup) {
298: NValidators::assert($setup, 'callable|stdClass', "setup item #$id");
299: if ($setup instanceof stdClass) {
300: NValidators::assert($setup->value, 'callable', "setup item #$id");
301: $definition->addSetup($setup->value, self::filterArguments($setup->attributes));
302: } else {
303: $definition->addSetup($setup);
304: }
305: }
306: }
307:
308: $definition->setShared($shared);
309: if (isset($config['parameters'])) {
310: NValidators::assertField($config, 'parameters', 'array');
311: $definition->setParameters($config['parameters']);
312: }
313:
314: if (isset($config['autowired'])) {
315: NValidators::assertField($config, 'autowired', 'bool');
316: $definition->setAutowired($config['autowired']);
317: }
318:
319: if (isset($config['internal'])) {
320: NValidators::assertField($config, 'internal', 'bool');
321: $definition->setInternal($config['internal']);
322: }
323:
324: if (isset($config['run'])) {
325: $config['tags']['run'] = (bool) $config['run'];
326: }
327:
328: if (isset($config['tags'])) {
329: NValidators::assertField($config, 'tags', 'array');
330: if (NConfigHelpers::takeParent($config['tags'])) {
331: $definition->tags = array();
332: }
333: foreach ($config['tags'] as $tag => $attrs) {
334: if (is_int($tag) && is_string($attrs)) {
335: $definition->addTag($attrs);
336: } else {
337: $definition->addTag($tag, $attrs);
338: }
339: }
340: }
341: }
342:
343:
344:
345: 346: 347: 348:
349: public static function filterArguments(array $args)
350: {
351: foreach ($args as $k => $v) {
352: if ($v === '...') {
353: unset($args[$k]);
354: } elseif ($v instanceof stdClass && isset($v->value, $v->attributes)) {
355: $args[$k] = new NDIStatement($v->value, self::filterArguments($v->attributes));
356: }
357: }
358: return $args;
359: }
360:
361: }
362: