10000 [Bridge/Doctrine] Use cache.prefix.seed parameter for generating cache namespace by nicolas-grekas · Pull Request #20616 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Bridge/Doctrine] Use cache.prefix.seed parameter for generating cache namespace #20616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,14 @@ protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheD

if (!isset($cacheDriver['namespace'])) {
// generate a unique namespace for the given application
$env = $container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment');
$hash = hash('sha256', $env);
$namespace = 'sf2'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.$hash;
if ($container->hasParameter('cache.prefix.seed')) {
$seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
} else {
$seed = '_'.$container->getParameter('kernel.root_dir');
}
$seed .= '.'.$container->getParameter('kernel.name').'.'.$container->getParameter('kernel.environment').'.'.$container->getParameter('kernel.debug');
$hash = hash('sha256', $seed);
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.$hash;

$cacheDriver['namespace'] = $namespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class CachePoolClearerPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
$container->getParameterBag()->remove('cache.prefix.seed');

foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) {
foreach (array_reverse($attributes) as $attr) {
if (isset($attr['clearer'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ class CachePoolPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
$namespaceSuffix = '';

foreach (array('kernel.name', 'kernel.environment', 'kernel.debug', 'cache.prefix.seed') as $key) {
if ($container->hasParameter($key)) {
$namespaceSuffix .= '.'.$container->getParameter($key);
}
if ($container->hasParameter('cache.prefix.seed')) {
$seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
} else {
$seed = '_'.$container->getParameter('kernel.root_dir');
}
$container->getParameterBag()->remove('cache.prefix.seed');
$seed .= '.'.$container->getParameter('kernel.name').'.'.$container->getParameter('kernel.environment').'.'.$container->getParameter('kernel.debug');

$aliases = $container->getAliases();
$attributes = array(
Expand All @@ -56,7 +54,7 @@ public function process(ContainerBuilder $container)
}
}
if (!isset($tags[0]['namespace'])) {
$tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, $id);
$tags[0]['namespace'] = $this->getNamespace($seed, $id);
}
if (isset($tags[0]['clearer'])) {
$clearer = strtolower($tags[0]['clearer']);
Expand Down Expand Up @@ -88,9 +86,9 @@ public function process(ContainerBuilder $container)
}
}

private function getNamespace($namespaceSuffix, $id)
private function getNamespace($seed, $id)
{
return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$namespaceSuffix, true))), 0, 10);
return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$seed, true))), 0, 10);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class CachePoolClearerPassTest extends \PHPUnit_Framework_TestCase
public function testPoolRefsAreWeak()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');

$publicPool = new Definition();
$publicPool->addArgument('namespace');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ protected function setUp()
public function testNamespaceArgumentIsReplaced()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$adapter = new Definition();
$adapter->setAbstract(true);
$adapter->addTag('cache.pool');
Expand All @@ -41,12 +45,16 @@ public function testNamespaceArgumentIsReplaced()

$this->cachePoolPass->process($container);

$this->assertSame('kRFqMp5odS', $cachePool->getArgument(0));
$this->assertSame('C42Pcl9VBJ', $cachePool->getArgument(0));
}

public function testArgsAreReplaced()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('cache.prefix.seed', 'foo');
$cachePool = new Definition();
$cachePool->addTag('cache.pool', array(
'provider' => 'foobar',
Expand All @@ -61,7 +69,7 @@ public function testArgsAreReplaced()

$this->assertInstanceOf(Reference::class, $cachePool->getArgument(0));
$this->assertSame('foobar', (string) $cachePool->getArgument(0));
$this->assertSame('kRFqMp5odS', $cachePool->getArgument(1));
$this->assertSame('KO3xHaFEZU', $cachePool->getArgument(1));
$this->assertSame(3, $cachePool->getArgument(2));
}

Expand All @@ -72,6 +80,10 @@ public function testArgsAreReplaced()
public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$adapter = new Definition();
$adapter->setAbstract(true);
$adapter->addTag('cache.pool');
Expand Down
0