コード例 #1
0
ファイル: Bootstrap.php プロジェクト: niclaslindberg/webx-ioc
 /**
  * Initializes the IOC instance with an optional resolver function.
  * The init function also registers the single IOC instance with itself allowing the container to resolve it self when asked for instance of Ioc::class:
  * <code>
  *  $ioc = $ioc->get(Ioc::class);
  * </code>
  * @param \Closure|null $resolver
  * @throws \WebX\Ioc\IocException
  * @return Ioc
  */
 public static function init(\Closure $resolver = null)
 {
     $ico = new IocImpl($resolver);
     $ico->register($ico);
     self::$ico = $ico;
     return $ico;
 }
コード例 #2
0
 public function testTraversePass()
 {
     $ioc = new IocImpl();
     $ioc->register(A2::class, ["traverse" => true]);
     $a_1 = $ioc->get(IA::class);
     $a_11 = $ioc->get(IA2::class);
     $this->assertSame($a_1, $a_11);
 }
コード例 #3
0
 public function testWithParameterPass()
 {
     $ioc = new IocImpl();
     $param = "123";
     $ioc->register(UnknownVarNoDefault::class, ["parameters" => ["var" => $param]]);
     $unknown = $ioc->get(IUnknownVar::class);
     $this->assertNotNull($unknown);
     $this->assertSame($param, $unknown->getVar());
 }
コード例 #4
0
 public function testUnknownFailsWithClosureToDeliveryArray()
 {
     $array = [1];
     $closure = function (IocNonResolvable $nonResolvable) use($array) {
         return $array;
     };
     $ioc = new IocImpl($closure);
     $ioc->register(UnknownArrayNoDefault::class);
     $unknown = $ioc->get(IUnknownArray::class);
     $this->assertNotNull($unknown);
     $this->assertSame($array, $unknown->getArray());
 }
コード例 #5
0
 public function testInvokeClosureWithMappingsTypes()
 {
     $ioc = new IocImpl();
     $ioc->register(ClassA::class, ["id" => "1"]);
     $ioc->register(ClassA::class, ["id" => "2"]);
     $result = $ioc->invoke(function (array $as) {
         return $as;
     }, ["types" => ["as" => InterfaceA::class]]);
     $this->assertNotNull($result);
     $this->assertSame(2, count($result));
     $this->assertContainsOnlyInstancesOf(InterfaceA::class, $result);
 }
コード例 #6
0
 public function testStaticRegister2Arg_Pass()
 {
     $ioc = new IocImpl();
     $ioc->register(C::class);
     $ioc->register(A::class);
     $ioc->initStatic(CStatic::class, "initAB");
     $cStatic = new CStatic();
     $this->assertInstanceOf(IC::class, CStatic::$c);
     $sum = $cStatic->executeAdd(1, 2);
     $this->assertEquals(3, $sum);
     $res = $cStatic->executeDoA();
     $this->assertEquals("A", $res);
 }
コード例 #7
0
 public function testCreateClassWithDependenciesAndParameter_Pass()
 {
     $ioc = new IocImpl();
     $testValue = "Hello";
     $ioc->register(ClassA::class);
     $ioc->register(ClassB::class, ["factory" => function (InterfaceA $a, $value) {
         return new ClassB($a, $value);
     }, "parameters" => ["value" => $testValue]]);
     $a = $ioc->get(InterfaceA::class);
     $this->assertNotNull($a);
     $b = $ioc->get(InterfaceB::class);
     $this->assertNotNull($b);
     $this->assertEquals($testValue, $b->getValue());
     $this->assertInstanceOf(InterfaceA::class, $a);
     $this->assertInstanceOf(InterfaceB::class, $b);
     $this->assertSame($a, $b->getA());
 }
コード例 #8
0
 public function testRegisterUnknownCallsResolverWithId()
 {
     $idHistory = [];
     $resolver = function (IocNonResolvable $nonResolvable) use(&$idHistory) {
         $config = $nonResolvable->config();
         $this->assertNotNull($config);
         $idHistory[] = $config["id"];
         return $config["id"];
     };
     $ioc = new IocImpl($resolver);
     $ioc->register(UnknownVarNoDefault::class, ["id" => "i1"]);
     $ioc->register(UnknownVarNoDefault::class, ["id" => "i2"]);
     $i1 = $ioc->get(IUnknownVar::class, "i1");
     $i2 = $ioc->get(IUnknownVar::class, "i2");
     $this->assertEquals(2, count($idHistory));
     $this->assertEquals("i1", $idHistory[0]);
     $this->assertEquals("i2", $idHistory[1]);
 }
コード例 #9
0
 public function testResolveMultipleDependencyPass()
 {
     $ioc = new IocImpl();
     $ioc->register(DependentAB::class);
     $ioc->register(A::class);
     $ioc->register(B::class);
     $a = $ioc->get(IA::class);
     $b = $ioc->get(IB::class);
     $dependentAB = $ioc->get(IDependentAB::class);
     $this->assertNotNull($dependentAB);
     $this->assertNotNull($a);
     $this->assertNotNull($b);
     $this->assertSame($a, $dependentAB->getA());
     $this->assertSame($b, $dependentAB->getB());
 }
コード例 #10
0
 public function testWithTypeSuccessA()
 {
     $ioc = new IocImpl();
     $ioc->register(B::class);
     $ioc->register(A::class);
     $ioc->register(A2::class);
     $ioc->register(DependentArrayA::class, ["types" => ["a" => IA::class]]);
     $dependentA = $ioc->get(IDependentArrayA::class);
     $this->assertEquals(2, count($dependentA->getAs()));
     $this->assertInstanceOf(IA::class, $dependentA->getAs()[0]);
     $this->assertInstanceOf(IA::class, $dependentA->getAs()[1]);
 }
コード例 #11
0
 public function testInstantiateWithConcreteClassDependency()
 {
     $ioc = new IocImpl();
     $b = new B();
     $ioc->register($b, ["registerClass" => true]);
     $a = $ioc->instantiate(DependentA_ConcreteB::class);
     $this->assertInstanceOf(DependentA_ConcreteB::class, $a);
     $this->assertSame($b, $a->getB());
 }