Skip to content

Commit 052d68c

Browse files
authored
[config] Add query/result cache doctrine support in StringExtensionToConfigBuilderRector (#830)
* [doctrine] add fixture for query cache * [doctrine] fix query and result cache driver
1 parent 5498b8a commit 052d68c

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
6+
7+
return static function (ContainerConfigurator $containerConfigurator): void {
8+
$containerConfigurator->extension('doctrine', [
9+
'orm' => [
10+
'query_cache_driver' => [
11+
'type' => 'pool',
12+
'pool' => 'pool_name_one',
13+
],
14+
'result_cache_driver' => [
15+
'type' => 'pool',
16+
'pool' => 'pool_name_two',
17+
],
18+
],
19+
]);
20+
};
21+
22+
?>
23+
-----
24+
<?php
25+
26+
declare(strict_types=1);
27+
28+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
29+
30+
return static function (\Symfony\Config\DoctrineConfig $doctrineConfig): void {
31+
$doctrineConfig->orm()->entityManager('default')->queryCacheDriver()->type('pool')->pool('pool_name_one');
32+
$doctrineConfig->orm()->entityManager('default')->resultCacheDriver()->type('pool')->pool('pool_name_two');
33+
};
34+
35+
?>

rules/CodeQuality/Rector/Closure/StringExtensionToConfigBuilderRector.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,29 @@ private function createMethodCallStmts(Array_ $configurationArray, Variable $con
213213
$itemName = StringUtils::underscoreToCamelCase($itemName);
214214

215215
// doctrine: implicit default connection now must be explicit
216-
if ($currentConfigCaller instanceof MethodCall && $this->isName(
217-
$currentConfigCaller->name,
218-
'dbal'
219-
)) {
220-
// this option requires call on connection(...)
221-
if (in_array($itemName, ['dbnameSuffix'], true)) {
222-
$currentConfigCaller = new MethodCall(
223-
$currentConfigCaller,
224-
'connection',
225-
$this->nodeFactory->createArgs(['default'])
226-
);
227-
}
216+
// this option requires call on connection(...)
217+
if ($currentConfigCaller instanceof MethodCall && $this->isName($currentConfigCaller->name, 'dbal') && $itemName === 'dbnameSuffix') {
218+
$currentConfigCaller = new MethodCall(
219+
$currentConfigCaller,
220+
'connection',
221+
$this->nodeFactory->createArgs(['default'])
222+
);
228223
}
229224

230225
$methodCall = new MethodCall($currentConfigCaller, $itemName, $args);
231226
$methodCallStmts[] = new Expression($methodCall);
232227
continue;
233228
}
234229

230+
if ($currentConfigCaller instanceof MethodCall && $this->isName($currentConfigCaller->name, 'orm') && in_array($itemName, ['query_cache_driver', 'result_cache_driver'], true)) {
231+
// implicit entityManagerDefault(...)
232+
$currentConfigCaller = new MethodCall(
233+
$currentConfigCaller,
234+
'entityManager',
235+
$this->nodeFactory->createArgs(['default'])
236+
);
237+
}
238+
235239
$nextMethodCallExpressions = $this->nestedConfigCallsFactory->create(
236240
[$itemConfiguration],
237241
$currentConfigCaller,

rules/Configs/ConfigArrayHandler/NestedConfigCallsFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
private const METHOD_RENAMES = [
2424
// monolog
2525
'excludedHttpCodes' => 'excludedHttpCode',
26+
// doctrine
27+
'result_cache_driver' => 'resultCacheDriver',
28+
'query_cache_driver' => 'queryCacheDriver',
2629
];
2730

2831
public function __construct(
@@ -66,6 +69,8 @@ public function create(
6669
}
6770
}
6871

72+
$mainMethodName = self::METHOD_RENAMES[$mainMethodName] ?? $mainMethodName;
73+
6974
$mainMethodCall = new MethodCall($configCaller, $mainMethodName);
7075
if ($key === 0 && $nextKeyArgument && is_string($nextKey)) {
7176
$mainMethodCall->args[] = new Arg(new String_($nextKey));

0 commit comments

Comments
 (0)