Example #1
0
 /**
  * @param Dependency $value
  *
  * @return Setter
  */
 public function setValue(Dependency $value)
 {
     if (!is_a($value->getClassName(), $this->className, true)) {
         throw new \RuntimeException("Failed to set `{$this->name}`-value: " . "Passed value `" . $value->getClassName() . "` " . "is not a valid `" . $this->className . "` instance");
     }
     $this->value = $value;
     return $this;
 }
Example #2
0
 /**
  * @param        $className
  * @param bool   $persistent
  * @param object $instance
  *
  * @return $this
  * @throws DiException
  */
 public function register($className, $persistent = true, $instance = null)
 {
     if (!$this instanceof ContainerInterface) {
         throw new DiException("Failed to register dependency: " . get_class($this) . " uses " . ContainerTrait::class . ", but doesnt implement " . ContainerInterface::class);
     }
     if (isset($this->dependencies[$className])) {
         throw new DiException("Failed to register dependency {$className}: A dependency " . "of this type is already registered. Use a sub-class to " . "avoid ambiguity");
     }
     /** @var ContainerInterface|ContainerTrait $this */
     $dep = new Dependency($className, $persistent, $instance);
     $this->dependencies[$className] = $dep;
     //TODO: those two are the workhorses, but everything should be prepared for caching. Just need an elegant way...
     $dep->analyze()->wire($this);
     return $this;
 }