bug #18630 [FrameworkBundle][Serializer] Fix a deprecation triggered by the ClassMetadataFactory (Ener-Getick)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[FrameworkBundle][Serializer] Fix a deprecation triggered by the ClassMetadataFactory

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT

Without apparent reasons, [FOSRestBundle's tests fail](https://travis-ci.org/FriendsOfSymfony/FOSRestBundle/jobs/124384888) since https://github.com/symfony/symfony/pull/18561.
```
Passing a Doctrine Cache instance as 2nd parameter of the "Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory" constructor is deprecated. This parameter will be removed in Symfony 4.0. Use the "Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory" class instead: 6x
    1x in ErrorWithTemplatingFormatTest::testSerializeExceptionHtml from FOS\RestBundle\Tests\Functional
    1x in SerializerErrorTest::testSerializeExceptionJson from FOS\RestBundle\Tests\Functional
    1x in SerializerErrorTest::testSerializeExceptionJsonWithoutDebug from FOS\RestBundle\Tests\Functional
    1x in SerializerErrorTest::testSerializeExceptionXml from FOS\RestBundle\Tests\Functional
    1x in SerializerErrorTest::testSerializeInvalidFormJson from FOS\RestBundle\Tests\Functional
    1x in SerializerErrorTest::testSerializeInvalidFormXml from FOS\RestBundle\Tests\Functional
```
We don't use cache in our tests but some of them are not in ``debug`` mode (will change soon) so the cache is automatically used.

This PR fixes this deprecation by detecting if the cache used by the serializer is psr6 compliant or not (if it is, then it replaces the default metadata factory by an instance of the new class ``CacheClassMetadataFactory``, otherwise the second parameter of the ``ClassMetadataFactory`` is used).

Commits
-------

15579d5 [FrameworkBundle] Deprecate framework.serializer.cache
eccbffb [Serializer] Improve a deprecation message
96e418a Revert "[FrameworkBundle] Fallback to default cache system in production for serializer"
This commit is contained in:
Nicolas Grekas 2016-04-27 09:24:49 +02:00
commit f2228d5cd4
14 changed files with 136 additions and 24 deletions

View File

@ -19,19 +19,19 @@ Form
* Support for data objects that implements both `Traversable` and `ArrayAccess`
in `ResizeFormListener::preSubmit` method has been deprecated and will be
removed in Symfony 4.0.
* Using callable strings as choice options in ChoiceType has been deprecated
in favor of `PropertyPath` in Symfony 4.0 use a "\Closure" instead.
Before:
```php
'choice_value' => new PropertyPath('range'),
'choice_label' => 'strtoupper',
```
After:
```php
'choice_value' => 'range',
'choice_label' => function ($choice) {
@ -88,6 +88,27 @@ FrameworkBundle
cache service. If you are using `serializer.mapping.cache.apc`, use
`serializer.mapping.cache.doctrine.apc` instead.
* The `framework.serializer.cache` option has been deprecated. Configure a cache pool
called `serializer` under `framework.cache.pools` instead.
Before:
```yaml
framework:
serializer:
cache: serializer.mapping.cache.apc
```
After:
```yaml
framework:
serializer: ~
cache:
pools:
serializer:
adapter: cache.adapter.apcu
HttpKernel
----------

View File

@ -16,26 +16,26 @@ Form
* Support for data objects that implements both `Traversable` and
`ArrayAccess` in `ResizeFormListener::preSubmit` method has been removed.
* Using callable strings as choice options in ChoiceType is not supported
* Using callable strings as choice options in ChoiceType is not supported
anymore in favor of passing PropertyPath instances.
Before:
```php
'choice_value' => new PropertyPath('range'),
'choice_label' => 'strtoupper',
```
After:
```php
'choice_value' => 'range',
'choice_label' => function ($choice) {
return strtoupper($choice);
},
```
FrameworkBundle
---------------
@ -78,6 +78,28 @@ FrameworkBundle
* The service `serializer.mapping.cache.apc` has been removed; use
`serializer.mapping.cache.doctrine.apc` instead.
* The `framework.serializer.cache` option has been removed. Configure a cache pool
called `serializer` under `framework.cache.pools` instead.
Before:
```yaml
framework:
serializer:
cache: serializer.mapping.cache.apc
```
After:
```yaml
framework:
serializer: ~
cache:
pools:
serializer:
adapter: cache.adapter.apcu
```
HttpKernel
----------

View File

@ -513,7 +513,7 @@ class Configuration implements ConfigurationInterface
->canBeEnabled()
->children()
->booleanNode('enable_annotations')->defaultFalse()->end()
->scalarNode('cache')->defaultValue('serializer.mapping.cache.symfony')->end()
->scalarNode('cache')->end()
->scalarNode('name_converter')->end()
->end()
->end()

View File

@ -24,6 +24,7 @@ use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
@ -982,7 +983,9 @@ class FrameworkExtension extends Extension
$chainLoader->replaceArgument(0, $serializerLoaders);
if (!$container->getParameter('kernel.debug')) {
if (isset($config['cache']) && $config['cache']) {
@trigger_error('The "framework.serializer.cache" option is deprecated since Symfony 3.1 and will be removed in 4.0. You can configure a cache pool called "serializer" under "framework.cache.pools" instead.', E_USER_DEPRECATED);
$container->setParameter(
'serializer.mapping.cache.prefix',
'serializer_'.$this->getKernelRootHash($container)
@ -991,6 +994,18 @@ class FrameworkExtension extends Extension
$container->getDefinition('serializer.mapping.class_metadata_factory')->replaceArgument(
1, new Reference($config['cache'])
);
} elseif (!$container->getParameter('kernel.debug')) {
$cacheMetadataFactory = new Definition(
CacheClassMetadataFactory::class,
array(
new Reference('serializer.mapping.class_metadata_factory.inner'),
new Reference('cache.pool.serializer'),
)
);
$cacheMetadataFactory->setPublic(false);
$cacheMetadataFactory->setDecoratedService('serializer.mapping.class_metadata_factory');
$container->setDefinition('serializer.mapping.cache_class_metadata_factory', $cacheMetadataFactory);
}
if (isset($config['name_converter']) && $config['name_converter']) {

View File

@ -38,10 +38,6 @@
</service>
<!-- Cache -->
<service id="serializer.mapping.cache.symfony" class="Symfony\Component\Cache\DoctrineProvider" public="false">
<argument type="service" id="cache.pool.serializer" />
</service>
<service id="serializer.mapping.cache.doctrine.apc" class="Doctrine\Common\Cache\ApcCache" public="false">
<call method="setNamespace">
<argument>%serializer.mapping.cache.prefix%</argument>

View File

@ -221,7 +221,6 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
'serializer' => array(
'enabled' => false,
'enable_annotations' => false,
'cache' => 'serializer.mapping.cache.symfony',
),
'property_access' => array(
'magic_call' => false,

View File

@ -66,7 +66,6 @@ $container->loadFromExtension('framework', array(
'serializer' => array(
'enabled' => true,
'enable_annotations' => true,
'cache' => 'serializer.mapping.cache.doctrine.apc',
'name_converter' => 'serializer.name_converter.camel_case_to_snake_case',
),
'ide' => 'file%%link%%format',

View File

@ -0,0 +1,8 @@
<?php
$container->loadFromExtension('framework', array(
'serializer' => array(
'enabled' => true,
'cache' => 'foo',
),
));

View File

@ -40,6 +40,6 @@
</framework:translator>
<framework:validation enabled="true" cache="validator.mapping.cache.doctrine.apc" />
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
<framework:serializer enabled="true" enable-annotations="true" cache="serializer.mapping.cache.doctrine.apc" name-converter="serializer.name_converter.camel_case_to_snake_case" />
<framework:serializer enabled="true" enable-annotations="true" name-converter="serializer.name_converter.camel_case_to_snake_case" />
</framework:config>
</container>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config>
<framework:serializer enabled="true" cache="foo"/>
</framework:config>
</container>

View File

@ -52,7 +52,6 @@ framework:
serializer:
enabled: true
enable_annotations: true
cache: serializer.mapping.cache.doctrine.apc
name_converter: serializer.name_converter.camel_case_to_snake_case
ide: file%%link%%format
request:

View File

@ -0,0 +1,4 @@
framework:
serializer:
enabled: true
cache: foo

View File

@ -449,7 +449,7 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertCount(1, $argument);
$this->assertEquals('Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader', $argument[0]->getClass());
$this->assertEquals(new Reference('serializer.mapping.cache.doctrine.apc'), $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
$this->assertNull($container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
$this->assertEquals(new Reference('serializer.name_converter.camel_case_to_snake_case'), $container->getDefinition('serializer.normalizer.object')->getArgument(1));
}
@ -521,6 +521,44 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertEquals(-1000, $tag[0]['priority']);
}
public function testSerializerCacheActivated()
{
$container = $this->createContainerFromFile('serializer_enabled');
$this->assertTrue($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
}
public function testSerializerCacheDisabled()
{
$container = $this->createContainerFromFile('serializer_enabled', array('kernel.debug' => true, 'kernel.container_class' => __CLASS__));
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
}
/**
* @group legacy
*/
public function testDeprecatedSerializerCacheOption()
{
$deprecations = array();
set_error_handler(function ($type, $msg) use (&$deprecations) {
if (E_USER_DEPRECATED !== $type) {
restore_error_handler();
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
}
$deprecations[] = $msg;
});
$container = $this->createContainerFromFile('serializer_legacy_cache', array('kernel.debug' => true, 'kernel.container_class' => __CLASS__));
restore_error_handler();
$this->assertCount(1, $deprecations);
$this->assertContains('The "framework.serializer.cache" option is deprecated', $deprecations[0]);
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
$this->assertEquals(new Reference('foo'), $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
}
public function testAssetHelperWhenAssetsAreEnabled()
{
$container = $this->createContainerFromFile('full');

View File

@ -50,7 +50,7 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
$this->cache = $cache;
if (null !== $cache) {
@trigger_error(sprintf('Passing a Doctrine Cache instance as 2nd parameter of the "%s" constructor is deprecated. This parameter will be removed in Symfony 4.0. Use the "%s" class instead.', __CLASS__, CacheClassMetadataFactory::class), E_USER_DEPRECATED);
@trigger_error(sprintf('Passing a Doctrine Cache instance as 2nd parameter of the "%s" constructor is deprecated since version 3.1. This parameter will be removed in Symfony 4.0. Use the "%s" class instead.', __CLASS__, CacheClassMetadataFactory::class), E_USER_DEPRECATED);
}
}