示例#1
0
文件: Compiler.php 项目: krecek/nrsn
	/**
	 * Parses section 'services' from configuration file.
	 * @return void
	 */
	public static function parseServices(NDIContainerBuilder $container, array $config, $namespace = NULL)
	{
		$services = isset($config['services']) ? $config['services'] : array();
		$factories = isset($config['factories']) ? $config['factories'] : array();
		$all = array_merge($services, $factories);

		uasort($all, create_function('$a, $b', '
			return strcmp(NConfigHelpers::isInheriting($a), NConfigHelpers::isInheriting($b));
		'));

		foreach ($all as $origName => $def) {
			$shared = array_key_exists($origName, $services);
			if ((string) (int) $origName === (string) $origName) {
				$name = (string) (count($container->getDefinitions()) + 1);
			} elseif ($shared && array_key_exists($origName, $factories)) {
				throw new NServiceCreationException("It is not allowed to use services and factories with the same name: '$origName'.");
			} else {
				$name = ($namespace ? $namespace . '.' : '') . strtr($origName, '\\', '_');
			}

			if (($parent = NConfigHelpers::takeParent($def)) && $parent !== $name) {
				$container->removeDefinition($name);
				$definition = $container->addDefinition($name);
				if ($parent !== NConfigHelpers::OVERWRITE) {
					foreach ($container->getDefinition($parent) as $k => $v) {
						$definition->$k = unserialize(serialize($v)); // deep clone
					}
				}
			} elseif ($container->hasDefinition($name)) {
				$definition = $container->getDefinition($name);
				if ($definition->shared !== $shared) {
					throw new NServiceCreationException("It is not allowed to use service and factory with the same name '$name'.");
				}
			} else {
				$definition = $container->addDefinition($name);
			}
			try {
				self::parseService($definition, $def, $shared);
			} catch (Exception $e) {
				throw new NServiceCreationException("Service '$name': " . $e->getMessage(), NULL, $e);
			}
		}
	}
示例#2
0
	/**
	 * Parses section 'services' from configuration file.
	 * @return void
	 */
	public static function parseServices(NDIContainerBuilder $container, array $config, $namespace = NULL)
	{
		$all = isset($config['services']) ? $config['services'] : array();
		$all += isset($config['factories']) ? $config['factories'] : array();

		uasort($all, create_function('$a, $b', '
			return strcmp(NConfigHelpers::isInheriting($a), NConfigHelpers::isInheriting($b));
		'));

		foreach ($all as $name => $def) {
			$isService = array_key_exists($name, $config['services']);
			$name = ($namespace ? $namespace . '_' : '') . $name;

			if (($parent = NConfigHelpers::takeParent($def)) && $parent !== $name) {
				$container->removeDefinition($name);
				$definition = $container->addDefinition($name);
				if ($parent !== NConfigHelpers::OVERWRITE) {
					foreach ($container->getDefinition($parent) as $k => $v) {
						$definition->$k = $v;
					}
				}
			} elseif ($container->hasDefinition($name)) {
				$definition = $container->getDefinition($name);
			} else {
				$definition = $container->addDefinition($name);
			}
			try {
				self::parseService($definition, $def, $isService);
			} catch (Exception $e) {
				throw new NServiceCreationException("Service '$name': " . $e->getMessage());
			}
		}
	}