Ejemplo n.º 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;
 }
Ejemplo n.º 2
0
 /**
  * @param Builder $builder
  * @dataProvider containerBuilderProvider
  */
 public function testInvalidDefinition(Builder $builder)
 {
     if ($builder->precompiled) {
         // compilation fails, so can't test precompiled
         return;
     }
     $this->setExpectedException(DomainException::class);
     $definition = new Fixture\InvalidDefinition(NoArguments::class);
     $builder->addDefinition($definition);
     // Note: different builders throw at different times here
     $container = $builder->build();
     $container->get(Fixture\NoArguments::class);
 }