/** * Injector constructor. * @param $injector */ public function __construct(Injector $injector = null) { $this->injector = $injector; if (!$injector) { $this->bind(Binding::init(LazyProxyBuilder::class)->toValue(LazyProxyBuilder::init()), bind(ProxyCache::class)->toValue(ProxyCache::init())); } }
public function testProxyByBuilder() { $injector = new Injector(null); $provider = ClassProvider::init(MethodReflectionTestObject::class); $builder = LazyProxyBuilder::init(); /** @var MethodReflectionTestObject $proxy */ $proxy = $builder->create($injector, $provider); $testFunction = function (MethodReflectionTestObject $a) { $this->assertInstanceOf(MethodReflectionTestObject::class, $a); return true; }; $this->assertEquals($proxy->getPri(), 409); $this->assertEquals($proxy->privateMethod(), "__call:privateMethod"); //private cant be called, call -> __call instead $this->assertEquals($proxy->publicMethod(), 'publicMethod'); $this->assertEquals($proxy->publicMethodWithoutModifier(), 'publicMethodWithoutModifier'); $this->assertEquals($proxy . "", '_TO_STRING_'); $this->assertEquals($proxy->getProvider(), 'PROVIDER'); $this->assertTrue($testFunction($proxy)); }
public function testProxyArrayAccess() { $injector = new Injector(null); $provider = ClassProvider::init(ArrayAccessTestObject::class); $builder = LazyProxyBuilder::init(); /** @var ArrayAccessTestObject $arr */ $arr = $builder->create($injector, $provider); $arr->hydrate(['a' => 'A', 'B' => 'b']); $testFunction = function (ArrayAccessTestObject $a) { $this->assertInstanceOf(ArrayAccessTestObject::class, $a); return true; }; $testFunction2 = function (\ArrayAccess $a) { $this->assertInstanceOf(\ArrayAccess::class, $a); return true; }; $this->assertTrue($testFunction($arr)); $this->assertTrue($testFunction2($arr)); ProxyTest::_isProxyTrait($arr); $this->assertTrue(isset($arr['a'])); $this->assertEquals($arr['a'], 'A'); $this->assertTrue(isset($arr['B'])); $this->assertEquals($arr['B'], 'b'); unset($arr['a']); $this->assertFalse(isset($arr['a'])); $arr['a'] = 'C'; $this->assertTrue(isset($arr['a'])); $this->assertEquals($arr['a'], 'C'); unset($arr['B']); foreach ($arr as $value) { $this->assertEquals('C', $value); } $arr['b'] = 'B'; /** @var \ArrayIterator $iterator */ $iterator = $arr->getIterator(); $this->assertEquals('C', $iterator->current()); $iterator->next(); $this->assertEquals('B', $iterator->current()); }
/** * @param Injector $injector * @return mixed */ function get(Injector $injector) { /** @var LazyProxyBuilder $builder */ $builder = LazyProxyBuilder::init(); return $builder->create($injector, $this->provider); }