This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |