Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • NConfigCompiler
  • NConfigCompilerExtension
  • NConfigHelpers
  • NConfigLoader
  • NConfigurator

Interfaces

  • IConfigAdapter
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Config
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * DI container compiler.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read array $extensions
 21:  * @property-read NDIContainerBuilder $containerBuilder
 22:  * @property-read array $config
 23:  * @package Nette\Config
 24:  */
 25: class NConfigCompiler extends NObject
 26: {
 27:     /** @var array of NConfigCompilerExtension */
 28:     private $extensions = array();
 29: 
 30:     /** @var NDIContainerBuilder */
 31:     private $container;
 32: 
 33:     /** @var array */
 34:     private $config;
 35: 
 36:     /** @var array reserved section names */
 37:     private static $reserved = array('services' => 1, 'factories' => 1, 'parameters' => 1);
 38: 
 39: 
 40: 
 41:     /**
 42:      * Add custom configurator extension.
 43:      * @return NConfigCompiler  provides a fluent interface
 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:      * @return array
 58:      */
 59:     public function getExtensions()
 60:     {
 61:         return $this->extensions;
 62:     }
 63: 
 64: 
 65: 
 66:     /**
 67:      * @return NDIContainerBuilder
 68:      */
 69:     public function getContainerBuilder()
 70:     {
 71:         return $this->container;
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * Returns configuration without expanded parameters.
 78:      * @return array
 79:      */
 80:     public function getConfig()
 81:     {
 82:         return $this->config;
 83:     }
 84: 
 85: 
 86: 
 87:     /**
 88:      * @return string
 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:         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('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:         }
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:         $list = array_keys($defs);
169:         foreach (array_reverse($defs, TRUE) as $name => $def) {
170:             if ($def->class === 'NDINestedAccessor' && ($found = preg_grep('#^'.$name.'\.#i', $list))) {
171:                 $list = array_diff($list, $found);
172:                 $def->class = $className . '_' . preg_replace('#\W+#', '_', $name);
173:                 $class->documents = preg_replace("#\S+(?= \\$$name$)#", $def->class, $class->documents);
174:                 $classes[] = $accessor = new NPhpClassType($def->class);
175:                 foreach ($found as $item) {
176:                     $short = substr($item, strlen($name)  + 1);
177:                     $accessor->addDocument($defs[$item]->shared
178:                         ? "@property {$defs[$item]->class} \$$short"
179:                         : "@method {$defs[$item]->class} create" . ucfirst("$short()"));
180:                 }
181:             }
182:         }
183: 
184:         return implode("\n\n\n", $classes);
185:     }
186: 
187: 
188: 
189:     /********************* tools ****************d*g**/
190: 
191: 
192: 
193:     /**
194:      * Parses section 'services' from configuration file.
195:      * @return void
196:      */
197:     public static function parseServices(NDIContainerBuilder $container, array $config, $namespace = NULL)
198:     {
199:         $services = isset($config['services']) ? $config['services'] : array();
200:         $factories = isset($config['factories']) ? $config['factories'] : array();
201:         if ($tmp = array_intersect_key($services, $factories)) {
202:             $tmp = implode("', '", array_keys($tmp));
203:             throw new NServiceCreationException("It is not allowed to use services and factories with the same names: '$tmp'.");
204:         }
205: 
206:         $all = $services + $factories;
207:         uasort($all, create_function('$a, $b', '
208:             return strcmp(NConfigHelpers::isInheriting($a), NConfigHelpers::isInheriting($b));
209:         '));
210: 
211:         foreach ($all as $name => $def) {
212:             $shared = array_key_exists($name, $services);
213:             $name = ($namespace ? $namespace . '.' : '') . $name;
214: 
215:             if (($parent = NConfigHelpers::takeParent($def)) && $parent !== $name) {
216:                 $container->removeDefinition($name);
217:                 $definition = $container->addDefinition($name);
218:                 if ($parent !== NConfigHelpers::OVERWRITE) {
219:                     foreach ($container->getDefinition($parent) as $k => $v) {
220:                         $definition->$k = $v;
221:                     }
222:                 }
223:             } elseif ($container->hasDefinition($name)) {
224:                 $definition = $container->getDefinition($name);
225:                 if ($definition->shared !== $shared) {
226:                     throw new NServiceCreationException("It is not allowed to use service and factory with the same name '$name'.");
227:                 }
228:             } else {
229:                 $definition = $container->addDefinition($name);
230:             }
231:             try {
232:                 self::parseService($definition, $def, $shared);
233:             } catch (Exception $e) {
234:                 throw new NServiceCreationException("Service '$name': " . $e->getMessage());
235:             }
236:         }
237:     }
238: 
239: 
240: 
241:     /**
242:      * Parses single service from configuration file.
243:      * @return void
244:      */
245:     public static function parseService(NDIServiceDefinition $definition, $config, $shared = TRUE)
246:     {
247:         if ($config === NULL) {
248:             return;
249:         } elseif (!is_array($config)) {
250:             $config = array('class' => NULL, 'factory' => $config);
251:         }
252: 
253:         $known = $shared
254:             ? array('class', 'factory', 'arguments', 'setup', 'autowired', 'run', 'tags')
255:             : array('class', 'factory', 'arguments', 'setup', 'autowired', 'tags', 'internal', 'parameters');
256: 
257:         if ($error = array_diff(array_keys($config), $known)) {
258:             throw new InvalidStateException("Unknown key '" . implode("', '", $error) . "' in definition of service.");
259:         }
260: 
261:         $arguments = array();
262:         if (array_key_exists('arguments', $config)) {
263:             NValidators::assertField($config, 'arguments', 'array');
264:             $arguments = self::filterArguments($config['arguments']);
265:             $definition->setArguments($arguments);
266:         }
267: 
268:         if (array_key_exists('class', $config) || array_key_exists('factory', $config)) {
269:             $definition->class = NULL;
270:             $definition->factory = NULL;
271:         }
272: 
273:         if (array_key_exists('class', $config)) {
274:             NValidators::assertField($config, 'class', 'string|stdClass|null');
275:             if ($config['class'] instanceof stdClass) {
276:                 $definition->setClass($config['class']->value, self::filterArguments($config['class']->attributes));
277:             } else {
278:                 $definition->setClass($config['class'], $arguments);
279:             }
280:         }
281: 
282:         if (array_key_exists('factory', $config)) {
283:             NValidators::assertField($config, 'factory', 'callable|stdClass|null');
284:             if ($config['factory'] instanceof stdClass) {
285:                 $definition->setFactory($config['factory']->value, self::filterArguments($config['factory']->attributes));
286:             } else {
287:                 $definition->setFactory($config['factory'], $arguments);
288:             }
289:         }
290: 
291:         if (isset($config['setup'])) {
292:             if (NConfigHelpers::takeParent($config['setup'])) {
293:                 $definition->setup = array();
294:             }
295:             NValidators::assertField($config, 'setup', 'list');
296:             foreach ($config['setup'] as $id => $setup) {
297:                 NValidators::assert($setup, 'callable|stdClass', "setup item #$id");
298:                 if ($setup instanceof stdClass) {
299:                     NValidators::assert($setup->value, 'callable', "setup item #$id");
300:                     $definition->addSetup($setup->value, self::filterArguments($setup->attributes));
301:                 } else {
302:                     $definition->addSetup($setup);
303:                 }
304:             }
305:         }
306: 
307:         $definition->setShared($shared);
308:         if (isset($config['parameters'])) {
309:             NValidators::assertField($config, 'parameters', 'array');
310:             $definition->setParameters($config['parameters']);
311:         }
312: 
313:         if (isset($config['autowired'])) {
314:             NValidators::assertField($config, 'autowired', 'bool');
315:             $definition->setAutowired($config['autowired']);
316:         }
317: 
318:         if (isset($config['internal'])) {
319:             NValidators::assertField($config, 'internal', 'bool');
320:             $definition->setInternal($config['internal']);
321:         }
322: 
323:         if (isset($config['run'])) {
324:             $config['tags']['run'] = (bool) $config['run'];
325:         }
326: 
327:         if (isset($config['tags'])) {
328:             NValidators::assertField($config, 'tags', 'array');
329:             if (NConfigHelpers::takeParent($config['tags'])) {
330:                 $definition->tags = array();
331:             }
332:             foreach ($config['tags'] as $tag => $attrs) {
333:                 if (is_int($tag) && is_string($attrs)) {
334:                     $definition->addTag($attrs);
335:                 } else {
336:                     $definition->addTag($tag, $attrs);
337:                 }
338:             }
339:         }
340:     }
341: 
342: 
343: 
344:     /**
345:      * Removes ... and replaces entities with NDIStatement.
346:      * @return array
347:      */
348:     public static function filterArguments(array $args)
349:     {
350:         foreach ($args as $k => $v) {
351:             if ($v === '...') {
352:                 unset($args[$k]);
353:             } elseif ($v instanceof stdClass && isset($v->value, $v->attributes)) {
354:                 $args[$k] = new NDIStatement($v->value, self::filterArguments($v->attributes));
355:             }
356:         }
357:         return $args;
358:     }
359: 
360: }
361: 
Nette Framework 2.0.1 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0