/** * parse service definition and add to the container * * @param array $config * @param Container $container * @param string $alias * @throws ConfigException * @throws ReferenceException */ protected function processServices(array $config, Container $container, $alias = "") { if (!isset($config["services"])) { return; } if (!$this->isAssocArray($config["services"])) { throw new ConfigException("The 'services' configuration must be an associative array"); } // scan for abstract definitions foreach ($config["services"] as $key => $definition) { if (!empty($definition["abstract"])) { $this->abstractDefinitions[self::SERVICE_CHAR . $this->referenceResolver->aliasThisKey($key, $alias)] = $definition; unset($config["services"][$key]); } } // process services foreach ($config["services"] as $key => $definition) { // check if this is an alias of another service if (!empty($definition["aliasOf"])) { // override any existing definitions for this key $aliasedService = $definition["aliasOf"]; $container[$key] = function () use($container, $aliasedService, $alias) { return $this->serviceFactory->aliasService($aliasedService, $alias); }; $this->serviceAliases[$key] = true; continue; } $key = $this->referenceResolver->aliasThisKey($key, $alias); // check for collisions if (isset($container[$key])) { if (isset($this->serviceAliases[$key])) { // this service has been aliased by another service. We can ignore the definition. continue; } throw new ConfigException(sprintf("Tried to define a service named '%s', but that name already exists in the container", $key)); } if (!$this->isAssocArray($definition)) { throw new ConfigException("A service definition must be an associative array"); } $maxIterations = 20; $currentIterations = 0; // check if this definition extends an abstract one while (!empty($definition["extends"]) && $currentIterations < $maxIterations) { $extends = self::SERVICE_CHAR . $this->referenceResolver->aliasThisKey(ltrim($definition["extends"], self::SERVICE_CHAR), $alias); if (!isset($this->abstractDefinitions[$extends])) { throw new ConfigException(sprintf("The service definition for '%s' extends '%s' but there is no abstract definition of that name", $key, $extends)); } // As calls gets wiped out by the replace_recursive, so we need to store it and merge it separately $calls = !empty($definition["calls"]) ? $definition["calls"] : []; if (!empty($this->abstractDefinitions[$extends]["calls"])) { $calls = array_merge($calls, $this->abstractDefinitions[$extends]["calls"]); } unset($definition["extends"]); $definition = array_replace_recursive($this->abstractDefinitions[$extends], $definition); $definition["calls"] = $calls; $currentIterations++; } // get class if (empty($definition["class"])) { throw new ConfigException(sprintf("The service definition for %s does not have a class", $key)); } // get class, resolving parameters if necessary try { $class = $this->referenceResolver->resolveParameter($definition["class"], $container, $alias); } catch (ReferenceException $e) { throw new ReferenceException("Error resolving class for '{$key}'. " . $e->getMessage()); } if (!class_exists($class) && !interface_exists($class)) { throw new ConfigException(sprintf("The service class '%s' does not exist", $class)); } // factories $factory = []; if (!empty($definition["factoryMethod"])) { $factory["method"] = $definition["factoryMethod"]; } if (!empty($definition["factoryClass"])) { // check for malformed definitions ... if (empty($factory["method"])) { throw new ConfigException(sprintf("A factory class was specified for '%s', but no method was set", $key)); } //... and non-existent classes try { $factoryClass = $this->referenceResolver->resolveParameter($definition["factoryClass"], $container, $alias); } catch (ReferenceException $e) { throw new ReferenceException("Error parsing factory class for '{$key}'. " . $e->getMessage()); } if (!class_exists($factoryClass)) { throw new ConfigException(sprintf("The factory class '%s', for '%s', does not exist", $factoryClass, $key)); } // make sure the method actually exists on the class if (!method_exists($factoryClass, $factory["method"])) { throw new ConfigException(sprintf("Invalid factory definition. The method '%s' does not exist on the class '%s'", $factory["method"], $factoryClass)); } $factory["class"] = $factoryClass; } if (!empty($definition["factoryService"])) { // check for malformed definitions if (empty($factory["method"])) { throw new ConfigException(sprintf("A factory service was specified for '%s', but no method was set", $key)); } if (!empty($factory["class"])) { throw new ConfigException(sprintf("The definition for '%s' cannot have both a factory class and a factory service", $key)); } // add the service char if it does not exist if ($definition["factoryService"][0] != self::SERVICE_CHAR) { $definition["factoryService"] = "@" . $definition["factoryService"]; } // don't alias the service here, we'll do that later when we're about to use it $factory["service"] = $definition["factoryService"]; } // arguments $arguments = !empty($definition["arguments"]) ? $definition["arguments"] : []; // calls / setters $calls = !empty($definition["calls"]) ? $definition["calls"] : []; foreach ($calls as $i => $call) { $calls[$i] = $this->processCall($call, $i, $key, $class); } // tags if (!empty($definition["tags"])) { if (!is_array($definition["tags"])) { throw new ConfigException(sprintf("Error for service '%s': the tags definition was not in the expected array format", $key)); } foreach ($definition["tags"] as $tag => $tagKey) { // tags are either defined as ' - "#tagName" ' or ' "#tagName": "tagKey" ', so // we have to detect the type of $tag and change the variables around if required if (is_numeric($tag)) { $tag = $tagKey; $tagKey = null; } $tag = self::TAG_CHAR . $tag; if (!isset($container[$tag])) { $container[$tag] = function () { return new TagCollection(); }; } /** @var TagCollection $collection */ $collection = $container[$tag]; $collection->addService($key, $tagKey); } } // add the definition to the container $container[$key] = function () use($class, $factory, $arguments, $calls, $alias) { // parse arguments for injected parameters and services return $this->serviceFactory->createService($class, $factory, $arguments, $calls, $alias); }; } }