Skip to content

Commit

Permalink
bug #21936 [PropertyAccess] Use ArrayAdapter in debug mode (chalasr)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.2 branch.

Discussion
----------

[PropertyAccess] Use ArrayAdapter in debug mode

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21894
| License       | MIT
| Doc PR        | n/a

https://github.com/symfony/symfony/pull/21936/files?w=1

Commits
-------

dca3f36 [PropertyAccess] Use ArrayAdapter in debug mode
  • Loading branch information
nicolas-grekas committed Mar 9, 2017
2 parents e2965c2 + dca3f36 commit 24f36b6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -1256,10 +1257,16 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
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(
Expand Down
Expand Up @@ -14,7 +14,9 @@
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;
Expand All @@ -25,6 +27,7 @@
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;
Expand Down Expand Up @@ -67,6 +70,32 @@ public function testPropertyAccessWithOverriddenValues()
$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.
Expand Down

0 comments on commit 24f36b6

Please sign in to comment.