[PropertyAccess] Use ArrayAdapter in debug mode

This commit is contained in:
Robin Chalas 2017-03-08 20:24:49 +01:00
parent 2fad5affe6
commit dca3f365d7
2 changed files with 40 additions and 4 deletions

View File

@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
use Doctrine\Common\Annotations\Reader;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -1256,10 +1257,16 @@ class FrameworkExtension extends Extension
if (method_exists(PropertyAccessor::class, 'createCache')) {
$propertyAccessDefinition = $container->register('cache.property_access', AdapterInterface::class);
$propertyAccessDefinition->setPublic(false);
$propertyAccessDefinition->setFactory(array(PropertyAccessor::class, 'createCache'));
$propertyAccessDefinition->setArguments(array(null, null, $version, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
$propertyAccessDefinition->addTag('cache.pool', array('clearer' => 'cache.default_clearer'));
$propertyAccessDefinition->addTag('monolog.logger', array('channel' => 'cache'));
if (!$container->getParameter('kernel.debug')) {
$propertyAccessDefinition->setFactory(array(PropertyAccessor::class, 'createCache'));
$propertyAccessDefinition->setArguments(array(null, null, $version, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
$propertyAccessDefinition->addTag('cache.pool', array('clearer' => 'cache.default_clearer'));
$propertyAccessDefinition->addTag('monolog.logger', array('channel' => 'cache'));
} else {
$propertyAccessDefinition->setClass(ArrayAdapter::class);
$propertyAccessDefinition->setArguments(array(0, false));
}
}
$this->addClassesToCompile(array(

View File

@ -14,7 +14,9 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
@ -25,6 +27,7 @@ use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
@ -67,6 +70,32 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertTrue($def->getArgument(1));
}
public function testPropertyAccessCache()
{
$container = $this->createContainerFromFile('property_accessor');
if (!method_exists(PropertyAccessor::class, 'createCache')) {
return $this->assertFalse($container->hasDefinition('cache.property_access'));
}
$cache = $container->getDefinition('cache.property_access');
$this->assertSame(array(PropertyAccessor::class, 'createCache'), $cache->getFactory(), 'PropertyAccessor::createCache() should be used in non-debug mode');
$this->assertSame(AdapterInterface::class, $cache->getClass());
}
public function testPropertyAccessCacheWithDebug()
{
$container = $this->createContainerFromFile('property_accessor', array('kernel.debug' => true));
if (!method_exists(PropertyAccessor::class, 'createCache')) {
return $this->assertFalse($container->hasDefinition('cache.property_access'));
}
$cache = $container->getDefinition('cache.property_access');
$this->assertNull($cache->getFactory());
$this->assertSame(ArrayAdapter::class, $cache->getClass(), 'ArrayAdapter should be used in debug mode');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage CSRF protection needs sessions to be enabled.