Beispiel #1
0
 public function testAliasProviderLazy()
 {
     $injector = new Injector(null);
     $injector->providers([Engine::class => ClassProvider::init(Engine::class)->lazy(), "AAA" => AliasProvider::init(Engine::class)]);
     $engine = $injector->get('AAA');
     $this->assertInstanceOf(Engine::class, $engine);
 }
Beispiel #2
0
 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());
 }
Beispiel #3
0
 public function getReflector()
 {
     return $this->provider->getReflector();
 }
Beispiel #4
0
<?php

namespace Brunt;

use Brunt\Provider\Classes\ClassProvider;
use BruntTest\Testobjects\Car;
use BruntTest\Testobjects\Controller;
use BruntTest\Testobjects\ControllerA;
use BruntTest\Testobjects\Engine;
use BruntTest\Testobjects\FastCar;
use BruntTest\Testobjects\HeavyEngine;
include_once 'fileloader.php';
$inj = new Injector();
$inj->provide(Car::class, ClassProvider::init(FastCar::class));
$inj->provide(Engine::class, ClassProvider::init(HeavyEngine::class));
$inj->provide(Controller::class, ClassProvider::init(ControllerA::class));
$inj->provide('%BASE_URL%', function () {
    return 'http://sysvyz.org/';
});
var_dump($inj->get(Car::class));
var_dump($inj->get(Controller::class));
Beispiel #5
0
 private function buildLazy($count)
 {
     $t = microtime(true);
     $injector = new Injector();
     $injector->provide(ControllerA::class, ClassProvider::init(ControllerA::class)->lazy());
     $injector->provide('%BASE_URL%', ValueProvider::init('%BASE_URL%'));
     $arr = [];
     for ($i = 0; $i < $count; $i++) {
         $arr[] = $injector->get(ControllerA::class);
     }
     return microtime(true) - $t;
 }
Beispiel #6
0
 public static function _DI_PROVIDERS()
 {
     return [RequestService::class => ClassProvider::init(RequestService::class)->singleton(), ServiceY::class => ClassProvider::init(ServiceY::class)->singleton(), ServiceZ::class => ClassProvider::init(ServiceZ::class)->singleton()->lazy(), Request::class => ClassProvider::init(Request::class)->singleton()] + parent::_DI_PROVIDERS();
 }
Beispiel #7
0
 public static function _DI_PROVIDERS()
 {
     return ['%FRONT_TIRE%' => ClassProvider::init(SmallTire::class, false), '%BACK_TIRE%' => ClassProvider::init(HeavyTire::class, false)] + parent::_DI_PROVIDERS();
 }
 public function testProvidersLazySingleton2()
 {
     // Arrange
     $injector = new Injector(null);
     $injector->providers([Tire::class => ClassProvider::init(HeavyTire::class)->singleton()->lazy()]);
     /** @var Tire|ProxyTrait $entity */
     $entity = $injector->{Tire::class};
     $this->assertInstanceOf(Tire::class, $entity);
     $this->assertInstanceOf(HeavyTire::class, $entity);
     //not
     $this->assertEquals($entity->type, 'HeavyTire');
     $this->assertSame($entity->getInstance(), $injector->{Tire::class}->getInstance());
     $entity->a = "a";
     $this->assertEquals("a", $injector->{Tire::class}->a);
 }
Beispiel #9
0
 public static function _DI_PROVIDERS()
 {
     return ['%BASE_URL%' => ValueProvider::init('http://www.sysvyz.org'), ServiceY::class => ClassProvider::init(ServiceY::class)] + parent::_DI_PROVIDERS();
 }
Beispiel #10
0
 public static function _DI_PROVIDERS()
 {
     return [Tire::class => ClassProvider::init(SmallTire::class, false), '%CAR_NAME%' => ValueFactoryProvider::init(function () {
         return 'CAR';
     })];
 }
Beispiel #11
0
 public function testNonInjectableDependencyFail()
 {
     // Arrange
     $injector = new Injector(null);
     $injector->providers([NonInjectableWrapper::class => ClassProvider::init(NonInjectableWrapper::class)]);
     /** @var NonInjectableWrapper $wrapper */
     try {
         $wrapper = $injector->get(NonInjectableWrapper::class);
         $this->assertTrue(false);
     } catch (InjectableException $e) {
         $this->assertTrue(true);
     }
 }
Beispiel #12
0
 public function testDependecies()
 {
     // Arrange
     $injector = new Injector(null);
     $injector->{Car::class}(ClassProvider::init(FastCar::class));
     $injector->{Engine::class}(ClassProvider::init(Engine::class)->singleton());
     /** @var Car $car */
     $car = $injector->get(Car::class);
     /** @var Engine $engine */
     $engine = $injector->get(Engine::class);
     $this->assertSame($car->engine, $engine);
     $this->assertNotSame($car->tire0, $car->tire1);
     $this->assertNotSame($car->tire0, $car->tire2);
     $this->assertNotSame($car->tire0, $car->tire3);
     $this->assertNotSame($car->tire1, $car->tire2);
     $this->assertNotSame($car->tire1, $car->tire3);
     $this->assertNotSame($car->tire2, $car->tire3);
     $this->assertInstanceOf(SmallTire::class, $car->tire0);
     $this->assertInstanceOf(SmallTire::class, $car->tire1);
     $this->assertInstanceOf(HeavyTire::class, $car->tire2);
     $this->assertInstanceOf(HeavyTire::class, $car->tire3);
 }
Beispiel #13
0
 public function providers(array $providers = [])
 {
     foreach ($providers as $name => $provider) {
         if (is_int($name) && is_string($provider)) {
             $this->provide($provider, ClassProvider::init($provider));
         } else {
             if ($provider instanceof ProviderInterface) {
                 $this->provide($name, $provider);
             } else {
                 if (is_string($provider) && class_exists($provider)) {
                     $this->provide($name, ClassProvider::init($provider));
                 }
             }
         }
     }
 }