/**
  * Adds a dependency for the provided class to this container
  * @param string $for A full class name
  * @param Dependency $dependency
  * @return null
  */
 public function addDependency($for, Dependency $dependency)
 {
     if (!String::isString($for, String::NOT_EMPTY)) {
         throw new ZiboException('Invalid for class name provided');
     }
     $id = $dependency->getId();
     if (!isset($this->dependencies[$for])) {
         $this->dependencies[$for] = array();
     }
     if ($id) {
         $this->dependencies[$for][$id] = $dependency;
     } else {
         $this->dependencies[$for][] = $dependency;
         $ids = array_keys($this->dependencies[$for]);
         $dependency->setId(array_pop($ids));
     }
 }
Ejemplo n.º 2
0
 public function testAddCallWithConstructorCallWillNotAddCallButSetConstructorArguments()
 {
     $dependency = new Dependency('className');
     $call = new DependencyCall('__construct');
     $dependency->addCall($call);
     $this->assertNull($dependency->getCalls());
     $this->assertNull($dependency->getConstructorArguments());
     $argument = new DependencyCallArgument();
     $call->addArgument($argument);
     $dependency->addCall($call);
     $expected = array($argument);
     $this->assertNull($dependency->getCalls());
     $this->assertEquals($expected, $dependency->getConstructorArguments());
 }
 public function testGetAll()
 {
     $interface = 'zibo\\core\\di\\TestInterface';
     $token1 = 'test1';
     $token2 = 'test2';
     $id = 'id';
     $construct1 = new DependencyCall('__construct');
     $construct1->addArgument(new DependencyCallArgument('value', $token1));
     $construct2 = new DependencyCall('__construct');
     $construct2->addArgument(new DependencyCallArgument('value', $token2));
     $dependency1 = new Dependency('zibo\\core\\di\\TestObject', $id);
     $dependency1->addCall($construct1);
     $dependency2 = new Dependency('zibo\\core\\di\\TestObject');
     $dependency2->addCall($construct2);
     $container = new DependencyContainer();
     $container->addDependency($interface, $dependency1);
     $container->addDependency($interface, $dependency2);
     $this->di->setContainer($container);
     $expected = array($id => new TestObject($token1), 0 => new TestObject($token2));
     $result = $this->di->getAll($interface);
     $this->assertEquals($expected, $result);
 }
Ejemplo n.º 4
0
 /**
  * Creates an instance of the provided dependency
  * @param string $interface Full class name of the interface or parent class
  * @param Dependency $dependency Definition of the class to create
  * @param array $exclude Array with the interface as key and an array with
  * id's of dependencies as key to exclude from the get calls.
  * @return mixed Instance of the dependency
  * @throws zibo\ZiboException when the dependency could not be created
  */
 protected function create($interface, Dependency $dependency, array $exclude = null)
 {
     if (!self::$objectFactory) {
         self::$objectFactory = new ObjectFactory();
     }
     if (!$exclude) {
         $exclude = array($interface => array($dependency->getId() => true));
     } elseif (!isset($exclude[$interface])) {
         $exclude[$interface] = array($dependency->getId() => true);
     } else {
         $exclude[$interface][$dependency->getId()] = true;
     }
     $className = $dependency->getClassName();
     $arguments = $dependency->getConstructorArguments();
     $arguments = $this->getCallbackArguments($arguments, $exclude);
     $instance = self::$objectFactory->create($className, $interface, $arguments);
     $calls = $dependency->getCalls();
     if ($calls) {
         foreach ($calls as $call) {
             $arguments = $this->getCallbackArguments($call->getArguments(), $exclude);
             $callback = new Callback(array($instance, $call->getMethodName()));
             $callback->invokeWithArrayArguments($arguments);
         }
     }
     return $instance;
 }
Ejemplo n.º 5
0
 /**
  * Reads the calls from the provided dependency element and adds them to
  * the dependency instance
  * @param zibo\core\di\Dependency $dependency
  * @param DOMElement $dependencyElement
  * @return null
  */
 private function readCalls(Dependency $dependency, DOMElement $dependencyElement)
 {
     $calls = array();
     $callElements = $dependencyElement->getElementsByTagName(self::TAG_CALL);
     foreach ($callElements as $callElement) {
         $methodName = $callElement->getAttribute(self::ATTRIBUTE_METHOD);
         $call = new DependencyCall($methodName);
         $argumentElements = $callElement->getElementsByTagName(self::TAG_ARGUMENT);
         foreach ($argumentElements as $argumentElement) {
             $type = $argumentElement->getAttribute(self::ATTRIBUTE_TYPE);
             $value = $argumentElement->getAttribute(self::ATTRIBUTE_VALUE);
             $extra = null;
             switch ($type) {
                 case DependencyCallArgument::TYPE_DEPENDENCY:
                     $id = $argumentElement->getAttribute(self::ATTRIBUTE_ID);
                     if ($id) {
                         $extra = $id;
                     }
                     break;
                 case DependencyCallArgument::TYPE_CONFIG:
                     if ($argumentElement->hasAttribute(self::ATTRIBUTE_DEFAULT_VALUE)) {
                         $extra = $argumentElement->getAttribute(self::ATTRIBUTE_DEFAULT_VALUE);
                     }
                     break;
             }
             $call->addArgument(new DependencyCallArgument($type, $value, $extra));
         }
         $dependency->addCall($call);
     }
 }