Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge 8153e4d into cb23064
Browse files Browse the repository at this point in the history
  • Loading branch information
tux-rampage committed Dec 8, 2017
2 parents cb23064 + 8153e4d commit 29aa719
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/CodeGenerator/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ public function setOutputDirectory(string $dir, ?int $mode = null) : self

return $this;
}

/**
* @return string
*/
public function getOutputDirectory(): ?string
{
return $this->outputDirectory;
}
}
8 changes: 8 additions & 0 deletions src/CodeGenerator/InjectorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ private function generateAutoload()
$this->autoloadGenerator->generate($classmap);
}

/**
* Returns the namespace this generator uses
*/
public function getNamespace(): string
{
return $this->namespace;
}

/**
* Generate the injector
*
Expand Down
3 changes: 2 additions & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function getDependencyConfig() : array
return [
'factories' => [
InjectorInterface::class => Container\InjectorFactory::class,
ConfigInterface::class => Container\ConfigFactory::class
ConfigInterface::class => Container\ConfigFactory::class,
CodeGenerator\InjectorGenerator::class => Container\GeneratorFactory::class,
],
'abstract_factories' => [
Container\ServiceManager\AutowireFactory::class
Expand Down
49 changes: 49 additions & 0 deletions src/Container/GeneratorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* @see https://github.com/zendframework/zend-di for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-di/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Di\Container;

use Psr\Container\ContainerInterface;
use Zend\Di\CodeGenerator\InjectorGenerator;
use Zend\Di\ConfigInterface;
use Zend\Di\Definition\RuntimeDefinition;
use Zend\Di\Resolver\DependencyResolver;

class GeneratorFactory
{
private function getConfig(ContainerInterface $container)
{
if ($container->has(ConfigInterface::class)) {
return $container->get(ConfigInterface::class);
}

return (new ConfigFactory())->create($container);
}

public function create(ContainerInterface $container)
{
$config = $container->has('config') ? $container->get('config') : [];
$diConfig = $this->getConfig($container);
$aotConfig = $config['dependencies']['auto']['aot'] ?? [];
$resolver = new DependencyResolver(new RuntimeDefinition(), $diConfig);
$namespace = $aotConfig['namespace'] ?? null;

$resolver->setContainer($container);
$generator = new InjectorGenerator($diConfig, $resolver, $namespace);

if (isset($aotConfig['directory'])) {
$generator->setOutputDirectory($aotConfig['directory']);
}

return $generator;
}

public function __invoke(ContainerInterface $container)
{
return $this->create($container);
}
}
31 changes: 29 additions & 2 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,50 @@
use PHPUnit\Framework\TestCase;
use Zend\Di\ConfigProvider;
use PHPUnit\Framework\Constraint\IsType;
use Zend\Di\CodeGenerator\InjectorGenerator;
use Zend\Di\InjectorInterface;
use Zend\Di\ConfigInterface;

/**
* @coversDefaultClass Zend\Di\Module
*/
class ConfigProviderTest extends TestCase
{
public function testInstanceIsInvokable()
public function testInstanceIsInvokable(): void
{
$this->assertInternalType(IsType::TYPE_CALLABLE, new ConfigProvider());
}

public function testProvidesDependencies()
public function testProvidesDependencies(): void
{
$provider = new ConfigProvider();
$result = $provider();

$this->assertArrayHasKey('dependencies', $result);
$this->assertEquals($provider->getDependencyConfig(), $result['dependencies']);
}

/**
* Provides service names that should be defined with a factory
*/
public function provideExpectedServicesWithFactory(): iterable
{
return [
// service name
'injector' => [ InjectorInterface::class ],
'config' => [ ConfigInterface::class ],
'generator' => [ InjectorGenerator::class ],
];
}

/**
* Test if a service is provided by factory definition
*
* @dataProvider provideExpectedServicesWithFactory
*/
public function testProvidesFactoryDefinition(string $serviceName): void
{
$result = (new ConfigProvider())->getDependencyConfig();
$this->assertArrayHasKey($serviceName, $result['factories']);
}
}
87 changes: 87 additions & 0 deletions test/Container/GeneratorFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* @see https://github.com/zendframework/zend-di for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-di/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Di\Container;

use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Zend\Di\CodeGenerator\InjectorGenerator;
use Zend\Di\Config;
use Zend\Di\ConfigInterface;
use Zend\Di\Container\GeneratorFactory;
use Zend\Di\Injector;
use Zend\ServiceManager\ServiceManager;

/**
* @covers Zend\Di\Container\GeneratorFactory
*
*/
class GeneratorFactoryTest extends TestCase
{
public function testInvokeCreatesGenerator()
{
$injector = new Injector();
$factory = new GeneratorFactory();

$result = $factory->create($injector->getContainer());
$this->assertInstanceOf(InjectorGenerator::class, $result);
}

public function testFactoryUsesDiConfigContainer(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMockForAbstractClass();
$container->method('has')->willReturnCallback(function ($type) {
return ($type == ConfigInterface::class);
});

$container->expects($this->atLeastOnce())
->method('get')
->with(ConfigInterface::class)
->willReturn(new Config());

$factory = new GeneratorFactory();
$factory->create($container);
}

public function testSetsOutputDirectoryFromConfig()
{
$vfs = vfsStream::setup(uniqid('zend-di'));
$expected = $vfs->url();
$container = new ServiceManager();
$container->setService('config', [
'dependencies' => [
'auto' => [
'aot' => [
'directory' => $expected
],
],
],
]);

$generator = (new GeneratorFactory())->create($container);
$this->assertEquals($expected, $generator->getOutputDirectory());
}

public function testSetsNamespaceFromConfig()
{
$expected = 'ZendTest\\Di\\' . uniqid('Generated');
$container = new ServiceManager();
$container->setService('config', [
'dependencies' => [
'auto' => [
'aot' => [
'namespace' => $expected,
],
],
],
]);

$generator = (new GeneratorFactory())->create($container);
$this->assertEquals($expected, $generator->getNamespace());
}
}

0 comments on commit 29aa719

Please sign in to comment.