Skip to content

Commit 88e3cac

Browse files
authored
Fix services configuration, remove code for old SF kernels. (#492)
* Fix services configuration, remove code for old SF kernels. * Add symfony/var-exporter in require-dev to fix lazy objects error in tests * Fix conflicts
1 parent 48811d4 commit 88e3cac

File tree

6 files changed

+40
-105
lines changed

6 files changed

+40
-105
lines changed

Command/ImportTranslationsCommand.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107107
$bundleName = $this->input->getArgument('bundle');
108108
if ($bundleName) {
109109
$bundle = $this->getApplication()->getKernel()->getBundle($bundleName);
110-
111-
if (Kernel::VERSION_ID < 40000 && null !== $bundle->getParent()) {
112-
// due to symfony's bundle inheritance if a bundle has a parent it is fetched first.
113-
// so we tell getBundle to NOT fetch the first if a parent is present
114-
$bundles = $this->getApplication()->getKernel()->getBundle($bundle->getParent(), false);
115-
$bundle = $bundles[1];
116-
$this->output->writeln('<info>Using: ' . $bundle->getName() . ' as bundle to lookup translations files for.');
117-
}
118-
119110
$this->importBundleTranslationFiles($bundle, $locales, $domains, (bool)$this->input->getOption('globals'));
120-
121111
} else {
122112
if (!$this->input->getOption('import-path')) {
123113

DependencyInjection/Compiler/TranslatorPass.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1010
use Symfony\Component\DependencyInjection\Reference;
11-
use Symfony\Component\HttpKernel\Kernel;
1211

1312
/**
1413
* Translator compiler pass to automatically pass loader to the other services.
@@ -50,24 +49,16 @@ public function process(ContainerBuilder $container): void
5049

5150
if ($translatorId && $container->hasDefinition($translatorId)) {
5251
$translatorDef = $container->findDefinition($translatorId);
53-
54-
if (Kernel::VERSION_ID >= 30300) {
55-
$serviceRefs = [...$loadersReferencesById, ...['event_dispatcher' => new Reference('event_dispatcher')]];
5652

57-
// Use named arguments if available, otherwise use numeric indices
58-
if ($translatorDef->getArguments() && array_key_exists('$container', $translatorDef->getArguments())) {
59-
$translatorDef->replaceArgument('$container', ServiceLocatorTagPass::register($container, $serviceRefs));
60-
$translatorDef->replaceArgument('$loaderIds', $loaders);
61-
} else {
62-
$translatorDef->replaceArgument(0, ServiceLocatorTagPass::register($container, $serviceRefs));
63-
$translatorDef->replaceArgument(3, $loaders);
64-
}
53+
$serviceRefs = [...$loadersReferencesById, ...['event_dispatcher' => new Reference('event_dispatcher')]];
54+
55+
// Use named arguments if available, otherwise use numeric indices
56+
if ($translatorDef->getArguments() && array_key_exists('$container', $translatorDef->getArguments())) {
57+
$translatorDef->replaceArgument('$container', ServiceLocatorTagPass::register($container, $serviceRefs));
58+
$translatorDef->replaceArgument('$loaderIds', $loaders);
6559
} else {
66-
if ($translatorDef->getArguments() && array_key_exists('$loaderIds', $translatorDef->getArguments())) {
67-
$translatorDef->replaceArgument('$loaderIds', $loaders);
68-
} else {
69-
$translatorDef->replaceArgument(2, $loaders);
70-
}
60+
$translatorDef->replaceArgument(0, ServiceLocatorTagPass::register($container, $serviceRefs));
61+
$translatorDef->replaceArgument(3, $loaders);
7162
}
7263
}
7364

DependencyInjection/LexikTranslationExtension.php

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\Component\DependencyInjection\Parameter;
2121
use Symfony\Component\DependencyInjection\Definition;
2222
use Symfony\Component\DependencyInjection\ContainerBuilder;
23-
use Symfony\Component\DependencyInjection\Loader;
2423
use Symfony\Component\Finder\Finder;
2524
use Symfony\Component\HttpKernel\Kernel;
2625

@@ -63,9 +62,6 @@ public function load(array $configs, ContainerBuilder $container): void
6362

6463
$objectManager = $config['storage']['object_manager'] ?? null;
6564

66-
// Translator is now handled via service decoration in services.yaml
67-
// buildTranslatorDefinition() is deprecated and no longer needed for Symfony 8
68-
// $this->buildTranslatorDefinition($container);
6965
$this->buildTranslationStorageDefinition($container, $config['storage']['type'], $objectManager);
7066

7167
if (true === $config['auto_cache_clean']) {
@@ -79,44 +75,6 @@ public function load(array $configs, ContainerBuilder $container): void
7975
$this->registerTranslatorConfiguration($config, $container);
8076
}
8177

82-
public function buildTranslatorDefinition(ContainerBuilder $container)
83-
{
84-
$translator = new Definition();
85-
$translator->setClass('%lexik_translation.translator.class%');
86-
87-
if (Kernel::VERSION_ID >= 30400) {
88-
$arguments = [
89-
new Reference('service_container'), // Will be replaced by service locator
90-
new Reference('translator.formatter.default'),
91-
new Parameter('kernel.default_locale'),
92-
[], // translation loaders
93-
new Parameter('lexik_translation.translator.options')
94-
];
95-
$translator->setPublic(true);
96-
} elseif (Kernel::VERSION_ID >= 30300) {
97-
$arguments = [
98-
new Reference('service_container'), // Will be replaced by service locator
99-
new Reference('translator.selector'),
100-
new Parameter('kernel.default_locale'),
101-
[], // translation loaders
102-
new Parameter('lexik_translation.translator.options')
103-
];
104-
} else {
105-
$arguments = [
106-
new Reference('service_container'),
107-
new Reference('translator.selector'),
108-
[], // translation loaders
109-
new Parameter('lexik_translation.translator.options')
110-
];
111-
}
112-
113-
$translator->setArguments($arguments);
114-
$translator->addMethodCall('setConfigCacheFactory', [new Reference('config_cache_factory')]);
115-
$translator->addTag('kernel.locale_aware');
116-
117-
$container->setDefinition('lexik_translation.translator', $translator);
118-
}
119-
12078
/**
12179
* @param int $cacheInterval
12280
*/
@@ -167,7 +125,7 @@ protected function buildTranslationStorageDefinition(ContainerBuilder $container
167125

168126
// Create XML driver for backward compatibility
169127
$this->createDoctrineMappingDriver($container, 'lexik_translation.orm.metadata.xml', '%doctrine.orm.metadata.xml.class%');
170-
128+
171129
// Create attribute driver for models (MappedSuperclass) that now use PHP attributes
172130
$this->createDoctrineAttributeDriver($container, 'lexik_translation.orm.metadata.attribute');
173131

@@ -208,7 +166,7 @@ protected function buildTranslationStorageDefinition(ContainerBuilder $container
208166
protected function createDoctrineMappingDriver(ContainerBuilder $container, $driverId, $driverClass)
209167
{
210168
$driverDefinition = new Definition($driverClass, [
211-
[realpath(__DIR__.'/../Resources/config/model') => 'Lexik\Bundle\TranslationBundle\Model'],
169+
[dirname(__DIR__) . '/Resources/config/model' => 'Lexik\Bundle\TranslationBundle\Model'],
212170
SimplifiedXmlDriver::DEFAULT_FILE_EXTENSION, true
213171
]);
214172
$driverDefinition->setPublic(false);
@@ -229,20 +187,20 @@ protected function createDoctrineAttributeDriver(ContainerBuilder $container, $d
229187
$bundleReflection = new \ReflectionClass(\Lexik\Bundle\TranslationBundle\LexikTranslationBundle::class);
230188
$bundleDir = dirname($bundleReflection->getFileName());
231189
$modelPath = $bundleDir . '/Model';
232-
190+
233191
// Try to get realpath, but use the calculated path if it fails
234192
$realModelPath = realpath($modelPath);
235193
if ($realModelPath) {
236194
$modelPath = $realModelPath;
237195
}
238-
196+
239197
// AttributeDriver constructor expects an array of paths (directories to scan)
240198
// It will automatically detect classes with #[ORM\MappedSuperclass] or #[ORM\Entity] attributes
241199
$driverDefinition = new Definition(AttributeDriver::class, [
242200
[$modelPath]
243201
]);
244202
$driverDefinition->setPublic(false);
245-
203+
246204
// Always set/override the definition to ensure it exists with correct arguments
247205
$container->setDefinition($driverId, $driverDefinition);
248206
}
@@ -270,19 +228,16 @@ protected function registerTranslatorConfiguration(array $config, ContainerBuild
270228
{
271229
// use the Lexik translator decorator as default translator service
272230
$alias = $container->setAlias('translator', 'lexik_translation.translator');
273-
274-
if (Kernel::VERSION_ID >= 30400) {
275-
$alias->setPublic(true);
276-
}
231+
$alias->setPublic(true);
277232

278233
// Get the inner translator (the actual Symfony translator) for adding resources
279234
// The decorator will delegate to it
280235
$innerTranslator = $container->hasDefinition('lexik_translation.translator.inner')
281236
? $container->findDefinition('lexik_translation.translator.inner')
282237
: $container->findDefinition('translator');
283-
238+
284239
$innerTranslator->addMethodCall('setFallbackLocales', [$config['fallback_locale']]);
285-
240+
286241
// For adding file resources, we'll add them to the inner translator
287242
$translator = $innerTranslator;
288243

Resources/config/services.xml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
<parameter key="lexik_translation.translator.class">Lexik\Bundle\TranslationBundle\Translation\Translator</parameter>
98
<parameter key="lexik_translation.loader.database.class">Lexik\Bundle\TranslationBundle\Translation\Loader\DatabaseLoader</parameter>
109
<parameter key="lexik_translation.trans_unit.manager.class">Lexik\Bundle\TranslationBundle\Manager\TransUnitManager</parameter>
1110
<parameter key="lexik_translation.file.manager.class">Lexik\Bundle\TranslationBundle\Manager\FileManager</parameter>
@@ -48,29 +47,28 @@
4847

4948
<parameter key="lexik_translation.importer.case_insensitive">false</parameter>
5049
<parameter key="lexik_translation.token_finder.limit">15</parameter>
51-
52-
<parameter key="lexik_translation.translator.options" type="collection">
53-
<parameter key="cache_dir">%kernel.cache_dir%/translations</parameter>
54-
<parameter key="debug">%kernel.debug%</parameter>
55-
<parameter key="resources_type">%lexik_translation.resources_type%</parameter>
56-
</parameter>
5750
</parameters>
5851

5952
<services>
60-
<service id="Lexik\Bundle\TranslationBundle\Translation\Translator">
61-
<argument key="$container" type="service" id="service_container" />
62-
<argument key="$formatter" type="service" id="translator.formatter.default" />
63-
<argument key="$defaultLocale" id="%kernel.default_locale%" />
64-
<argument key="$loaderIds" type="collection"></argument>
65-
<argument key="$options" type="collection" >
66-
<argument key="cache_dir">%kernel.cache_dir%</argument>
53+
<service id="lexik_translation.translator" class="Lexik\Bundle\TranslationBundle\Translation\Translator" public="true">
54+
<argument type="service" id="service_container" />
55+
<argument type="service" id="translator.formatter.default" />
56+
<argument id="%kernel.default_locale%" />
57+
<argument type="collection" />
58+
<argument type="collection" >
59+
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
6760
<argument key="debug">%kernel.debug%</argument>
6861
<argument key="resource_files" type="collection" />
6962
<argument key="scanned_directories" type="collection" />
7063
<argument key="cache_vary" type="collection" />
64+
<argument key="resources_type">%lexik_translation.resources_type%</argument>
7165
</argument>
66+
<call method="setConfigCacheFactory">
67+
<argument type="service" id="config_cache_factory" />
68+
</call>
69+
<tag name="kernel.locale_aware" />
7270
</service>
73-
<service id="lexik_translation.translator" alias="Lexik\Bundle\TranslationBundle\Translation\Translator" public="true"/>
71+
<service id="Lexik\Bundle\TranslationBundle\Translation\Translator" alias="lexik_translation.translator" public="true"/>
7472
<service id="Lexik\Bundle\TranslationBundle\Storage\StorageInterface" alias="lexik_translation.translation_storage" />
7573

7674
<!-- Loader -->

Resources/config/services.yaml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,34 @@ parameters:
4343
lexik_translation.importer.case_insensitive: false
4444
lexik_translation.token_finder.limit: 15
4545

46-
lexik_translation.translator.options:
47-
cache_dir: '%kernel.cache_dir%/translations'
48-
debug: '%kernel.debug%'
49-
resources_type: '%lexik_translation.resources_type%'
50-
5146
services:
5247
_defaults:
5348
autowire: true
5449
autoconfigure: true
5550

5651
# Translator
57-
Lexik\Bundle\TranslationBundle\Translation\Translator:
52+
lexik_translation.translator:
53+
class: Lexik\Bundle\TranslationBundle\Translation\Translator
5854
arguments:
5955
$container: '@service_container'
6056
$formatter: '@translator.formatter.default'
6157
$defaultLocale: '%kernel.default_locale%'
6258
$loaderIds: []
6359
$options:
64-
cache_dir: '%kernel.cache_dir%'
60+
cache_dir: '%kernel.cache_dir%/translations'
6561
debug: '%kernel.debug%'
6662
resource_files: []
6763
scanned_directories: []
6864
cache_vary: []
65+
resources_type: '%lexik_translation.resources_type%'
66+
calls:
67+
- [setConfigCacheFactory, ['@config_cache_factory']]
68+
tags:
69+
- { name: kernel.locale_aware }
70+
public: true
6971

70-
lexik_translation.translator:
71-
alias: Lexik\Bundle\TranslationBundle\Translation\Translator
72+
Lexik\Bundle\TranslationBundle\Translation\Translator:
73+
alias: lexik_translation.translator
7274
public: true
7375

7476
# TranslatorDecorator alias for backward compatibility

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,3 @@
7373
"sort-packages": true
7474
}
7575
}
76-

0 commit comments

Comments
 (0)