/** * @param Builder $builder * @dataProvider containerBuilderProvider */ public function testClosureScalarReturnTypeDeclaration(Builder $builder) { $builder->define()->name('foo')->using(static function () : string { return 'bar'; })->build(); $container = $builder->build(); $this->assertSame('bar', $container->get('foo')); }
/** * @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; }
public function containerBuilderProvider() { static $counter; // Generate a temp file $tmpDir = __DIR__ . '/tmp/'; $tmpFilePrefix = 'Integration' . sprintf('%02d', ++$counter); $tmpFileSuffix = '.php'; $counter2 = 0; do { $className = $tmpFilePrefix . ($counter2++ ? '_' . $counter2 : ''); //$className = $tmpFilePrefix . base_convert(mt_rand(0, PHP_INT_MAX), 10, 36); $tmpFile = $tmpDir . $className . $tmpFileSuffix; } while (file_exists($tmpFile)); // Make the container builders $defaultContainerBuilder = new Builder(); $compileContainerBuilder = new Builder(); $compileContainerBuilder->ttl(0)->stat(false)->file($tmpFile)->className('zdi\\Tests\\Gen\\' . $className); $precompiledContainerBuilder = clone $compileContainerBuilder; $precompiledContainerBuilder->precompiled(true); return array(array($defaultContainerBuilder), array($compileContainerBuilder), array($precompiledContainerBuilder)); }
private function getBuilder() { list($class, $tmpFile) = $this->mktmp(); $namespace = 'zdi\\Tests\\Gen'; $builder = new ContainerBuilder(); $builder->file($tmpFile); $builder->className($namespace . '\\' . $class); $builder->define(Fixture\NoArguments::class)->build(); return $builder; }
public function define(ContainerBuilder $builder) { $builder->define(NoArguments::class)->build(); $builder->define(OneObjectArgument::class)->build(); }