Skip to content

Commit d320c9e

Browse files
authored
Merge pull request #466 from derrabus/drop/symfony-4
Drop Support for Symfony 4
2 parents 67ed61d + d425367 commit d320c9e

File tree

9 files changed

+60
-213
lines changed

9 files changed

+60
-213
lines changed

.github/workflows/ci.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
php: [ '7.2', '7.3', '7.4', '8.0', '8.1' ]
1515
monolog: [ '1.*', '2.*' ]
1616
include:
17-
- php: '7.1'
1817
- php: '7.4'
1918
deps: lowest
2019
deprecations: max[self]=0

DependencyInjection/Compiler/LoggerChannelPass.php

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,17 @@ public function process(ContainerBuilder $container)
6666
}
6767
$definition->setMethodCalls($calls);
6868

69-
if (\method_exists($definition, 'getBindings')) {
70-
$binding = new BoundArgument(new Reference($loggerId));
71-
72-
// Mark the binding as used already, to avoid reporting it as unused if the service does not use a
73-
// logger injected through the LoggerInterface alias.
74-
$values = $binding->getValues();
75-
$values[2] = true;
76-
$binding->setValues($values);
77-
78-
$bindings = $definition->getBindings();
79-
$bindings['Psr\Log\LoggerInterface'] = $binding;
80-
$definition->setBindings($bindings);
81-
}
69+
$binding = new BoundArgument(new Reference($loggerId));
70+
71+
// Mark the binding as used already, to avoid reporting it as unused if the service does not use a
72+
// logger injected through the LoggerInterface alias.
73+
$values = $binding->getValues();
74+
$values[2] = true;
75+
$binding->setValues($values);
76+
77+
$bindings = $definition->getBindings();
78+
$bindings['Psr\Log\LoggerInterface'] = $binding;
79+
$definition->setBindings($bindings);
8280
}
8381
}
8482

@@ -117,11 +115,9 @@ public function getChannels()
117115
}
118116

119117
/**
120-
* @param array $configuration
121-
*
122118
* @return array
123119
*/
124-
protected function processChannels($configuration)
120+
protected function processChannels(?array $configuration)
125121
{
126122
if (null === $configuration) {
127123
return $this->channels;
@@ -137,11 +133,9 @@ protected function processChannels($configuration)
137133
/**
138134
* Create new logger from the monolog.logger_prototype
139135
*
140-
* @param string $channel
141-
* @param string $loggerId
142-
* @param ContainerBuilder $container
136+
* @return void
143137
*/
144-
protected function createLogger($channel, $loggerId, ContainerBuilder $container)
138+
protected function createLogger(string $channel, string $loggerId, ContainerBuilder $container)
145139
{
146140
if (!in_array($channel, $this->channels)) {
147141
$logger = new ChildDefinition('monolog.logger_prototype');
@@ -150,29 +144,16 @@ protected function createLogger($channel, $loggerId, ContainerBuilder $container
150144
$this->channels[] = $channel;
151145
}
152146

153-
// Allows only for Symfony 4.2+
154-
if (\method_exists($container, 'registerAliasForArgument')) {
155-
$parameterName = $channel . 'Logger';
147+
$parameterName = $channel . 'Logger';
156148

157-
$container->registerAliasForArgument($loggerId, LoggerInterface::class, $parameterName);
158-
}
149+
$container->registerAliasForArgument($loggerId, LoggerInterface::class, $parameterName);
159150
}
160151

161152
/**
162153
* Creates a copy of a reference and alters the service ID.
163-
*
164-
* @param Reference $reference
165-
* @param string $serviceId
166-
*
167-
* @return Reference
168154
*/
169-
private function changeReference(Reference $reference, $serviceId)
155+
private function changeReference(Reference $reference, string $serviceId): Reference
170156
{
171-
if (method_exists($reference, 'isStrict')) {
172-
// Stay compatible with Symfony 2
173-
return new Reference($serviceId, $reference->getInvalidBehavior(), $reference->isStrict(false));
174-
}
175-
176157
return new Reference($serviceId, $reference->getInvalidBehavior());
177158
}
178159
}

DependencyInjection/Configuration.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class Configuration implements ConfigurationInterface
379379
public function getConfigTreeBuilder(): TreeBuilder
380380
{
381381
$treeBuilder = new TreeBuilder('monolog');
382-
$rootNode = method_exists(TreeBuilder::class, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('monolog');
382+
$rootNode = $treeBuilder->getRootNode();
383383

384384
$handlers = $rootNode
385385
->fixXmlConfig('channel')
@@ -602,7 +602,7 @@ public function getConfigTreeBuilder(): TreeBuilder
602602
->end()
603603
// console
604604
->variableNode('console_formater_options')
605-
->setDeprecated(...$this->getDeprecationMsg('"%path%.%node%" is deprecated, use "%path%.console_formatter_options" instead.', 3.7))
605+
->setDeprecated('symfony/monolog-bundle', 3.7, '"%path%.%node%" is deprecated, use "%path%.console_formatter_options" instead.')
606606
->validate()
607607
->ifTrue(function ($v) {
608608
return !is_array($v);
@@ -1135,27 +1135,4 @@ private function addChannelsSection(ArrayNodeDefinition $handerNode)
11351135
->end()
11361136
;
11371137
}
1138-
1139-
/**
1140-
* Returns the correct deprecation param's as an array for setDeprecated.
1141-
*
1142-
* Symfony/Config v5.1 introduces a deprecation notice when calling
1143-
* setDeprecation() with less than 3 args and the getDeprecation method was
1144-
* introduced at the same time. By checking if getDeprecation() exists,
1145-
* we can determine the correct param count to use when calling setDeprecated.
1146-
*
1147-
* @return array{0:string}|array{0:string, 1: numeric-string, string}
1148-
*/
1149-
private function getDeprecationMsg(string $message, string $version): array
1150-
{
1151-
if (method_exists(BaseNode::class, 'getDeprecation')) {
1152-
return [
1153-
'symfony/monolog-bundle',
1154-
$version,
1155-
$message,
1156-
];
1157-
}
1158-
1159-
return [$message];
1160-
}
11611138
}

DependencyInjection/MonologExtension.php

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3333
use Symfony\Component\DependencyInjection\Reference;
3434
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
35-
use Symfony\Component\HttpKernel\Kernel;
3635
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
3736
use Symfony\Contracts\HttpClient\HttpClientInterface;
3837

@@ -56,10 +55,6 @@ class MonologExtension extends Extension
5655
*/
5756
public function load(array $configs, ContainerBuilder $container)
5857
{
59-
if (class_exists(FullStack::class) && Kernel::MAJOR_VERSION < 5 && Logger::API >= 2) {
60-
throw new \RuntimeException('Symfony 5 is required for Monolog 2 support. Please downgrade Monolog to version 1.');
61-
}
62-
6358
$configuration = $this->getConfiguration($configs, $container);
6459
$config = $this->processConfiguration($configuration, $configs);
6560

@@ -107,29 +102,27 @@ public function load(array $configs, ContainerBuilder $container)
107102

108103
$container->setParameter('monolog.additional_channels', isset($config['channels']) ? $config['channels'] : []);
109104

110-
if (method_exists($container, 'registerForAutoconfiguration')) {
111-
if (interface_exists(ProcessorInterface::class)) {
112-
$container->registerForAutoconfiguration(ProcessorInterface::class)
113-
->addTag('monolog.processor');
114-
} else {
115-
$container->registerForAutoconfiguration(WebProcessor::class)
116-
->addTag('monolog.processor');
117-
}
118-
if (interface_exists(ResettableInterface::class)) {
119-
$container->registerForAutoconfiguration(ResettableInterface::class)
120-
->addTag('kernel.reset', ['method' => 'reset']);
121-
}
122-
$container->registerForAutoconfiguration(TokenProcessor::class)
105+
if (interface_exists(ProcessorInterface::class)) {
106+
$container->registerForAutoconfiguration(ProcessorInterface::class)
123107
->addTag('monolog.processor');
124-
if (interface_exists(HttpClientInterface::class)) {
125-
$handlerAutoconfiguration = $container->registerForAutoconfiguration(HandlerInterface::class);
126-
$handlerAutoconfiguration->setBindings($handlerAutoconfiguration->getBindings() + [
127-
HttpClientInterface::class => new BoundArgument(new Reference('monolog.http_client'), false),
128-
]);
129-
}
108+
} else {
109+
$container->registerForAutoconfiguration(WebProcessor::class)
110+
->addTag('monolog.processor');
111+
}
112+
if (interface_exists(ResettableInterface::class)) {
113+
$container->registerForAutoconfiguration(ResettableInterface::class)
114+
->addTag('kernel.reset', ['method' => 'reset']);
115+
}
116+
$container->registerForAutoconfiguration(TokenProcessor::class)
117+
->addTag('monolog.processor');
118+
if (interface_exists(HttpClientInterface::class)) {
119+
$handlerAutoconfiguration = $container->registerForAutoconfiguration(HandlerInterface::class);
120+
$handlerAutoconfiguration->setBindings($handlerAutoconfiguration->getBindings() + [
121+
HttpClientInterface::class => new BoundArgument(new Reference('monolog.http_client'), false),
122+
]);
130123
}
131124

132-
if (80000 <= \PHP_VERSION_ID && method_exists($container, 'registerAttributeForAutoconfiguration')) {
125+
if (80000 <= \PHP_VERSION_ID) {
133126
$container->registerAttributeForAutoconfiguration(AsMonologProcessor::class, static function (ChildDefinition $definition, AsMonologProcessor $attribute, \Reflector $reflector): void {
134127
$tagAttributes = get_object_vars($attribute);
135128
if ($reflector instanceof \ReflectionMethod) {
@@ -431,9 +424,6 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
431424
$container->setDefinition($handlerId.'.not_found_strategy', $activationDef);
432425
$activation = new Reference($handlerId.'.not_found_strategy');
433426
} elseif (!empty($handler['excluded_http_codes'])) {
434-
if (!class_exists('Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy')) {
435-
throw new \LogicException('"excluded_http_codes" cannot be used as your version of Monolog bridge does not support it.');
436-
}
437427
$activationDef = new Definition('Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy', [
438428
new Reference('request_stack'),
439429
$handler['excluded_http_codes'],
@@ -888,10 +878,6 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
888878
]);
889879
break;
890880
case 'server_log':
891-
if (!class_exists('Symfony\Bridge\Monolog\Handler\ServerLogHandler')) {
892-
throw new \RuntimeException('The ServerLogHandler is not available. Please update "symfony/monolog-bridge" to 3.3.');
893-
}
894-
895881
$definition->setArguments([
896882
$handler['host'],
897883
$handler['level'],

MonologBundle.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,22 @@
2727
*/
2828
class MonologBundle extends Bundle
2929
{
30+
/**
31+
* @return void
32+
*/
3033
public function build(ContainerBuilder $container)
3134
{
3235
parent::build($container);
3336

3437
$container->addCompilerPass($channelPass = new LoggerChannelPass());
35-
if (!class_exists('Symfony\Bridge\Monolog\Processor\DebugProcessor') || !class_exists('Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass')) {
36-
$container->addCompilerPass(new DebugHandlerPass($channelPass));
37-
}
3838
$container->addCompilerPass(new FixEmptyLoggerPass($channelPass));
3939
$container->addCompilerPass(new AddProcessorsPass());
4040
$container->addCompilerPass(new AddSwiftMailerTransportPass());
4141
}
4242

4343
/**
4444
* @internal
45+
* @return void
4546
*/
4647
public static function includeStacktraces(HandlerInterface $handler)
4748
{

Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ public function testProcess()
5656

5757
public function testTypeHintedAliasesExistForEachChannel()
5858
{
59-
if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) {
60-
$this->markTestSkipped('Need DependencyInjection 4.2+ to register type-hinted aliases for channels.');
61-
}
62-
6359
$container = $this->getContainer();
6460
$expectedChannels = ['test', 'foo', 'bar', 'additional'];
6561

@@ -81,10 +77,6 @@ public function testProcessSetters()
8177

8278
public function testAutowiredLoggerArgumentsAreReplacedWithChannelLogger()
8379
{
84-
if (!\method_exists('Symfony\Component\DependencyInjection\Definition', 'getBindings')) {
85-
$this->markTestSkipped('Need DependencyInjection 3.4+ to autowire channel logger.');
86-
}
87-
8880
$container = $this->getFunctionalContainer();
8981

9082
$dummyService = $container->register('dummy_service', 'Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService')
@@ -99,10 +91,6 @@ public function testAutowiredLoggerArgumentsAreReplacedWithChannelLogger()
9991

10092
public function testAutowiredLoggerArgumentsAreReplacedWithChannelLoggerWhenAutoconfigured()
10193
{
102-
if (!\method_exists('Symfony\Component\DependencyInjection\Definition', 'getBindings')) {
103-
$this->markTestSkipped('Need DependencyInjection 3.4+ to autowire channel logger.');
104-
}
105-
10694
$container = $this->getFunctionalContainer();
10795

10896
$container->registerForAutoconfiguration('Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService')
@@ -121,10 +109,6 @@ public function testAutowiredLoggerArgumentsAreReplacedWithChannelLoggerWhenAuto
121109

122110
public function testAutowiredLoggerArgumentsAreNotReplacedWithChannelLoggerIfLoggerArgumentIsConfiguredExplicitly()
123111
{
124-
if (!\method_exists('Symfony\Component\DependencyInjection\Definition', 'getBindings')) {
125-
$this->markTestSkipped('Need DependencyInjection 3.4+ to autowire channel logger.');
126-
}
127-
128112
$container = $this->getFunctionalContainer();
129113

130114
$dummyService = $container->register('dummy_service', 'Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService')

Tests/DependencyInjection/FixtureMonologExtensionTest.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,9 @@
2222

2323
abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
2424
{
25-
/** @group legacy */
26-
public function testLegacyLoadWithSeveralHandlers()
27-
{
28-
if (class_exists(SwitchUserTokenProcessor::class)) {
29-
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
30-
}
31-
32-
$this->doTestLoadWithSeveralHandlers('ERROR');
33-
}
34-
3525
public function testLoadWithSeveralHandlers()
3626
{
37-
if (!class_exists(SwitchUserTokenProcessor::class)) {
38-
$this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
39-
}
40-
41-
$this->doTestLoadWithSeveralHandlers(new Definition(ErrorLevelActivationStrategy::class, ['ERROR']));
42-
}
43-
44-
private function doTestLoadWithSeveralHandlers($activation)
45-
{
27+
$activation = new Definition(ErrorLevelActivationStrategy::class, ['ERROR']);
4628
$container = $this->getContainer('multiple_handlers');
4729

4830
$this->assertTrue($container->hasDefinition('monolog.logger'));
@@ -70,27 +52,9 @@ private function doTestLoadWithSeveralHandlers($activation)
7052
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested2'), ['WARNING', 'ERROR'], 'EMERGENCY', true]);
7153
}
7254

73-
/** @group legacy */
74-
public function testLegacyLoadWithOverwriting()
75-
{
76-
if (class_exists(SwitchUserTokenProcessor::class)) {
77-
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
78-
}
79-
80-
$this->doTestLoadWithOverwriting('ERROR');
81-
}
82-
8355
public function testLoadWithOverwriting()
8456
{
85-
if (!class_exists(SwitchUserTokenProcessor::class)) {
86-
$this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
87-
}
88-
89-
$this->doTestLoadWithOverwriting(new Definition(ErrorLevelActivationStrategy::class, ['ERROR']));
90-
}
91-
92-
private function doTestLoadWithOverwriting($activation)
93-
{
57+
$activation = new Definition(ErrorLevelActivationStrategy::class, ['ERROR']);
9458
$container = $this->getContainer('overwriting');
9559

9660
$this->assertTrue($container->hasDefinition('monolog.logger'));
@@ -202,10 +166,6 @@ public function testSingleEmailRecipient()
202166

203167
public function testServerLog()
204168
{
205-
if (!class_exists('Symfony\Bridge\Monolog\Handler\ServerLogHandler')) {
206-
$this->markTestSkipped('The ServerLogHandler is not available.');
207-
}
208-
209169
$container = $this->getContainer('server_log');
210170

211171
$this->assertEquals([

0 commit comments

Comments
 (0)