Example #1
0
 /**
  * @return Definition
  * @throws Exception\DomainException
  */
 public function build()
 {
     if (is_string($this->provider)) {
         $definition = new ClassDefinition($this->provider, $this->class, $this->name, $this->flags);
     } else {
         if ($this->provider instanceof Closure) {
             $closure = $this->provider;
             $reflectionFunction = new ReflectionFunction($closure);
             $this->convertReturnType($reflectionFunction);
             $params = $this->convertParameters($reflectionFunction->getParameters());
             $definition = new ClosureDefinition($closure, $params, $this->class, $this->name, $this->flags);
         } else {
             if ($this->class) {
                 $reflectionClass = new ReflectionClass($this->class);
                 if ($reflectionClass->isAbstract() || $reflectionClass->isInterface()) {
                     throw new Exception\DomainException('Cannot build abstract class or interface');
                 }
                 $params = $this->convertConstructor($reflectionClass);
                 $setters = $this->convertSetters($reflectionClass);
                 $definition = new DataDefinition($params, $setters, $this->class, $this->name, $this->flags);
             } else {
                 throw new Exception\DomainException('Unable to determine definition type');
             }
         }
     }
     // Add to container
     if (null !== $this->container) {
         $this->container->addDefinition($definition);
         if ($this->alias) {
             $this->container->alias($this->alias, $this->class);
         }
     }
     return $definition;
 }
Example #2
0
 /**
  * @param Builder $builder
  * @dataProvider containerBuilderProvider
  */
 public function testGlobalAliasedParam(Builder $builder)
 {
     $builder->define()->name('stralias')->using(static function () {
         return 'val';
     })->setGlobal()->build();
     $builder->alias('str', 'stralias');
     $builder->define(Fixture\OneScalarArgument::class)->build();
     $container = $builder->build();
     $this->assertSame('val', $container->get(Fixture\OneScalarArgument::class)->getString());
 }