-
-
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.
Avoid is_psr6 option by creating compiler pass
- Loading branch information
1 parent
bf3805e
commit bccc1c7
Showing
10 changed files
with
206 additions
and
144 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,65 @@ | ||
<?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\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
use function array_keys; | ||
use function is_a; | ||
use function trigger_deprecation; | ||
|
||
/** @internal */ | ||
final class CacheCompatibilitPass implements CompilerPassInterface | ||
{ | ||
private const CACHE_METHODS_PSR6_SUPPORT_MAP = [ | ||
'setMetadataCache' => true, | ||
'setQueryCacheImpl' => false, | ||
'setResultCacheImpl' => false, | ||
]; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
foreach (array_keys($container->findTaggedServiceIds(IdGeneratorPass::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 | ||
); | ||
} | ||
|
||
$container->setDefinition($aliasId, (new Definition($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 () 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 () 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(); | ||
} | ||
} |
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
Oops, something went wrong.