8000 [FrameworkBundle][Serializer] Fix a deprecation triggered by the ClassMetadataFactory by GuilhemN · Pull Request #18630 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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 3 commits into from
Apr 27, 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
31 changes: 26 additions & 5 deletions UPGRADE-3.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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: ~
Copy link
Member
cache:
pools:
serializer:
adapter: cache.adapter.apcu

HttpKernel
----------

Expand Down
36 changes: 29 additions & 7 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
Expand Down Expand Up @@ -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: ~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be kept as the two code blocks aren't equal otherwise (the serializer is not enabled if this is not present).

cache:
pools:
serializer:
adapter: cache.adapter.apcu
```

HttpKernel
----------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
->canBeEnabled()
->children()
->booleanNode('enable_annotations')->defaultFalse()->end()
->scalarNode('cache')->defaultValue('serializer.mapping.cache.symfony')->end()
->scalarNode('cache')->end()
->scalarNode('name_converter')->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -982,7 +983,9 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder

$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)
Expand All @@ -991,6 +994,18 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
$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']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ protected static function getBundleDefaultConfig()
'serializer' => array(
'enabled' => false,
'enable_annotations' => false,
'cache' => 'serializer.mapping.cache.symfony',
),
'property_access' => array(
'magic_call' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
'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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('framework', array(
'serializer' => array(
'enabled' => true,
'cache' => 'foo',
),
));
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
framework:
serializer:
enabled: true
cache: foo
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public function testSerializerEnabled()

$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));
}

Expand Down Expand Up @@ -521,6 +521,44 @@ public function testObjectNormalizerRegistered()
$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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be tagged @group legacy so that we don't forget removing it when preparing 4.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

{
$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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(LoaderInterface $loader, Cache $cache = null)
$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);
}
}

Expand Down
0