-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] [DI] Automatically add "setLogger" method call. #24973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aa39944
[FrameworkBundle] [DI] Automatically add "setLogger" method call.
1eb32d3
Fix email
0d415d3
Fabbot.io fixes.
b4bc0dc
Disable for service without autoconfiguration enabled.
4d167f8
Small fixes
6a3a5bc
Use reflection class instead of class_exists
35404a9
Add tag for LoggerAwareInterface, add tagged services CP.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/LoggerAwarePass.php
This file contains hidden or 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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Psr\Log\LoggerAwareInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Automatically add setLogger method call to any service that implements Psr\Log\LoggerAwareInterface. | ||
* | ||
* @see http://www.php-fig.org/psr/psr-3/ | ||
* | ||
* @author Gary PEGEOT <[email protected]> | ||
*/ | ||
class LoggerAwarePass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('logger') && !$container->hasAlias('logger')) { | ||
return; | ||
} | ||
|
||
$reference = new Reference('logger'); | ||
|
||
foreach (\array_keys($container->findTaggedServiceIds('logger.aware')) as $id) { | ||
$definition = $container->findDefinition($id); | ||
$class = $container->getReflectionClass($definition->getClass()); | ||
|
||
if (!$definition->hasMethodCall('setLogger') && $class && $class->implementsInterface(LoggerAwareInterface::class)) { | ||
$definition->addMethodCall('setLogger', array($reference)); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
59 changes: 59 additions & 0 deletions
59
...Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/LoggerAwarePassTest.php
This file contains hidden or 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,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerAwarePass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Unit tests for Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerAwarePass. | ||
* | ||
* @author Gary PEGEOT <[email protected]> | ||
*/ | ||
class LoggerAwarePassTest extends TestCase | ||
{ | ||
/** | ||
* @covers \Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerAwarePass::process() | ||
*/ | ||
public function testProcess() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('logger', LoggerInterface::class); | ||
|
||
$definition = $container->register('foo', get_class($this->createMock(LoggerAwareInterface::class))) | ||
->addTag('logger.aware'); | ||
|
||
$container->register('bar', 'stdClass'); | ||
$container->register('not.autowired', LoggerInterface::class); | ||
$this->assertFalse( | ||
$definition->hasMethodCall('setLogger'), | ||
'Service should not have "setLogger" method call yet.' | ||
); | ||
|
||
(new LoggerAwarePass())->process($container); | ||
|
||
$this->assertTrue($definition->hasMethodCall('setLogger'), 'Service should have "setLogger" method call.'); | ||
|
||
$this->assertFalse( | ||
$container->findDefinition('bar')->hasMethodCall('setLogger'), | ||
'"bar" service should not be affected' | ||
); | ||
|
||
$this->assertFalse( | ||
$container->findDefinition('not.autowired')->hasMethodCall('setLogger'), | ||
'Not autoconfigured service should not be affected.' | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont think this is needed really.