예제 #1
0
파일: DI.php 프로젝트: ozziest/di
 public static function resolve($name)
 {
     $class = new ReflectionClass($name);
     $constructor = $class->getConstructor();
     $resolved = [];
     if ($constructor !== null) {
         foreach ($constructor->getParameters() as $index => $param) {
             $paramType = $param->getClass()->name;
             if (isset(self::$list[$paramType]) === false) {
                 if (class_exists($paramType) === false) {
                     throw new Exception("DI couldn't resolve the dependency: " . $paramType);
                 }
                 array_push($resolved, DI::resolve($paramType));
             } else {
                 if (is_string(self::$list[$paramType])) {
                     array_push($resolved, DI::resolve(self::$list[$paramType]));
                 } else {
                     $callable = self::$list[$paramType];
                     array_push($resolved, $callable());
                 }
             }
         }
     }
     return $class->newInstanceArgs($resolved);
 }
예제 #2
0
파일: UnitTest.php 프로젝트: ozziest/di
 public function test_bind_function()
 {
     DI::bind('OtherModel', function () {
         return new OtherModel("my_string");
     });
     $instance = DI::resolve('MyFunction');
     $this->assertInstanceOf('MyFunction', $instance);
 }