Skip to content

Commit

Permalink
added the ability to pass a logger
Browse files Browse the repository at this point in the history
Signed-off-by: waahhhh <40632052+waahhhh@users.noreply.github.com>
  • Loading branch information
waahhhh committed Aug 29, 2020
1 parent d2bebdb commit c3d25c6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 5 additions & 1 deletion docs/book/codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use Laminas\Di\Resolver\DependencyResolver;
use Laminas\Di\CodeGenerator\InjectorGenerator;

$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config)
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
$generator = new InjectorGenerator($config, $resolver);

// It is highly recommended to set the container that is used at runtime:
Expand Down Expand Up @@ -75,6 +75,9 @@ following keys (unknown keys are ignored):
this value is not provided, you will need to set it with the generator's
`setOutputDirectory()` method before calling `generate()`.

- `logger`: must be resolvable in container and must be an instance of `Psr\Log\LoggerInterface.`
By default `Psr\Log\NullLogger` is used. See the [Logging section](#logging) for details.

Below is an example detailing configuration of the generator factory:

```php
Expand All @@ -84,6 +87,7 @@ return [
'aot' => [
'namespace' => 'AppAoT\Generated',
'directory' => __DIR__ . '/../gen',
'logger' => Psr\Log\LoggerInterface::class,
],
],
],
Expand Down
7 changes: 6 additions & 1 deletion src/Container/GeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ public function create(ContainerInterface $container) : InjectorGenerator
$config = $container->has('config') ? $container->get('config') : [];
$aotConfig = $config['dependencies']['auto']['aot'] ?? [];
$namespace = $aotConfig['namespace'] ?? null;
$logger = null;

$generator = new InjectorGenerator($diConfig, $resolver, $namespace);
if (isset($aotConfig['logger'])) {
$logger = $container->get($aotConfig['logger']);
}

$generator = new InjectorGenerator($diConfig, $resolver, $namespace, $logger);

if (isset($aotConfig['directory'])) {
$generator->setOutputDirectory($aotConfig['directory']);
Expand Down
36 changes: 36 additions & 0 deletions test/Container/GeneratorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
* @covers Laminas\Di\Container\GeneratorFactory
Expand Down Expand Up @@ -87,6 +89,40 @@ public function testSetsNamespaceFromConfig() : void
$this->assertEquals($expected, $generator->getNamespace());
}

public function testDefaultLogger() : void
{
$generator = (new GeneratorFactory())->create(new ServiceManager());
$reflection = new \ReflectionClass($generator);
$property = $reflection->getProperty('logger');
$property->setAccessible(true);

$this->assertInstanceOf(NullLogger::class, $property->getValue($generator));
}

public function testSetsLoggerFromConfig() : void
{
$logger = $this->getMockBuilder(LoggerInterface::class)
->getMockForAbstractClass();
$container = new ServiceManager();
$container->setService('MyCustomLogger', $logger);
$container->setService('config', [
'dependencies' => [
'auto' => [
'aot' => [
'logger' => 'MyCustomLogger',
],
],
],
]);

$generator = (new GeneratorFactory())->create($container);
$reflection = new \ReflectionClass($generator);
$property = $reflection->getProperty('logger');
$property->setAccessible(true);

$this->assertNotInstanceOf(NullLogger::class, $property->getValue($generator));
}

public function testInvokeCallsCreate() : void
{
$mock = $this->getMockBuilder(GeneratorFactory::class)
Expand Down

0 comments on commit c3d25c6

Please sign in to comment.