-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix compatibility layer for cache services (#1352)
Co-authored-by: Gabriel Ostrolucký <[email protected]>
- Loading branch information
1 parent
00648cb
commit e4ba833
Showing
12 changed files
with
241 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; | ||
|
||
use Doctrine\Common\Cache\CacheProvider; | ||
use Doctrine\Common\Cache\Psr6\CacheAdapter; | ||
use Doctrine\Common\Cache\Psr6\DoctrineProvider; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
use function array_keys; | ||
use function is_a; | ||
use function trigger_deprecation; | ||
|
||
/** @internal */ | ||
final class CacheCompatibilityPass implements CompilerPassInterface | ||
{ | ||
private const CONFIGURATION_TAG = 'doctrine.orm.configuration'; | ||
private const CACHE_METHODS_PSR6_SUPPORT_MAP = [ | ||
'setMetadataCache' => true, | ||
'setQueryCacheImpl' => false, | ||
'setResultCacheImpl' => false, | ||
]; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
foreach (array_keys($container->findTaggedServiceIds(self::CONFIGURATION_TAG)) as $id) { | ||
foreach ($container->getDefinition($id)->getMethodCalls() as $methodCall) { | ||
if (! isset(self::CACHE_METHODS_PSR6_SUPPORT_MAP[$methodCall[0]])) { | ||
continue; | ||
} | ||
|
||
$aliasId = (string) $methodCall[1][0]; | ||
$definitionId = (string) $container->getAlias($aliasId); | ||
$isPsr6 = is_a($container->getDefinition($definitionId)->getClass(), CacheItemPoolInterface::class, true); | ||
$shouldBePsr6 = self::CACHE_METHODS_PSR6_SUPPORT_MAP[$methodCall[0]]; | ||
|
||
if ($shouldBePsr6 === $isPsr6) { | ||
continue; | ||
} | ||
|
||
$targetClass = CacheProvider::class; | ||
$targetFactory = DoctrineProvider::class; | ||
|
||
if ($shouldBePsr6) { | ||
$targetClass = CacheItemPoolInterface::class; | ||
$targetFactory = CacheAdapter::class; | ||
|
||
trigger_deprecation( | ||
'doctrine/doctrine-bundle', | ||
'2.4', | ||
'Configuring doctrine/cache is deprecated. Please update the cache service "%s" to use a PSR-6 cache.', | ||
$definitionId | ||
); | ||
} | ||
|
||
$compatibilityLayerId = $definitionId . '.compatibility_layer'; | ||
$container->setAlias($aliasId, $compatibilityLayerId); | ||
$container->register($compatibilityLayerId, $targetClass) | ||
->setFactory([$targetFactory, 'wrap']) | ||
->addArgument(new Reference($definitionId)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel; | ||
use Doctrine\Bundle\DoctrineBundle\Tests\TestCase; | ||
use Doctrine\Common\Cache\Psr6\DoctrineProvider; | ||
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
class CacheCompatibilityPassTest extends TestCase | ||
{ | ||
use ExpectDeprecationTrait; | ||
|
||
public function testLegacyCacheConfigUsingServiceDefinedByApplication(): void | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
(new class () extends TestKernel { | ||
public function registerContainerConfiguration(LoaderInterface $loader): void | ||
{ | ||
parent::registerContainerConfiguration($loader); | ||
$loader->load(static function (ContainerBuilder $containerBuilder): void { | ||
$containerBuilder->loadFromExtension( | ||
'doctrine', | ||
['orm' => ['query_cache_driver' => ['type' => 'service', 'id' => 'custom_cache_service']]] | ||
); | ||
$containerBuilder->setDefinition( | ||
'custom_cache_service', | ||
(new Definition(DoctrineProvider::class)) | ||
->setArguments([new Definition(ArrayAdapter::class)]) | ||
->setFactory([DoctrineProvider::class, 'wrap']) | ||
); | ||
}); | ||
} | ||
})->boot(); | ||
} | ||
|
||
/** @group legacy */ | ||
public function testMetadataCacheConfigUsingPsr6ServiceDefinedByApplication(): void | ||
{ | ||
$this->expectDeprecation('%aThe "metadata_cache_driver" configuration key is deprecated.%a'); | ||
(new class (false) extends TestKernel { | ||
public function registerContainerConfiguration(LoaderInterface $loader): void | ||
{ | ||
parent::registerContainerConfiguration($loader); | ||
$loader->load(static function (ContainerBuilder $containerBuilder): void { | ||
$containerBuilder->loadFromExtension( | ||
'doctrine', | ||
['orm' => ['metadata_cache_driver' => ['type' => 'service', 'id' => 'custom_cache_service']]] | ||
); | ||
$containerBuilder->setDefinition( | ||
'custom_cache_service', | ||
new Definition(ArrayAdapter::class) | ||
); | ||
}); | ||
} | ||
})->boot(); | ||
} | ||
|
||
/** @group legacy */ | ||
public function testMetdataCacheConfigUsingNonPsr6ServiceDefinedByApplication(): void | ||
{ | ||
$this->expectDeprecation('Since doctrine/doctrine-bundle 2.4: Configuring doctrine/cache is deprecated. Please update the cache service "custom_cache_service" to use a PSR-6 cache.'); | ||
(new class (false) extends TestKernel { | ||
public function registerContainerConfiguration(LoaderInterface $loader): void | ||
{ | ||
parent::registerContainerConfiguration($loader); | ||
$loader->load(static function (ContainerBuilder $containerBuilder): void { | ||
$containerBuilder->loadFromExtension( | ||
'doctrine', | ||
['orm' => ['metadata_cache_driver' => ['type' => 'service', 'id' => 'custom_cache_service']]] | ||
); | ||
$containerBuilder->setDefinition( | ||
'custom_cache_service', | ||
(new Definition(DoctrineProvider::class)) | ||
->setArguments([new Definition(ArrayAdapter::class)]) | ||
->setFactory([DoctrineProvider::class, 'wrap']) | ||
); | ||
}); | ||
} | ||
})->boot(); | ||
} | ||
} |
Oops, something went wrong.