/** * 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)); } }
public function testGetId() { $id = 'id'; $dependency = new Dependency('className', $id); $this->assertEquals($id, $dependency->getId()); }
/** * 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; }