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

Inject logger instance via constructor #34

Merged
merged 12 commits into from
Jan 26, 2018
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- [#32](https://github.com/zendframework/zend-di/pull/32) adds the implementation of
`Psr\Log\LoggerAwareInterface` to `Zend\Di\CodeGenerator\InjectorGenerator`
- [#34](https://github.com/zendframework/zend-di/pull/34) adds ability to pass
`Psr\Log\LoggerInterface` to `Zend\Di\CodeGenerator\InjectorGenerator`'s constructor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rephrase slightly:

... adds the ability to pass a Psr\Log\LoggerInterface instance to the constructor of Zend\Di\CodeGenerator\InjectorGenerator

(e.g. `new InjectorGenerator($config, $resolver, $namespace, $logger)`)

- [#31](https://github.com/zendframework/zend-di/pull/31) adds the service
factory `Zend\Di\Container\GeneratorFactory` for creating a
Expand Down
4 changes: 2 additions & 2 deletions docs/book/codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ return [

## Logging

The `InjectorGenerator` implements the PSR-3 [`LoggerAwareInterface`](http://www.php-fig.org/psr/psr-3/#4-psrlogloggerawareinterface).
So you can pass any PSR-3 logger to its `setLogger()` method.
The `InjectorGenerator` allows passing a [PSR-3 logger](http://www.php-fig.org/psr/psr-3/) instance
via an optional fourth constructor parameter.

The generator will log the following information:

Expand Down
3 changes: 0 additions & 3 deletions src/CodeGenerator/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Zend\Di\CodeGenerator;

use Psr\Log\LoggerAwareTrait;
use Zend\Di\Exception\GenerateCodeException;
use Zend\Di\Exception\LogicException;

Expand All @@ -16,8 +15,6 @@
*/
trait GeneratorTrait
{
use LoggerAwareTrait;

/**
* @var int
*/
Expand Down
17 changes: 13 additions & 4 deletions src/CodeGenerator/InjectorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Zend\Di\CodeGenerator;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Throwable;
use Zend\Code\Generator\ClassGenerator;
Expand All @@ -25,7 +26,7 @@
* type, if available. This factory will contained pre-resolved dependencies
* from the provided configuration, definition and resolver instances.
*/
class InjectorGenerator implements LoggerAwareInterface
class InjectorGenerator
{
use GeneratorTrait;

Expand Down Expand Up @@ -60,25 +61,33 @@ class InjectorGenerator implements LoggerAwareInterface
*/
private $autoloadGenerator;

/**
* @var LoggerInterface
*/
private $logger;

/**
* Constructs the compiler instance
*
* @param ConfigInterface $config The configuration to compile from
* @param DependencyResolverInterface $resolver The resolver to utilize
* @param string $namespace Namespace to use for generated class; defaults
* @param string|null $namespace Namespace to use for generated class; defaults
* to Zend\Di\Generated.
* @param LoggerInterface|null $logger An optional logger instance to log failures
* and processed classes.
*/
public function __construct(
ConfigInterface $config,
DependencyResolverInterface $resolver,
?string $namespace = null
string $namespace = null,
LoggerInterface $logger = null
) {
$this->config = $config;
$this->resolver = $resolver;
$this->namespace = $namespace ? : 'Zend\Di\Generated';
$this->factoryGenerator = new FactoryGenerator($config, $resolver, $this->namespace . '\Factory');
$this->autoloadGenerator = new AutoloadGenerator($this->namespace);
$this->logger = new NullLogger();
$this->logger = $logger ?? new NullLogger();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions test/CodeGenerator/InjectorGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ public function testGeneratorLogsDebugForEachClass()
{
$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
$generator = new InjectorGenerator($config, $resolver);
$logger = $this->prophesize(LoggerInterface::class);

$generator = new InjectorGenerator($config, $resolver, null, $logger->reveal());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you also change the constructor, or was it already accepting an optional logger?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the test, only. Just commited the code changes ;-)

$generator->setOutputDirectory($this->dir);
$generator->setLogger($logger->reveal());
$generator->generate([
TestAsset\B::class
]);
Expand All @@ -90,11 +89,10 @@ public function testGeneratorLogsErrorWhenFactoryGenerationFailed()
{
$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
$generator = new InjectorGenerator($config, $resolver);
$logger = $this->prophesize(LoggerInterface::class);
$generator = new InjectorGenerator($config, $resolver, null, $logger->reveal());

$generator->setOutputDirectory($this->dir);
$generator->setLogger($logger->reveal());
$generator->generate([
'Bad.And.Undefined.ClassName'
]);
Expand Down