Skip to content

Commit fed148b

Browse files
committed
[PropertyAccess] Use ArrayAdapter in debug mode
1 parent 2fad5af commit fed148b

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Annotations\Reader;
1515
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
1616
use Symfony\Component\Cache\Adapter\AdapterInterface;
17+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1718
use Symfony\Component\DependencyInjection\Alias;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -1254,16 +1255,24 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
12541255
}
12551256

12561257
if (method_exists(PropertyAccessor::class, 'createCache')) {
1257-
$propertyAccessDefinition = $container->register('cache.property_access', AdapterInterface::class);
1258+
$propertyAccessDefinition = $container->register('cache.property_access');
12581259
$propertyAccessDefinition->setPublic(false);
1259-
$propertyAccessDefinition->setFactory(array(PropertyAccessor::class, 'createCache'));
1260-
$propertyAccessDefinition->setArguments(array(null, null, $version, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
12611260
$propertyAccessDefinition->addTag('cache.pool', array('clearer' => 'cache.default_clearer'));
12621261
$propertyAccessDefinition->addTag('monolog.logger', array('channel' => 'cache'));
1262+
1263+
if (!$container->getParameter('kernel.debug')) {
1264+
$propertyAccessDefinition->setClass(AdapterInterface::class);
1265+
$propertyAccessDefinition->setFactory(array(PropertyAccessor::class, 'createCache'));
1266+
$propertyAccessDefinition->setArguments(array(null, null, $version, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
1267+
} else {
1268+
$propertyAccessDefinition->setClass(ArrayAdapter::class);
1269+
$propertyAccessDefinition->setArguments(array(0));
1270+
}
12631271
}
12641272

12651273
$this->addClassesToCompile(array(
12661274
'Symfony\Component\Cache\Adapter\ApcuAdapter',
1275+
'Symfony\Component\Cache\Adapter\ArrayAdapter',
12671276
'Symfony\Component\Cache\Adapter\FilesystemAdapter',
12681277
'Symfony\Component\Cache\CacheItem',
12691278
));

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
1616
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
17+
use Symfony\Component\Cache\Adapter\AdapterInterface;
1718
use Symfony\Component\Cache\Adapter\ApcuAdapter;
19+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1820
use Symfony\Component\Cache\Adapter\ChainAdapter;
1921
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
2022
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
@@ -25,6 +27,7 @@
2527
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
2628
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2729
use Symfony\Component\DependencyInjection\Reference;
30+
use Symfony\Component\PropertyAccess\PropertyAccessor;
2831
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
2932
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
3033
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
@@ -67,6 +70,32 @@ public function testPropertyAccessWithOverriddenValues()
6770
$this->assertTrue($def->getArgument(1));
6871
}
6972

73+
public function testPropertyAccessCache()
74+
{
75+
$container = $this->createContainerFromFile('property_accessor');
76+
77+
if (!method_exists(PropertyAccessor::class, 'createCache')) {
78+
return $this->assertFalse($container->hasDefinition('cache.property_access'));
79+
}
80+
81+
$cache = $container->getDefinition('cache.property_access');
82+
$this->assertSame(array(PropertyAccessor::class, 'createCache'), $cache->getFactory(), 'PropertyAccessor::createCache() should be used in non-debug mode');
83+
$this->assertSame(AdapterInterface::class, $cache->getClass());
84+
}
85+
86+
public function testPropertyAccessCacheWithDebug()
87+
{
88+
$container = $this->createContainerFromFile('property_accessor', array('kernel.debug' => true));
89+
90+
if (!method_exists(PropertyAccessor::class, 'createCache')) {
91+
return $this->assertFalse($container->hasDefinition('cache.property_access'));
92+
}
93+
94+
$cache = $container->getDefinition('cache.property_access');
95+
$this->assertNull($cache->getFactory());
96+
$this->assertSame(ArrayAdapter::class, $cache->getClass(), 'ArrayAdapter should be used in debug mode');
97+
}
98+
7099
/**
71100
* @expectedException \LogicException
72101
* @expectedExceptionMessage CSRF protection needs sessions to be enabled.

0 commit comments

Comments
 (0)