コード例 #1
0
ファイル: ProxyTest.php プロジェクト: sysvyz/brunt
 public function testProxyRenderer()
 {
     $ref1 = ReflectorFactory::buildReflectorByClassName(MethodReflectionTestObject::class);
     $crclass = $ref1->getCompactReferenceClass();
     //        print_r($crclass->toArray());
     $renderer = new ProxyRenderer($crclass, 'RandomProxyName_ds8bfgFHGTG4');
     $renderedClass = $renderer->render();
     $injector = new Injector(null);
     $provider = ClassProvider::init(MethodReflectionTestObject::class);
     /** @var MethodReflectionTestObject $proxy */
     $proxy = null;
     eval($renderedClass . '$proxy = new \\Brunt\\ProxyObject\\RandomProxyName_ds8bfgFHGTG4($provider, $injector);');
     $this->assertInstanceOf(MethodReflectionTestObject::class, $proxy);
 }
コード例 #2
0
ファイル: Invoker.php プロジェクト: sysvyz/brunt
 public function invoke($object, $function)
 {
     //TODO opimize
     $reflector = ReflectorFactory::buildReflectorByClassName(get_class($object));
     $reflectionMethod = new \ReflectionMethod($object, $function);
     $params = $reflectionMethod->getParameters();
     $ps = [];
     foreach ($params as $param) {
         $ps[$param->getName()] = new ReflectiveCRParam($param->getName() . "", $param);
     }
     $crMethod = new ReflectiveCRMethod($reflectionMethod, $ps);
     $dependencies = $reflector->resolveDependencies($crMethod->getParams(), $function);
     $params = array_map(function ($dependency) {
         return $this->injector->get($dependency['token']);
     }, $dependencies);
     return call_user_func_array([$object, $function], $params);
 }
コード例 #3
0
ファイル: ClassFactoryProvider.php プロジェクト: sysvyz/brunt
 public function __construct(string $class, callable $callable)
 {
     parent::__construct($callable);
     $this->reflector = ReflectorFactory::buildReflectorByClassName($class);
     $this->class = $class;
 }
コード例 #4
0
ファイル: RelfectorTest.php プロジェクト: sysvyz/brunt
 public function testReflectionGetMethods()
 {
     $ref1 = ReflectorFactory::buildReflectorByClassName(MethodReflectionTestObject::class);
     $class = $ref1->getCompactReferenceClass();
     $this->assertSame($class->getClassName(), MethodReflectionTestObject::class);
 }
コード例 #5
0
ファイル: ClassProvider.php プロジェクト: sysvyz/brunt
 /**
  * detect circular dependencies -> maybe not in production?
  * if A depends on B and B on A then the injector cant resolve this by himself.
  * dependencies MUST be A DAG https://en.wikipedia.org/wiki/Directed_acyclic_graph
  * Validator uses depth-first-search to find loops - there are better ways.
  *
  * todo research: claim: it could be possible to have circular dependencies with bounds or inheritance if they converge somehow
  * circular dependencies should be avoided or if REALLY (recursive structures i.e. lists, trees, graphs) necessary build it with FactoryProviders
  * http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/
  *
  * @param Reflector $reflector
  * @param array $path
  */
 private static function validate(Reflector $reflector, $path = [])
 {
     //get class name
     $className = $reflector->getClassName();
     if ($className == Injector::class) {
         return;
     }
     if (in_array($className, $path)) {
         throw new CircularDependencyException($className . ' must not depend on it self');
     }
     array_push($path, $reflector->getClassName());
     /** @var CRParam $dependency */
     foreach ($reflector->getConstructorParams() as $dependency) {
         if ($dependency->hasType() && !$dependency->isBuiltin()) {
             $nextReflector = ReflectorFactory::buildReflectorByClassName($dependency->getType() . '');
             self::validate($nextReflector, $path);
         }
     }
 }