Пример #1
0
 public function testSingletonWithPersistence()
 {
     $container = new Container();
     $container->singleton('mySingleton', '\\stdClass');
     $stdClass = $container->build('mySingleton');
     $stdClass->testVar = 'value';
     $stdClass2 = $container->build('mySingleton');
     $this->assertEquals('value', $stdClass2->testVar);
 }
Пример #2
0
 /**
  * Magic method. Runs when using this class as a function.
  * Ex: $object = new Definition($container, $class);
  *     $invoked = $object();
  *
  * @return object The instantiated $class with optional args passed to the constructor and methods called.
  */
 public function __invoke()
 {
     $this->mergeInheritedDependencies();
     if (empty($this->arguments)) {
         $object = $this->container->build($this->class);
     } else {
         $reflection = new \ReflectionClass($this->class);
         $arguments = [];
         foreach ($this->arguments as $arg) {
             if (is_string($arg) && (class_exists($arg) || $this->container->bound($arg))) {
                 $arguments[] = $this->container->resolve($arg);
                 continue;
             }
             $arguments[] = $arg;
         }
         $object = $reflection->newInstanceArgs($arguments);
     }
     if ($object instanceof ContainerAwareInterface) {
         $this->withMethod('setContainer', [$this->container]);
     }
     return $this->callMethods($object);
 }
Пример #3
0
 /**
  * Magic method. Runs when using this class as a function.
  * Ex: $object = new Definition($container, $class);
  *     $invoked = $object();
  *
  * @return object The instantiated $class with optional args passed to the constructor and methods called.
  */
 public function __invoke()
 {
     if (empty($this->arguments)) {
         if (empty($this->methods)) {
             $object = $this->container->build($this->class);
         } else {
             $object = new $this->class();
         }
     } else {
         $reflection = new \ReflectionClass($this->class);
         $arguments = array();
         foreach ($this->arguments as $arg) {
             if (is_string($arg) && (class_exists($arg) || $this->container->bound($arg))) {
                 $arguments[] = $this->container->resolve($arg);
                 continue;
             }
             $arguments[] = $arg;
         }
         $object = $reflection->newInstanceArgs($arguments);
     }
     return $this->callMethods($object);
 }