Пример #1
0
 public function __construct(Engine $engine, Injector $injector)
 {
     $this->engine = $engine;
     $this->tire0 = $injector->get('%FRONT_TIRE%');
     $this->tire1 = $injector->get('%FRONT_TIRE%');
     $this->tire2 = $injector->get('%BACK_TIRE%');
     $this->tire3 = $injector->get('%BACK_TIRE%');
     $this->name = 'FASTCAR';
 }
Пример #2
0
 /**
  * Car constructor.
  * @param Engine $engine
  * @param Injector $injector
  * @param string $name
  * @internal param Tire $tire
  */
 public function __construct(Engine $engine, Injector $injector, string $name)
 {
     $this->engine = $engine;
     $this->tire0 = $injector->get(Tire::class);
     $this->tire1 = $injector->get(Tire::class);
     $this->tire2 = $injector->get(Tire::class);
     $this->tire3 = $injector->get(Tire::class);
     $this->name = $name;
 }
Пример #3
0
 public function testBinding()
 {
     $injector = new Injector(null);
     $injector->bind([Binding::init(HeavyEngine::class)->lazy(), bind("AAA")->toAlias(HeavyEngine::class)]);
     $engine = $injector->get('AAA');
     $this->assertInstanceOf(Engine::class, $engine);
 }
Пример #4
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);
 }
Пример #5
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));
Пример #6
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;
 }
Пример #7
0
 /**
  * @param Injector $injector
  * @return mixed
  */
 function get(Injector $injector)
 {
     return $injector->get($this->alias);
 }
Пример #8
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);
     }
 }
Пример #9
0
 public function testBindings()
 {
     // Arrange
     $injector = new Injector(null);
     $injector->bind(bind(Car::class)->toClass(FastCar::class), bind(Engine::class)->toClass(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);
 }