コード例 #1
0
 /**
  * @test
  */
 public function classAnnotatedWithSingletonWillOnlyBeCreatedOnce()
 {
     $binder = new Binder();
     $binder->bind(Number::class)->to(RandomSingleton::class);
     $slot = $binder->getInjector()->getInstance(SlotMachine::class);
     assert($slot->number1, isInstanceOf(RandomSingleton::class)->and(isSameAs($slot->number2)));
 }
コード例 #2
0
 /**
  * @test
  */
 public function injectWithProviderClass()
 {
     $binder = new Binder();
     $binder->bind(Answer::class)->toProviderClass(new \ReflectionClass(MyProviderClass::class));
     $question = $binder->getInjector()->getInstance(AnotherQuestion::class);
     assert($question->getAnswer(), isInstanceOf(Answer::class));
 }
コード例 #3
0
 /**
  * @test
  */
 public function otherScopeIsUsedToCreateInstance()
 {
     $binder = new Binder();
     $instance = new \stdClass();
     $binder->bind(\stdClass::class)->to(\stdClass::class)->in(NewInstance::of(BindingScope::class)->mapCalls(['getInstance' => $instance]));
     assert($binder->getInjector()->getInstance(\stdClass::class), isSameAs($instance));
 }
コード例 #4
0
 /**
  * @test
  */
 public function namedConstructorInjectionWithMultipleParamAndNamedParamGroup()
 {
     $binder = new Binder();
     $binder->bind(Employee::class)->named('schst')->to(Boss::class);
     $binder->bind(Employee::class)->to(TeamMember::class);
     $injector = $binder->getInjector();
     $group = $injector->getInstance(DevelopersMultipleConstructorParamsGroupedName::class);
     assert($group, equals(new DevelopersMultipleConstructorParamsGroupedName(new Boss(), new Boss())));
 }
コード例 #5
0
 /**
  * @test
  */
 public function injectWithClosure()
 {
     $binder = new Binder();
     $answer = new Answer();
     $binder->bind(Answer::class)->toClosure(function () use($answer) {
         return $answer;
     });
     $question = $binder->getInjector()->getInstance(AnotherQuestion::class);
     assert($question, isInstanceOf(AnotherQuestion::class));
     assert($question->getAnswer(), isSameAs($answer));
 }
コード例 #6
0
 /**
  * @test
  */
 public function instanceCreationThrowsBindingExceptionWhenNoPropertiesBound()
 {
     $injector = Binder::createInjector();
     expect(function () use($injector) {
         $injector->getInstance(PropertyReceiver::class);
     })->throws(BindingException::class)->message(startsWith('Can not inject into ' . PropertyReceiver::class . '::__construct($foo).' . ' No binding for type __PROPERTY__ (named "example.foo") specified.'));
 }
コード例 #7
0
 /**
  * @since  2.1.0
  * @test
  * @group  issue_31
  */
 public function injectConstantViaClosure()
 {
     $binder = new Binder();
     $binder->bindConstant('answer')->toClosure(function () {
         return 42;
     });
     $this->assertConstantInjection($binder->getInjector());
 }
コード例 #8
0
 /**
  * @test
  */
 public function explicitBindingOverwritesProvidedByAnnotation()
 {
     $binder = new Binder();
     $binder->bind(Person2::class)->to(Mikey::class);
     assert($binder->getInjector()->getInstance(Person2::class), isInstanceOf(Mikey::class));
 }
コード例 #9
0
 /**
  * @test
  * @since  4.1.3
  */
 public function propertyBindingUsedWhenParamHasTypeHintButIsAnnotated()
 {
     try {
         $binder = new Binder();
         $properties = new Properties(['config' => ['example.password' => 'somePassword']]);
         $binder->bindProperties($properties, 'PROD');
         $example = $binder->getInjector()->getInstance(Example::class);
         assert($example->password, isInstanceOf(Secret::class));
     } finally {
         // ensure all references are removed to clean up environment
         unset($properties);
         $example->password = null;
         unset($example);
         unset($binder);
         gc_collect_cycles();
     }
 }
コード例 #10
0
 /**
  * configure the binder
  *
  * @param  \stubbles\ioc\Binder  $binder
  * @param  string                $projectPath  optional  project base path
  */
 public function configure(Binder $binder, string $projectPath = null)
 {
     $binder->bind('bar')->to('stdClass');
 }
コード例 #11
0
 /**
  * creates plugin handler instance
  *
  * @param   Binder  $binder
  * @return  PluginHandler
  */
 private function createPluginHandler(Binder $binder) : PluginHandler
 {
     return $binder->getInjector()->getInstance(PluginHandler::class);
 }
コード例 #12
0
 /**
  * @test
  * @since  6.0.0
  */
 public function throwsBindingExceptionWhenNoFallbackSpecifiedAndNoModeSet()
 {
     $injector = Binder::createInjector();
     expect(function () use($injector) {
         $injector->getInstance(Person4::class);
     })->throws(BindingException::class);
 }
コード例 #13
0
 /**
  * set up test environment
  */
 public function setUp()
 {
     $binder = new Binder();
     $binder->bind(Person2::class)->to(Mikey::class)->inSession();
     $this->injector = $binder->getInjector();
 }
コード例 #14
0
 /**
  * @test
  * @since  5.1.0
  */
 public function constructorInjectionWithOptionalSecondParam()
 {
     $injector = Binder::createInjector(function (Binder $binder) {
         $binder->bind(Tire::class)->to(Goodyear::class);
     });
     $bike = $injector->getInstance(BikeWithOptionalOtherParam::class);
     assert($bike->other, equals('foo'));
 }
コード例 #15
0
ファイル: Runtime.php プロジェクト: stubbles/stubbles-ioc
 /**
  * configure the binder
  *
  * @param  \stubbles\ioc\Binder  $binder
  * @param  string                $projectPath  optional  project base path
  */
 public function configure(Binder $binder, string $projectPath = null)
 {
     $this->environment->registerErrorHandler($projectPath);
     $this->environment->registerExceptionHandler($projectPath);
     $binder->setEnvironment($this->environment->name())->bind(Environment::class)->toInstance($this->environment);
     if (file_exists($this->propertiesFile($projectPath))) {
         $binder->bindPropertiesFromFile($this->propertiesFile($projectPath), $this->environment->name());
     }
     $binder->bindConstant('stubbles.project.path')->to($projectPath);
     foreach ($this->buildPathes($projectPath) as $name => $value) {
         $binder->bindConstant($name)->to($value);
     }
 }