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

Commit

Permalink
Merge 814501f into 4742f0b
Browse files Browse the repository at this point in the history
  • Loading branch information
tux-rampage committed Jan 25, 2018
2 parents 4742f0b + 814501f commit de082bc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
9 changes: 5 additions & 4 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
(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 All @@ -27,11 +28,11 @@ All notable changes to this project will be documented in this file, in reverse

### Removed

- Nothing.
- Nothing

### Fixed

- Nothing.
- Nothing

## 3.0.1 - TBD

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());
$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

0 comments on commit de082bc

Please sign in to comment.