Skip to content

Commit d6c5172

Browse files
committed
refactor: fix deprecation on http kernel dependency injection extension
1 parent 0ee0f75 commit d6c5172

File tree

9 files changed

+45
-32
lines changed

9 files changed

+45
-32
lines changed

DependencyInjection/Compiler/IntegrationPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2424
use Symfony\Component\DependencyInjection\ContainerBuilder;
2525

26-
class IntegrationPass implements CompilerPassInterface
26+
final class IntegrationPass implements CompilerPassInterface
2727
{
28+
#[\Override()]
2829
public function process(ContainerBuilder $container): void
2930
{
3031
if (!$container->hasDefinition('translation.loader.xliff')) {

DependencyInjection/Compiler/MountDumpersPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
use Symfony\Component\DependencyInjection\ContainerBuilder;
2727
use Symfony\Component\DependencyInjection\Reference;
2828

29-
class MountDumpersPass implements CompilerPassInterface
29+
final class MountDumpersPass implements CompilerPassInterface
3030
{
31+
#[\Override()]
3132
public function process(ContainerBuilder $container): void
3233
{
3334
if (!$container->hasDefinition('jms_translation.file_writer')) {

DependencyInjection/Compiler/MountExtractorsPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
use Symfony\Component\DependencyInjection\ContainerBuilder;
2626
use Symfony\Component\DependencyInjection\Reference;
2727

28-
class MountExtractorsPass implements CompilerPassInterface
28+
final class MountExtractorsPass implements CompilerPassInterface
2929
{
30+
#[\Override()]
3031
public function process(ContainerBuilder $container): void
3132
{
3233
if (!$container->hasDefinition('jms_translation.extractor_manager')) {

DependencyInjection/Compiler/MountFileVisitorsPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
use Symfony\Component\DependencyInjection\ContainerBuilder;
2525
use Symfony\Component\DependencyInjection\Reference;
2626

27-
class MountFileVisitorsPass implements CompilerPassInterface
27+
final class MountFileVisitorsPass implements CompilerPassInterface
2828
{
29+
#[\Override()]
2930
public function process(ContainerBuilder $container): void
3031
{
3132
if (!$container->hasDefinition('jms_translation.extractor.file_extractor')) {

DependencyInjection/Compiler/MountLoadersPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
use Symfony\Component\DependencyInjection\ContainerBuilder;
2727
use Symfony\Component\DependencyInjection\Reference;
2828

29-
class MountLoadersPass implements CompilerPassInterface
29+
final class MountLoadersPass implements CompilerPassInterface
3030
{
31+
#[\Override()]
3132
public function process(ContainerBuilder $container): void
3233
{
3334
if (!$container->hasDefinition('jms_translation.loader_manager')) {

DependencyInjection/Configuration.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,21 @@
2222

2323
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
2424
use Symfony\Component\Config\Definition\ConfigurationInterface;
25-
use Symfony\Component\DependencyInjection\ContainerBuilder;
25+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
2626

27-
class Configuration implements ConfigurationInterface
27+
final class Configuration implements ConfigurationInterface
2828
{
29-
private ContainerBuilder $container;
30-
31-
public function __construct(ContainerBuilder $container)
32-
{
33-
$this->container = $container;
29+
public function __construct(
30+
/** @var array<string, class-string<BundleInterface>> */
31+
private array $bundles,
32+
) {
3433
}
3534

35+
#[\Override()]
3636
public function getConfigTreeBuilder(): TreeBuilder
3737
{
38-
$c = $this->container;
39-
40-
$tb = new TreeBuilder('jms_translation');
41-
// Keep compatibility with symfony/config < 4.2
42-
if (!method_exists($tb, 'getRootNode')) {
43-
$rootNode = $tb->root('jms_translation');
44-
} else {
45-
$rootNode = $tb->getRootNode();
46-
}
38+
$treeBuilder = new TreeBuilder('jms_translation');
39+
$rootNode = $treeBuilder->getRootNode();
4740

4841
$rootNode
4942
->fixXmlConfig('config')
@@ -77,7 +70,7 @@ public function getConfigTreeBuilder(): TreeBuilder
7770
->requiresAtLeastOneElement()
7871
->prototype('scalar')
7972
->validate()
80-
->always(static function ($v) use ($c) {
73+
->always(function ($v): string {
8174
$v = str_replace(DIRECTORY_SEPARATOR, '/', $v);
8275

8376
if ('@' === $v[0]) {
@@ -87,12 +80,11 @@ public function getConfigTreeBuilder(): TreeBuilder
8780
$bundleName = substr($v, 1, $pos - 1);
8881
}
8982

90-
$bundles = $c->getParameter('kernel.bundles');
91-
if (!isset($bundles[$bundleName])) {
92-
throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, implode(', ', array_keys($bundles))));
83+
if (null === $bundleClass = ($this->bundles[$bundleName] ?? null)) {
84+
throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, implode(', ', array_keys($this->bundles))));
9385
}
9486

95-
$ref = new \ReflectionClass($bundles[$bundleName]);
87+
$ref = new \ReflectionClass($bundleClass);
9688
$v = false === $pos ? dirname($ref->getFileName()) : dirname($ref->getFileName()) . substr($v, $pos);
9789
}
9890

@@ -130,6 +122,6 @@ public function getConfigTreeBuilder(): TreeBuilder
130122
->end()
131123
->end();
132124

133-
return $tb;
125+
return $treeBuilder;
134126
}
135127
}

DependencyInjection/JMSTranslationExtension.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@
2424
use Symfony\Component\Config\FileLocator;
2525
use Symfony\Component\DependencyInjection\ContainerBuilder;
2626
use Symfony\Component\DependencyInjection\Definition;
27+
use Symfony\Component\DependencyInjection\Extension\Extension;
2728
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
28-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2929

30-
class JMSTranslationExtension extends Extension
30+
final class JMSTranslationExtension extends Extension
3131
{
32+
#[\Override()]
33+
public function getConfiguration(array $config, ContainerBuilder $container): Configuration
34+
{
35+
return new Configuration($container->getParameter('kernel.bundles'));
36+
}
37+
38+
#[\Override()]
3239
public function load(array $configs, ContainerBuilder $container): void
3340
{
34-
$config = $this->processConfiguration(new Configuration($container), $configs);
41+
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
3542

3643
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
3744
$loader->load('services.xml');

JMSTranslationBundle.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
use Symfony\Component\DependencyInjection\ContainerBuilder;
2727
use Symfony\Component\HttpKernel\Bundle\Bundle;
2828

29-
class JMSTranslationBundle extends Bundle
29+
final class JMSTranslationBundle extends Bundle
3030
{
31-
const VERSION = '1.1.0-DEV';
31+
public const VERSION = '3.0.0-DEV';
3232

33+
#[\Override()]
3334
public function build(ContainerBuilder $container): void
3435
{
3536
$container->addCompilerPass(new IntegrationPass());

Tests/DependencyInjection/JMSTranslationExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace JMS\TranslationBundle\Tests\DependencyInjection;
66

77
use JMS\TranslationBundle\DependencyInjection\JMSTranslationExtension;
8+
use JMS\TranslationBundle\JMSTranslationBundle;
89
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
910
use PHPUnit\Framework\Attributes\Test;
1011

@@ -13,6 +14,13 @@
1314
*/
1415
class JMSTranslationExtensionTest extends AbstractExtensionTestCase
1516
{
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->container->setParameter('kernel.bundles', ['JMSTranslationBundle' => JMSTranslationBundle::class]);
22+
}
23+
1624
protected function getContainerExtensions(): array
1725
{
1826
return [

0 commit comments

Comments
 (0)