<?php

class DependencyInjector {

	private $_dependencyArray=array();

	public function Register($interface, $class) {
		if (isset(class_implements($class)[$interface])) {
			$this->_dependencyArray[$interface]=$class;
		} else
			throw new Exception("Class '$class' does not implement interface '$interface'");
	}

	public function Resolve($interface) {
		$class=new ReflectionClass($this->_dependencyArray[$interface]);
		$constructor=$class->getConstructor();
		$callArgs=array();
		if ($constructor!=null){
			$methodParams=$constructor->getParameters();
			foreach ($methodParams as $methodParam) {
				$index=$methodParam->getPosition();
				$type=$methodParam->getClass();
				$callArgs[$index]=$this->Resolve($type->getName());
			}
			$item=$class->newInstanceArgs($callArgs);
		}else{
			$item=new $this->_dependencyArray[$interface];
		}
		#$item->DependencyInjector=$this;
		return $item;
	}

}
