示例#1
0
 /**
  * Constructor.
  * 
  * Pass the object you wanted to be returned immediately:
  *
  * <code>
  * $injector = new FauxInjector(
  *     new Reference('Carrot\Request'),
  *     $request
  * );
  * </code>
  *
  * The container will then get the instance given here directly
  * when it calls {@see inject()}.
  * 
  * @throws RuntimeException If the given Reference instance's
  *         lifecycle is not a singleton.
  * @param Reference $reference Refers to the object this injector
  *        is supposed to instantiate.
  * @param mixed $object The object instance to be returned when
  *        {@see inject()} is called.
  *
  */
 public function __construct(Reference $reference, $object)
 {
     if ($reference->isSingleton() == FALSE) {
         $id = $reference->getID();
         throw new InvalidArgumentException("FauxInjector error in instantiation. The given Reference instance's ({$id}) lifecycle is not singleton.");
     }
     $this->reference = $reference;
     $this->object = $object;
 }
 /**
  * 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);
 }
 /**
  * Generate CallbackInjector instance from the given
  * configuration item array.
  *
  * @throws RuntimeException If the provided configuration item is
  *         invalid.
  * @param Reference $reference Refers to the instance whose
  *        injector is to be generated.
  * @param array $item Configuration item array.
  *
  */
 protected function generateCallbackInjector(Reference $reference, array $item)
 {
     if ($this->isCallbackConfigItemArrayValid($item) == FALSE) {
         $id = $reference->getID();
         throw new RuntimeException("ArrayMapConfig error in getting CallbackInjector for '{$id}'. The configuration item array is not valid.");
     }
     return new CallbackInjector($reference, $item['func'], $item['args']);
 }
 /**
  * Get an instantiated dependency from the given Reference instance.
  * 
  * Throws InvalidArgumentException if the Reference instance
  * given is not present in the dependency list.
  * 
  * @throws InvalidArgumentException
  * @param Reference $reference Refers to the instantiated dependency we wanted to get.
  * @return mixed|FALSE The instantiated dependency, or FALSE if it's not yet present.
  *
  */
 public function getInstantiatedDependency(Reference $reference)
 {
     $id = $reference->getID();
     if (array_key_exists($id, $this->list) == FALSE) {
         throw new InvalidArgumentException("DependencyList error in getting an instantiated dependency. The Reference instance provided ({$id}) is not present in the dependency list.");
     }
     if (array_key_exists($id, $this->instantiatedDependencies) == FALSE) {
         return FALSE;
     }
     return $this->instantiatedDependencies[$id];
 }
示例#5
0
 /**
  * Get an instance from the singleton objects cache.
  *
  * @see get()
  * @param Reference $reference The reference to the object to be
  *        retrieved.
  * @return mixed The object instance.
  *
  */
 protected function getFromSingletonCache(Reference $reference)
 {
     return $this->singletons[$reference->getID()];
 }