Esempio n. 1
0
 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);
 }
Esempio n. 2
0
 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);
 }
Esempio n. 3
0
 public function __construct(string $class, callable $callable)
 {
     parent::__construct($callable);
     $this->reflector = ReflectorFactory::buildReflectorByClassName($class);
     $this->class = $class;
 }
Esempio n. 4
0
 public function testReflectionGetMethods()
 {
     $ref1 = ReflectorFactory::buildReflectorByClassName(MethodReflectionTestObject::class);
     $class = $ref1->getCompactReferenceClass();
     $this->assertSame($class->getClassName(), MethodReflectionTestObject::class);
 }
Esempio n. 5
0
 /**
  * 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);
         }
     }
 }