Skip to content

Commit db9d7c2

Browse files
bartmcleodJonas De Keukelaere
andauthored
Fix for symfony 8 (#504)
* Make it work with Symfony 8 * Backward compability * Whitespace --------- Co-authored-by: Jonas De Keukelaere <jonas@sumocoders.be>
1 parent 6f5b9e7 commit db9d7c2

File tree

8 files changed

+796
-427
lines changed

8 files changed

+796
-427
lines changed

DependencyInjection/Compiler/RegisterMappingPass.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,45 +33,45 @@ public function process(ContainerBuilder $container): void
3333
// Models now use PHP attributes, so we need to use AttributeDriver
3434
// Create attribute driver if it doesn't exist (fallback if Extension didn't create it)
3535
$attributeDriverId = 'lexik_translation.orm.metadata.attribute';
36-
36+
3737
// Ensure attribute driver exists - create it if Extension didn't create it
3838
if (!$container->hasDefinition($attributeDriverId)) {
3939
// Calculate bundle path using ReflectionClass
4040
$bundleReflection = new \ReflectionClass(\Lexik\Bundle\TranslationBundle\LexikTranslationBundle::class);
4141
$bundleDir = dirname($bundleReflection->getFileName());
4242
$modelPath = $bundleDir . '/Model';
43-
43+
4444
// Try to get realpath
4545
$realModelPath = realpath($modelPath);
4646
if ($realModelPath) {
4747
$modelPath = $realModelPath;
4848
}
49-
49+
5050
// Create AttributeDriver service
5151
$driverDefinition = new Definition(AttributeDriver::class, [
5252
[$modelPath]
5353
]);
5454
$driverDefinition->setPublic(false);
5555
$container->setDefinition($attributeDriverId, $driverDefinition);
5656
}
57-
57+
5858
// Register attribute driver for models namespace
5959
// IMPORTANT: We need to check if it's already registered to avoid duplicates
6060
$ormDriver = $container->getDefinition($ormDriverId);
6161
$methodCalls = $ormDriver->getMethodCalls();
62-
62+
6363
// Check if attribute driver is already registered
6464
$attributeDriverRegistered = false;
6565
foreach ($methodCalls as $call) {
66-
if ($call[0] === 'addDriver' &&
67-
isset($call[1][0]) &&
66+
if ($call[0] === 'addDriver' &&
67+
isset($call[1][0]) &&
6868
$call[1][0] instanceof Reference &&
6969
(string)$call[1][0] === $attributeDriverId) {
7070
$attributeDriverRegistered = true;
7171
break;
7272
}
7373
}
74-
74+
7575
// Register XML driver for Entity namespace FIRST (entities use XML mapping)
7676
// This must be registered before Model namespace to ensure entities are recognized
7777
// Create XML driver for entities if it doesn't exist
@@ -81,14 +81,18 @@ public function process(ContainerBuilder $container): void
8181
$bundleReflection = new \ReflectionClass(\Lexik\Bundle\TranslationBundle\LexikTranslationBundle::class);
8282
$bundleDir = dirname($bundleReflection->getFileName());
8383
$doctrinePath = $bundleDir . '/Resources/config/doctrine';
84-
84+
8585
$realDoctrinePath = realpath($doctrinePath);
8686
if ($realDoctrinePath) {
8787
$doctrinePath = $realDoctrinePath;
8888
}
89-
90-
// Create XML driver for entities using the same class as the model XML driver
91-
$xmlDriverClass = $container->getParameter('doctrine.orm.metadata.xml.class');
89+
90+
// Create XML driver for entities using SimplifiedXmlDriver
91+
if (class_exists(SimplifiedXmlDriver::class)) {
92+
$xmlDriverClass = SimplifiedXmlDriver::class;
93+
} else {
94+
$xmlDriverClass = $container->getParameter('doctrine.orm.metadata.xml.class');
95+
}
9296
$entityDriverDefinition = new Definition($xmlDriverClass, [
9397
[$doctrinePath => 'Lexik\Bundle\TranslationBundle\Entity'],
9498
SimplifiedXmlDriver::DEFAULT_FILE_EXTENSION,
@@ -97,18 +101,18 @@ public function process(ContainerBuilder $container): void
97101
$entityDriverDefinition->setPublic(false);
98102
$container->setDefinition($entityXmlDriverId, $entityDriverDefinition);
99103
}
100-
104+
101105
// Register XML driver for Entity namespace FIRST
102106
$entityDriverRegistered = false;
103107
foreach ($methodCalls as $call) {
104-
if ($call[0] === 'addDriver' &&
105-
isset($call[1][1]) &&
108+
if ($call[0] === 'addDriver' &&
109+
isset($call[1][1]) &&
106110
$call[1][1] === 'Lexik\Bundle\TranslationBundle\Entity') {
107111
$entityDriverRegistered = true;
108112
break;
109113
}
110114
}
111-
115+
112116
if (!$entityDriverRegistered && $container->hasDefinition($entityXmlDriverId)) {
113117
// Insert at the beginning to ensure Entity namespace is processed first
114118
$newMethodCalls = [[
@@ -121,15 +125,15 @@ public function process(ContainerBuilder $container): void
121125
$ormDriver->setMethodCalls($newMethodCalls);
122126
$methodCalls = $newMethodCalls; // Update for next checks
123127
}
124-
128+
125129
// Register attribute driver if not already registered
126130
if (!$attributeDriverRegistered && $container->hasDefinition($attributeDriverId)) {
127131
// Remove any existing registration for the Model namespace (XML driver)
128132
$newMethodCalls = [];
129133
foreach ($methodCalls as $call) {
130134
// Skip XML driver registration for Model namespace
131-
if ($call[0] === 'addDriver' &&
132-
isset($call[1][1]) &&
135+
if ($call[0] === 'addDriver' &&
136+
isset($call[1][1]) &&
133137
$call[1][1] === 'Lexik\Bundle\TranslationBundle\Model' &&
134138
isset($call[1][0]) &&
135139
$call[1][0] instanceof Reference &&
@@ -139,13 +143,13 @@ public function process(ContainerBuilder $container): void
139143
}
140144
$newMethodCalls[] = $call;
141145
}
142-
146+
143147
// Add attribute driver for Model namespace
144148
$newMethodCalls[] = [
145149
'addDriver',
146150
[new Reference($attributeDriverId), 'Lexik\Bundle\TranslationBundle\Model']
147151
];
148-
152+
149153
// Update method calls
150154
$ormDriver->setMethodCalls($newMethodCalls);
151155
} elseif (!$container->hasDefinition($attributeDriverId) && $container->hasDefinition('lexik_translation.orm.metadata.xml')) {

DependencyInjection/LexikTranslationExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ protected function buildTranslationStorageDefinition(ContainerBuilder $container
124124
$args = [new Reference('doctrine'), $objectManager ?? 'default'];
125125

126126
// Create XML driver for backward compatibility
127-
$this->createDoctrineMappingDriver($container, 'lexik_translation.orm.metadata.xml', '%doctrine.orm.metadata.xml.class%');
127+
if (class_exists(SimplifiedXmlDriver::class)) {
128+
$xmlDriverClass = SimplifiedXmlDriver::class;
129+
} else {
130+
$xmlDriverClass = $container->getParameter('doctrine.orm.metadata.xml.class');
131+
}
132+
$this->createDoctrineMappingDriver($container, 'lexik_translation.orm.metadata.xml', $xmlDriverClass);
128133

129134
// Create attribute driver for models (MappedSuperclass) that now use PHP attributes
130135
$this->createDoctrineAttributeDriver($container, 'lexik_translation.orm.metadata.attribute');
@@ -144,8 +149,8 @@ protected function buildTranslationStorageDefinition(ContainerBuilder $container
144149
}
145150

146151
$args[] = [
147-
'trans_unit' => new Parameter(sprintf('lexik_translation.%s.trans_unit.class', $storage)),
148-
'translation' => new Parameter(sprintf('lexik_translation.%s.translation.class', $storage)),
152+
'trans_unit' => new Parameter(sprintf('lexik_translation.%s.trans_unit.class', $storage)),
153+
'translation' => new Parameter(sprintf('lexik_translation.%s.translation.class', $storage)),
149154
'file' => new Parameter(sprintf('lexik_translation.%s.file.class', $storage))
150155
];
151156

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ DOCKER_RUN = docker compose run --rm lexik_translation
77
help:
88
@echo "LexikTranslationBundle - Available commands:"
99
@echo ""
10-
@echo " make up Start services in the background"
10+
@echo " make up Start services in the background"
1111
@echo " make ensure-up Start services and install dependencies (composer)"
1212
@echo " make down Stop and remove containers"
1313
@echo " make build Build Docker images"

Tests/Unit/Translation/TranslatorTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public function testAddDatabaseResources(): void
3636
$expected = [
3737
'de' => [
3838
['database', 'DB', 'superTranslations']
39-
],
39+
],
4040
'en' => [
41-
['database', 'DB', 'messages'],
41+
['database', 'DB', 'messages'],
4242
['database', 'DB', 'superTranslations']
43-
],
43+
],
4444
'fr' => [
45-
['database', 'DB', 'messages'],
45+
['database', 'DB', 'messages'],
4646
['database', 'DB', 'superTranslations'],
4747
]
4848
];
@@ -140,10 +140,10 @@ protected function createTranslator($em, $cacheDir): TranslatorMock
140140
];
141141

142142
return new TranslatorMock(
143-
container: $container,
144-
formatter: new MessageFormatter(),
145-
defaultLocale: 'en',
146-
loaderIds: $loaderIds,
143+
container: $container,
144+
formatter: new MessageFormatter(),
145+
defaultLocale: 'en',
146+
loaderIds: $loaderIds,
147147
options: $options
148148
);
149149
}
@@ -170,9 +170,9 @@ protected function createFakeCacheFiles($cacheDir): void
170170

171171
class TranslatorMock extends Translator
172172
{
173-
public $dbResources = [];
173+
public array $dbResources = [];
174174
public array $options = [
175-
'cache_dir' => '',
175+
'cache_dir' => '',
176176
'debug' => false,
177177
'resource_files' => [],
178178
'cache_vary' => [],
@@ -197,4 +197,4 @@ public function addResource(string $format, mixed $resource, string $locale, ?st
197197

198198
parent::addResource($format, $resource, $locale, $domain);
199199
}
200-
}
200+
}

Tests/Unit/Util/DataGrid/DataGridRequestHandlerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ private function getReflectionMethod($name)
133133
{
134134
$class = new \ReflectionClass(DataGridRequestHandler::class);
135135
$method = $class->getMethod($name);
136-
$method->setAccessible(true);
137136

138137
return $method;
139138
}

0 commit comments

Comments
 (0)