Skip to content

[Serializer] Instantiator - Add an interface and default implementation to instantiate objects #30956

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

Closed
wants to merge 3 commits into from
Closed
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
35 changes: 35 additions & 0 deletions src/Symfony/Component/Serializer/Context/ChildContextFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Context;

use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

/**
* Create a child context during serialization/deserialization process.
*
* @author Baptiste Leduc <baptiste.leduc@gmail.com>
*/
class ChildContextFactory implements ChildContextFactoryInterface
{
public const ATTRIBUTES = AbstractObjectNormalizer::ATTRIBUTES;

public function create(array $parentContext, string $attribute, ?string $format = null, array $defaultContext = []): array
{
if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
$parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
} else {
unset($parentContext[self::ATTRIBUTES]);
}

return $parentContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Context;

/**
* Defines the interface to create a child context during serialization/deserialization or instantiation process.
*
* @author Baptiste Leduc <baptiste.leduc@gmail.com>
*/
interface ChildContextFactoryInterface
{
public function create(array $parentContext, string $attribute, ?string $format = null, array $defaultContext = []): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Context;

use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

/**
* Create a child context with cache_key during serialization/deserialization or instantiation process.
*
* @author Baptiste Leduc <baptiste.leduc@gmail.com>
*/
class ObjectChildContextFactory extends ChildContextFactory
{
public const EXCLUDE_FROM_CACHE_KEY = AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY;
public const IGNORED_ATTRIBUTES = AbstractObjectNormalizer::IGNORED_ATTRIBUTES;

public function create(array $parentContext, string $attribute, ?string $format = null, array $defaultContext = []): array
{
$parentContext = parent::create($parentContext, $attribute, $format, $defaultContext);
$parentContext['cache_key'] = $this->getAttributesCacheKey($parentContext, $format, $defaultContext);

return $parentContext;
}

/**
* Builds the cache key for the attributes cache.
*
* The key must be different for every option in the context that could change which attributes should be handled.
*
* @return bool|string
*/
private function getAttributesCacheKey(array $context, ?string $format = null, array $defaultContext = [])
{
foreach ($context[self::EXCLUDE_FROM_CACHE_KEY] ?? $defaultContext[self::EXCLUDE_FROM_CACHE_KEY] ?? [] as $key) {
unset($context[$key]);
}
unset($context[self::EXCLUDE_FROM_CACHE_KEY]);
unset($context['cache_key']); // avoid artificially different keys

try {
return md5($format.serialize([
'context' => $context,
'ignored' => $context[self::IGNORED_ATTRIBUTES] ?? $defaultContext[self::IGNORED_ATTRIBUTES] ?? [],
]));
} catch (\Exception $exception) {
// The context cannot be serialized, skip the cache
return false;
}
}
}
Loading