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

Conversation

tux-rampage
Copy link
Contributor

@tux-rampage tux-rampage commented Jan 22, 2018

PR #32 introduces logging capabilities by the use of an "Aware" interface (LoggerAwareInterface of PSR-3). This is discouraged in ZF.

This pull request changes it to constructor injection and removes the introduced usage of Aware.

Since this fixes a violation that only exists in the develop branch, this PR is targeted against develop instead of master.

  • Are you fixing a bug?

    • Detail how the bug is invoked currently.
    • Detail the original, incorrect behavior.
    • Detail the new, expected behavior.
    • Add a regression test that demonstrates the bug, and proves the fix.
    • Add a CHANGELOG.md entry for the fix.
  • Is this related to quality assurance?
    As stated by @Ocramius in PR Add Logging To Code Generators (WIP) #32, the usage of aware-interfaces are discouraged throughout the Framework

.gitignore Outdated
@@ -5,3 +5,4 @@
/vendor/
/zf-mkdoc-theme.tgz
/zf-mkdoc-theme/
/.idea
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please don't mind this change, it will be reverted when I complete the PR

Copy link
Member

Choose a reason for hiding this comment

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

👍

@tux-rampage tux-rampage self-assigned this Jan 22, 2018
@tux-rampage tux-rampage added this to the 3.1.0 milestone Jan 22, 2018
.gitignore Outdated
@@ -5,3 +5,4 @@
/vendor/
/zf-mkdoc-theme.tgz
/zf-mkdoc-theme/
/.idea
Copy link
Member

Choose a reason for hiding this comment

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

👍

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

CHANGELOG.md Outdated
- [#33](https://github.com/zendframework/zend-di/pull/32) add ability to pass
`Psr\Log\LoggerInterface` to `Zend\Di\CodeGenerator\InjectorGenerator`'s constructor


- [#32](https://github.com/zendframework/zend-di/pull/32) adds the implementation of
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ocramius I'm a bit unsure about leaving this in the changelog. Should it be removed as for readability (Same goes for the Removed and Fixed section). Since the changes from PR #32 were never released.

Copy link
Member

Choose a reason for hiding this comment

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

Note is fine as long as you aggregate it with the changes in this patch. What matters is that the reader of the release notes understands what happened.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess it would be more confusing then, since it may lead people to the assumption that 3.1 introduces the Aware implementation which is actually not the case. I moved the reference to PR #32 to the Removed and Fixed entries to state clearly that this is not the case.

CHANGELOG.md Outdated
- [#33](https://github.com/zendframework/zend-di/pull/32) add ability to pass
`Psr\Log\LoggerInterface` to `Zend\Di\CodeGenerator\InjectorGenerator`'s constructor


Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted: Double space willl be removed -> single space.

@tux-rampage
Copy link
Contributor Author

Just for the records:

I chose the Aware implementation of PSR-3 since it is aliving PSR standard and it states a purely optional dependency that does not affect the classes core behaviour. But aware implementations are discouraged. Since this was already discussed - it will be removed without further arguing. (@Ocramius a ref would be awesome, if you got it at hand).

@Ocramius just two things to prevent this in the future and reference potential PR's to this:

  1. Can you please specify your disagreement with the following line any further in the following terms https://github.com/zendframework/zend-di/pull/32/files#diff-6f24fd8c91ea4350af062354fa9081d2R19
  • Is third party use of traits discouraged*
  • Because it implements aware?
  • Anything else?
  1. I assume a setter without an Aware implementation would be ok to make life easier for consumers that want to decorate an already existing instance?

@tux-rampage tux-rampage changed the title Change generator to constructor injection (WIP) Change generator to constructor injection Jan 22, 2018
Removed the added note since it might lead to confusion to the reader
and moved the ref to PR zendframework#23 to the Removed and Fixed section which
states more clearly what happened
@Ocramius
Copy link
Member

Is third party

Minor issue, given the interface stability

use of traits discouraged

Always discouraged. Only ever use traits in tests, as any downstream users may also use them and therefore we increase the BC surface by A LOT.

Because it implements aware?

This is the bigger issue - there is no need to expose that, as it is an internal implementation detail. Also, dependencies should not be mutable, and the *aware* interfaces render internal components mutable.

@Ocramius
Copy link
Member

I assume a setter without an Aware implementation would be ok

The existence of the setter is actually the bigger issue here.

@tux-rampage
Copy link
Contributor Author

tux-rampage commented Jan 22, 2018

Thanks a lot for the clearification. 👍

use of traits discouraged

Always discouraged.

The internal use only, could be stated with an "internal" annotation, but I got your point. Even though it was marked internal someone will use it and blame the framework when the trait changes. I'll open a PR removing remaining traits in this component (but this will be low prio). By now no more introduction of new traits.

The existence of the setter is actually the bigger issue here.

Even though it is not an actual dependency, more a utility aggregation (the generator's functionality itself does not actually depend on a logger)? I find this a bit too dogmatic, but I'm fine with cutting this.

edit: thank's github for mentioning internal ...
edit2: This comment is informational and topic for discussion elsewhere. With this PR the trait is removed as requested.

/**
* 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
* to Zend\Di\Generated.
* @param LoggerInterface $logger An optional logger instance to log failures
Copy link
Member

Choose a reason for hiding this comment

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

Missing null type in php doc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, on $namespace as well ...

CHANGELOG.md Outdated
- Nothing.
- [#33](https://github.com/zendframework/zend-di/pull/32) removes the implementation of
`Psr\Log\LoggerAwareInterface` from `Zend\Di\CodeGenerator\InjectorGenerator` that
was introduced with [#32](https://github.com/zendframework/zend-di/pull/32)
Copy link
Member

Choose a reason for hiding this comment

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

I think we should remove it, because #32 is not going to be released, because this PR replaces it. The same in Fixed section.
Notes in Added section are enough. But this is my option and maybe I am wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, in fact I was a bit unsure about it. I'd like to remove these entries for the same reason.
@Ocramius any objections?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @webimpress. Instead, note that #32 and #34 provide the ability to log generator processing, and give a short example of how that can be enabled.

@tux-rampage
Copy link
Contributor Author

@Ocramius since you brought this up: Can you please approve?
I'd like to merge this by tomorrow, and update the changelog according to the suggestions from @webimpress, if there are no further objections.

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@weierophinney
Copy link
Member

edit: thank's github for mentioning internal ...

Surround annotation markings with code markers to avoid that: @internal. 😄

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

Looks good; I've noted a few documentation/changelog changes.

Also: I think marking any traits developed for internal purposes with an @internal class-level annotation is reasonable. That can be done later, though.

CHANGELOG.md Outdated
- Nothing.
- [#33](https://github.com/zendframework/zend-di/pull/32) removes the implementation of
`Psr\Log\LoggerAwareInterface` from `Zend\Di\CodeGenerator\InjectorGenerator` that
was introduced with [#32](https://github.com/zendframework/zend-di/pull/32)
Copy link
Member

Choose a reason for hiding this comment

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

I agree with @webimpress. Instead, note that #32 and #34 provide the ability to log generator processing, and give a short example of how that can be enabled.

CHANGELOG.md Outdated

### Fixed

- Nothing.
- [#33](https://github.com/zendframework/zend-di/pull/32) fixes discouraged use of
`Psr\Log\LoggerAware*` that was introduced with [#32](https://github.com/zendframework/zend-di/pull/32)
Copy link
Member

Choose a reason for hiding this comment

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

No need to add this here, as it was never previously released.

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 to pass a [PSR-3 logger](http://www.php-fig.org/psr/psr-3/) as optional
fourth construction parameter.
Copy link
Member

Choose a reason for hiding this comment

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

Rephrase:

The InjectorGenerator allows passing a PSR-3 logger instance via an optional, fourth constuctor parameter.

*/
public function __construct(
ConfigInterface $config,
DependencyResolverInterface $resolver,
?string $namespace = null
?string $namespace = null,
Copy link
Member

Choose a reason for hiding this comment

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

Just an FYI: you don't need to use a nullable type here when providing a default null value. (Something @webimpress demonstrated to me recently.)

@tux-rampage
Copy link
Contributor Author

Instead, note that #32 and #34 provide the ability to log generator processing

Now I'm confused again. Since #34 is replacing #32, I'd just remove the refs to #32.

and give a short example of how that can be enabled.

In the changelog? I'd just place a constructor example, would that be sufficient?

Maybe it's too late and I should finish for today, and you can enlighten me tomorrow in the slack channel.

@weierophinney
Copy link
Member

weierophinney commented Jan 24, 2018

Now I'm confused again. Since #34 is replacing #32, I'd just remove the refs to #32.

Just note whatever PRs led to the logger capabilities; I may be a bit fuzzy on which ones to use. 😄

and give a short example of how that can be enabled.

In the changelog? I'd just place a constructor example, would that be sufficient?

Perfect!

@tux-rampage tux-rampage changed the title Change generator to constructor injection Inject Logger Instance via constructor Jan 25, 2018
@tux-rampage tux-rampage changed the title Inject Logger Instance via constructor Inject logger instance via constructor Jan 25, 2018
Typehints of parameters, that have a default value of `null` do not
have to be marked nullable explicitly, i.e. `?string` can be just
`string`.
@tux-rampage
Copy link
Contributor Author

@weierophinney I incorporated the resquested changes. I also changed the title of this PR to reflect what was actually changed. Can you approve, please?

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

I've noted a slight rephrase of the changelog entry, but you can do that on merge.

Looks great! 👍

- [#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

@tux-rampage tux-rampage merged commit 2f65872 into zendframework:develop Jan 26, 2018
tux-rampage added a commit that referenced this pull request Jan 26, 2018
Inject logger instance via constructor
tux-rampage added a commit that referenced this pull request Jan 26, 2018
tux-rampage added a commit that referenced this pull request Jan 26, 2018
@tux-rampage tux-rampage deleted the logging-fixes branch November 27, 2018 09:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants