/** * Get the injector that instantiates the object referred to by * the given Reference instance. * * This method will first check for explicit injectors. If there * are none set, it will generate a ConstructorInjector instance * on the fly using bindings configuration. This method will * create an injector that always tries to satisfy the object's * dependencies, even if the said dependencies is allowed to be * NULL. If you need the injector to behave differently, you can * explicitly set an injector instance using * {@see setInjector()}. * * @param Reference $reference Refers to the instance whose * injector is to be returned. * @return InjectorInterface The injector for the given Reference * instance. * */ public function getInjector(Reference $reference) { $id = $reference->getID(); if ($this->hasExplicitInjector($id)) { return $this->injectors[$id]; } $className = $reference->getClass(); $ctorArgs = $this->getConstructorArguments($className, $id); return new ConstructorInjector($reference, $ctorArgs); }
/** * Set an instantiated dependency. * * Will first check if the Reference instance provided is * actually registered in the list, will throw * InvalidArgumentException if not. * * Will also check if the provided dependency is the actual * required dependency object. If not, will also throw an * InvalidArgumentException. * * @throws InvalidArgumentException * @param Reference $reference Refers to the instantiated dependency object instance. * @param mixed $dependency The instantiated dependency object. * */ public function setInstantiatedDependency(Reference $reference, $dependency) { $id = $reference->getID(); $class = $reference->getClass(); if (array_key_exists($id, $this->list) == FALSE) { throw new InvalidArgumentException("DependencyList error in setting instantiated dependency. The Reference instance provided ({$id}) is not present in the dependency list."); } if ($dependency instanceof $class == FALSE) { $unexpectedType = is_object($dependency) ? get_class($dependency) : gettype($dependency); throw new InvalidArgumentException("DependencyList error in setting instantiated dependency. {$class} was expected, {$unexpectedType} given."); } $this->instantiatedDependencies[$id] = $dependency; }