Skip to content

Commit d6a3536

Browse files
committed
📦 Avoid deprecation by using compiler pass which is compatible with all supported symfony versions (4/5)
> Since symfony/dependency-injection 5.1: The signature of method "Symfony\Component\DependencyInjection\Alias::setDeprecated()" requires 3 arguments: "string $package, string $version, string $message", not defining them is deprecated
1 parent a80cd45 commit d6a3536

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Jaeger\Symfony\DependencyInjection;
5+
6+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Dumper\Preloader;
9+
10+
class DeprecatedAliasesCompilerPass implements CompilerPassInterface
11+
{
12+
public function process(ContainerBuilder $container): void
13+
{
14+
$container->getAlias('jaeger.span.handler.gloabal')
15+
->setDeprecated(
16+
...
17+
$this->getDeprecationMsg(
18+
'The "%alias_id%" service is deprecated. Use "jaeger.span.handler.global".',
19+
'3.5.0'
20+
)
21+
);
22+
}
23+
24+
/**
25+
* Returns the correct deprecation param's as an array for setDeprecated.
26+
*
27+
* symfony/dependency-injection v5.1 introduces a deprecation notice when calling
28+
* setDeprecation() with less than 3 args and the
29+
* `Symfony\Component\DependencyInjection\Dumper\Preloader` class was
30+
* introduced at the same time. By checking if this class exists,
31+
* we can determine the correct param count to use when calling setDeprecated.
32+
*
33+
* @param string $message
34+
* @param string $version
35+
*
36+
* @return array
37+
*/
38+
private function getDeprecationMsg(string $message, string $version): array
39+
{
40+
if (class_exists(Preloader::class)) {
41+
return [
42+
'code-tool/jaeger-client-symfony-bridge',
43+
$version,
44+
$message,
45+
];
46+
}
47+
48+
return [$message];
49+
}
50+
}

src/JaegerBundle.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Jaeger\Symfony\DependencyInjection\CodecRegistryCompilerPass;
77
use Jaeger\Symfony\DependencyInjection\ContextExtractorChainCompilerPass;
88
use Jaeger\Symfony\DependencyInjection\DebugExtractorChainCompilerPass;
9+
use Jaeger\Symfony\DependencyInjection\DeprecatedAliasesCompilerPass;
910
use Jaeger\Symfony\DependencyInjection\NameGeneratorChainCompilerPass;
1011
use Symfony\Component\DependencyInjection\ContainerBuilder;
1112
use Symfony\Component\HttpKernel\Bundle\Bundle;
@@ -19,6 +20,7 @@ public function build(ContainerBuilder $container)
1920
->addCompilerPass(new CodecRegistryCompilerPass())
2021
->addCompilerPass(new ContextExtractorChainCompilerPass())
2122
->addCompilerPass(new DebugExtractorChainCompilerPass())
22-
->addCompilerPass(new NameGeneratorChainCompilerPass());
23+
->addCompilerPass(new NameGeneratorChainCompilerPass())
24+
->addCompilerPass(new DeprecatedAliasesCompilerPass());
2325
}
2426
}

src/Resources/services.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ services:
139139
jaeger.span.handler.background:
140140
class: Jaeger\Symfony\Bridge\BackgroundSpanHandler
141141
arguments: ['@jaeger.tracer']
142-
jaeger.span.handler.gloabal:
143-
alias: jaeger.span.handler.global
144-
deprecated: 'The "%alias_id%" service is deprecated. Use "jaeger.span.handler.global".'
145142
jaeger.span.handler.global:
146143
class: Jaeger\Symfony\Bridge\GlobalSpanHandler
147144
arguments: ['@jaeger.tracer', '@jaeger.name.generator']
@@ -198,3 +195,7 @@ services:
198195
- '@jaeger.span.manager'
199196
tags:
200197
- {name: 'kernel.event_subscriber'}
198+
# deprecated since 3.5.0
199+
jaeger.span.handler.gloabal:
200+
alias: jaeger.span.handler.global
201+

0 commit comments

Comments
 (0)