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 ConfigCompiler extends Object
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, ConfigCompilerExtension $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 DIContainerBuilder;
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: if ($extra = array_diff_key($this->config, self::$reserved, $this->extensions)) {
114: $extra = implode("', '", array_keys($extra));
115: throw new InvalidStateException("Found sections '$extra' in configuration, but corresponding extensions are missing.");
116: }
117:
118: foreach ($this->extensions as $name => $extension) {
119: $extension->loadConfiguration();
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('DINestedAccessor', 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('Callback', array('@container', 'create' . ucfirst($name)))
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: }
157:
158: $classes[] = $class = $this->container->generateClass($parentName);
159: $class->setName($className)
160: ->addMethod('initialize');
161:
162: foreach ($this->extensions as $extension) {
163: $extension->afterCompile($class);
164: }
165:
166: $defs = $this->container->getDefinitions();
167: ksort($defs);
168: foreach (array_reverse($defs, TRUE) as $name => $def) {
169: if ($def->class === 'DINestedAccessor' && ($list = preg_grep('#^'.$name.'_#i', array_keys($defs)))) {
170: $def->class = $className . '_' . $name;
171: $class->documents = preg_replace("#\S+(?= \\$$name$)#", $def->class, $class->documents);
172: $classes[] = $accessor = new PhpClassType($def->class);
173: foreach ($list as $item) {
174: $short = substr($item, strlen($name) + 1);
175: $accessor->addDocument($defs[$item]->shared
176: ? "@property {$defs[$item]->class} \$$short"
177: : "@method {$defs[$item]->class} create" . ucfirst("$short()"));
178: }
179: }
180: }
181:
182: return implode("\n\n\n", $classes);
183: }
184:
185:
186:
187:
188:
189:
190:
191: 192: 193: 194:
195: public static function parseServices(DIContainerBuilder $container, array $config, $namespace = NULL)
196: {
197: $services = isset($config['services']) ? $config['services'] : array();
198: $factories = isset($config['factories']) ? $config['factories'] : array();
199: if ($tmp = array_intersect_key($services, $factories)) {
200: $tmp = implode("', '", array_keys($tmp));
201: throw new ServiceCreationException("It is not allowed to use services and factories with the same names: '$tmp'.");
202: }
203:
204: $all = $services + $factories;
205: uasort($all, create_function('$a, $b', '
206: return strcmp(ConfigHelpers::isInheriting($a), ConfigHelpers::isInheriting($b));
207: '));
208:
209: foreach ($all as $name => $def) {
210: $shared = array_key_exists($name, $config['services']);
211: $name = ($namespace ? $namespace . '_' : '') . $name;
212:
213: if (($parent = ConfigHelpers::takeParent($def)) && $parent !== $name) {
214: $container->removeDefinition($name);
215: $definition = $container->addDefinition($name);
216: if ($parent !== ConfigHelpers::OVERWRITE) {
217: foreach ($container->getDefinition($parent) as $k => $v) {
218: $definition->$k = $v;
219: }
220: }
221: } elseif ($container->hasDefinition($name)) {
222: $definition = $container->getDefinition($name);
223: if ($definition->shared !== $shared) {
224: throw new ServiceCreationException("It is not allowed to use service and factory with the same name '$name'.");
225: }
226: } else {
227: $definition = $container->addDefinition($name);
228: }
229: try {
230: self::parseService($definition, $def, $shared);
231: } catch (Exception $e) {
232: throw new ServiceCreationException("Service '$name': " . $e->getMessage());
233: }
234: }
235: }
236:
237:
238:
239: 240: 241: 242:
243: public static function parseService(DIServiceDefinition $definition, $config, $shared = TRUE)
244: {
245: if ($config === NULL) {
246: return;
247: } elseif (!is_array($config)) {
248: $config = array('class' => NULL, 'factory' => $config);
249: }
250:
251: $known = $shared
252: ? array('class', 'factory', 'arguments', 'setup', 'autowired', 'run', 'tags')
253: : array('class', 'factory', 'arguments', 'setup', 'tags', 'internal', 'parameters');
254:
255: if ($error = array_diff(array_keys($config), $known)) {
256: throw new InvalidStateException("Unknown key '" . implode("', '", $error) . "' in definition of service.");
257: }
258:
259: $arguments = array();
260: if (array_key_exists('arguments', $config)) {
261: Validators::assertField($config, 'arguments', 'array');
262: $arguments = self::filterArguments($config['arguments']);
263: $definition->setArguments($arguments);
264: }
265:
266: if (array_key_exists('class', $config) || array_key_exists('factory', $config)) {
267: $definition->class = NULL;
268: $definition->factory = NULL;
269: }
270:
271: if (array_key_exists('class', $config)) {
272: Validators::assertField($config, 'class', 'string|stdClass|null');
273: if ($config['class'] instanceof stdClass) {
274: $definition->setClass($config['class']->value, self::filterArguments($config['class']->attributes));
275: } else {
276: $definition->setClass($config['class'], $arguments);
277: }
278: }
279:
280: if (array_key_exists('factory', $config)) {
281: Validators::assertField($config, 'factory', 'callable|stdClass|null');
282: if ($config['factory'] instanceof stdClass) {
283: $definition->setFactory($config['factory']->value, self::filterArguments($config['factory']->attributes));
284: } else {
285: $definition->setFactory($config['factory'], $arguments);
286: }
287: }
288:
289: if (isset($config['setup'])) {
290: if (ConfigHelpers::takeParent($config['setup'])) {
291: $definition->setup = array();
292: }
293: Validators::assertField($config, 'setup', 'list');
294: foreach ($config['setup'] as $id => $setup) {
295: Validators::assert($setup, 'callable|stdClass', "setup item #$id");
296: if ($setup instanceof stdClass) {
297: Validators::assert($setup->value, 'callable', "setup item #$id");
298: if (strpos(is_array($setup->value) ? implode('', $setup->value) : $setup->value, '$') === FALSE) {
299: $definition->addSetup($setup->value, self::filterArguments($setup->attributes));
300: } else {
301: Validators::assert($setup->attributes, 'list:1', "setup arguments for '" . callback($setup->value) . "'");
302: $definition->addSetup($setup->value, $setup->attributes[0]);
303: }
304: } else {
305: $definition->addSetup($setup);
306: }
307: }
308: }
309:
310: $definition->setShared($shared);
311: if (isset($config['parameters'])) {
312: Validators::assertField($config, 'parameters', 'array');
313: $definition->setParameters($config['parameters']);
314: }
315:
316: if (isset($config['autowired'])) {
317: Validators::assertField($config, 'autowired', 'bool|string');
318: $definition->setAutowired($config['autowired']);
319: }
320:
321: if (isset($config['internal'])) {
322: Validators::assertField($config, 'internal', 'bool');
323: $definition->setInternal($config['internal']);
324: }
325:
326: if (isset($config['run'])) {
327: $config['tags']['run'] = (bool) $config['run'];
328: }
329:
330: if (isset($config['tags'])) {
331: Validators::assertField($config, 'tags', 'array');
332: if (ConfigHelpers::takeParent($config['tags'])) {
333: $definition->tags = array();
334: }
335: foreach ($config['tags'] as $tag => $attrs) {
336: if (is_int($tag) && is_string($attrs)) {
337: $definition->addTag($attrs);
338: } else {
339: $definition->addTag($tag, $attrs);
340: }
341: }
342: }
343: }
344:
345:
346:
347: private static function filterArguments(array $args)
348: {
349: foreach ($args as $k => $v) {
350: if ($v === '...') {
351: unset($args[$k]);
352: } elseif ($v instanceof stdClass && isset($v->value, $v->attributes)) {
353: $args[$k] = new DIStatement($v->value, self::filterArguments($v->attributes));
354: }
355: }
356: return $args;
357: }
358:
359: }
360: