Example #1
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;
 }