コード例 #1
0
ファイル: CallableDefinitionTest.php プロジェクト: fwk/di
 /**
  */
 public function testInvokeWithErroneousArguments()
 {
     $this->object->setCallable('date_default_timezone_set');
     $this->object->addArgument(new Reference('invalid_ref'));
     $this->setExpectedException('Fwk\\Di\\Exceptions\\InvalidCallableDefinitionException');
     $it = $this->object->invoke($this->getContainer());
 }
コード例 #2
0
ファイル: ContainerTest.php プロジェクト: fwk/di
 public function testSharedSetAndGetCallable()
 {
     $this->assertFalse($this->object->has('test'));
     $callable = function () {
         $a = new \stdClass();
         $a->mt = microtime(true);
         return $a;
     };
     $this->object->set('test', CallableDefinition::factory($callable)->setShared(true));
     $this->assertTrue($this->object->has('test'));
     $inst = $this->object->get('test');
     $this->assertInstanceOf('stdClass', $inst);
     $this->assertTrue($inst === $this->object->get('test'));
 }
コード例 #3
0
ファイル: Container.php プロジェクト: fwk/di
 /**
  * Registers a definition
  * 
  * @param string  $name             Identifier
  * @param DefinitionInterface|mixed $definition Definition, callable or value
  *
  * @return Container
  */
 public function set($name, $definition)
 {
     if (!$definition instanceof DefinitionInterface) {
         if (is_callable($definition)) {
             $definition = CallableDefinition::factory($definition);
         } elseif (is_array($definition)) {
             $definition = ArrayDefinition::factory($definition);
         } else {
             $wasObj = is_object($definition);
             $definition = ScalarDefinition::factory($definition);
             if ($wasObj) {
                 $definition->setShared(true);
                 $this->_sharedInstances->attach($definition, $name);
             }
         }
     }
     $event = new BeforeServiceRegisteredEvent($this, $name, $definition);
     $this->notify($event);
     if ($event->isStopped()) {
         return $this;
     }
     $this->store->attach($definition, $name);
     $this->notify(new AfterServiceRegisteredEvent($this, $name, $definition));
     return $this;
 }