_specs[$componentName] = $spec; } /** * Gets the specification for the given $componentName. * @param string $componentName * @return net_sourceforge_phpcrafty_ComponentSpec * @throws net_sourceforge_phpcrafty_ComponenyFactoryException If spec is not found */ public function getComponentSpec($componentName) { return $this->_specs[$componentName]; } /** * Test if the given parameter is a dependency to be resolved. * @param mixed $input * @return boolean * @access private */ private function _isDependency($input) { return ($input instanceof net_sourceforge_phpcrafty_ComponentReference); } /** * Resolve all dependencies from ComponentReference objects into their * appropriate instances. * @param mixed $input * @return mixed * @access private */ private function _resolveDependencies($input) { if (is_array($input)) { $ret = array(); foreach ($input as $value) { $ret[] = $this->_resolveDependencies($value); } return $ret; } else { if ($this->_isDependency($input)) { $componentName = $input->getComponentName(); return $this->create($componentName); } else { return $input; } } } /** * Get a ReflectionClass decorated to provide setter-based injection * components during instantiation. * @param string $componentName * @return net_sourceforge_phpcrafty_ComponentReflector */ public function classOf($componentName) { $spec = $this->getComponentSpec($componentName); $className = $spec->getClassName(); //Apply properties $properties = array(); foreach ($spec->getProperties() as $key => $value) { $properties[$key] = $this->_resolveDependencies($value); } $class = $this->_newComponentReflector($className, $properties); return $class; } /** * Create an instance of the given component. * @param string $componentName * @param mixed[] $constructorArgs, optional * @return object */ public function create($componentName, $constructorArgs = null) { $spec = $this->getComponentSpec($componentName); //If shared instances are used, try to return a registered instance // if not, reference it now if ($spec->isShared()) { if (isset($this->_sharedInstances[$componentName])) { return $this->_sharedInstances[$componentName]; } else { $o = null; $this->_sharedInstances[$componentName] =& $o; } } //Get the Reflector $class = $this->classOf($componentName); //Determine constructor params if (!is_array($constructorArgs)) { $injectedArgs = $this->_resolveDependencies( $spec->getConstructorArgs()); } else { $injectedArgs = $this->_resolveDependencies($constructorArgs); } $o = $class->newInstanceArgs($injectedArgs); if ($o instanceof org_tubepress_ioc_ContainerAware) { $o->setContainer($this); } return $o; } }