forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
[SerDes] Introduce component #5
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
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
42cc163
[SerDes] Introduce component
mtarld b013f64
add enum support
mtarld 27cc000
updates to select 7.0 branch
mtarld f6aec4b
set eager read and instantiator by default
mtarld 3c872be
split context builders and remove cache trait
mtarld fdb4a66
use callable in formatter signature
mtarld 5497e69
minor fixes
mtarld f12ec50
unify symfony context
mtarld 7371213
remove support of non backed enums
mtarld e885240
static data providers
mtarld a0a7af9
update xxh128 hash instead of md5
mtarld 972b2af
simplify object instantiation
mtarld a05dd46
remove serializable attribute
mtarld bed00e3
split deserializer
mtarld 1a3870c
use null coalescing assignment operator
mtarld f62e6d6
fix attribute context builders
mtarld a6425e5
remove nullable and improve cache
mtarld 560ebb1
improve tagged iterator arguments
mtarld 838b68e
add force_generate_template option
mtarld 2ddc742
allow to ignore properties during serialization
mtarld 3bb892b
better deserializers split
mtarld 1f575f0
add proper defaults
mtarld 1640817
rename attributes
mtarld 62ab9c3
improve lazy instantiator
mtarld f58de4d
refactor template generator
mtarld d46de84
remove useless serialize union selector
mtarld 418f968
make context builders internal
mtarld d7c4c08
handle mixed, object and raw array
mtarld 260c991
handle groups
mtarld 394407c
remove initial closure nodes
mtarld 59a74d8
handle csv serialization
mtarld d5c5e40
handle csv deserialization
mtarld 227eab3
remove stream validation
mtarld 74026d9
add readonly classes
mtarld 7a1d404
fix missing internal
mtarld 0f57cab
move types out of internal
mtarld c41cd4c
fix csv serialization
mtarld d9a05c8
serialize hooks
mtarld 65738fc
deserialize hooks
mtarld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.0 | ||
--- | ||
|
||
* Register SerDes services | ||
|
||
6.3 | ||
--- | ||
|
||
|
147 changes: 147 additions & 0 deletions
147
src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerDesCacheWarmer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer; | ||
use Symfony\Component\SerDes\Context\ContextBuilder\ContextBuilderInterface; | ||
use Symfony\Component\SerDes\Context\ContextBuilder\SerializeContextBuilderInterface; | ||
use Symfony\Component\SerDes\Exception\ExceptionInterface; | ||
use Symfony\Component\SerDes\SerializableResolver\SerializableResolverInterface; | ||
use Symfony\Component\SerDes\Template\TemplateHelper; | ||
use Symfony\Component\SerDes\Template\TemplateVariation; | ||
use Symfony\Component\VarExporter\ProxyHelper; | ||
|
||
use function Symfony\Component\SerDes\serialize_generate; | ||
|
||
/** | ||
* @author Mathias Arlaud <[email protected]> | ||
* | ||
* @experimental in 7.0 | ||
*/ | ||
final class SerDesCacheWarmer extends CacheWarmer | ||
{ | ||
/** | ||
* @var iterable<ContextBuilderInterface> | ||
*/ | ||
private iterable $contextBuilders = []; | ||
|
||
private readonly TemplateHelper $templateHelper; | ||
|
||
/** | ||
* @param iterable<SerializeContextBuilderInterface> $contextBuilders | ||
* @param list<string> $formats | ||
*/ | ||
public function __construct( | ||
private readonly SerializableResolverInterface $serializableResolver, | ||
private readonly string $templateCacheDir, | ||
private readonly string $lazyObjectCacheDir, | ||
private readonly array $formats, | ||
private readonly int $maxVariants, | ||
private readonly LoggerInterface $logger = new NullLogger(), | ||
) { | ||
$this->templateHelper = new TemplateHelper($this->templateCacheDir); | ||
} | ||
|
||
public function warmUp(string $cacheDir): array | ||
{ | ||
if (!file_exists($this->templateCacheDir)) { | ||
mkdir($this->templateCacheDir, recursive: true); | ||
} | ||
|
||
if (!file_exists($this->lazyObjectCacheDir)) { | ||
mkdir($this->lazyObjectCacheDir, recursive: true); | ||
} | ||
|
||
foreach ($this->serializableResolver->resolve() as $className) { | ||
$variants = $this->templateHelper->classTemplateVariants($className); | ||
if (\count($variants) > $this->maxVariants) { | ||
$this->logger->debug('Too many variants for "{className}", keeping only the first {maxVariants}.', ['className' => $className, 'maxVariants' => $this->maxVariants]); | ||
$variants = array_slice($variants, offset: 0, length: $this->maxVariants); | ||
} | ||
|
||
foreach ($this->formats as $format) { | ||
$this->warmClassTemplate($className, $variants, $format); | ||
} | ||
|
||
$this->warmClassLazyObject($className); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* @param iterable<ContextBuilderInterface> $contextBuilders | ||
* | ||
* @internal | ||
*/ | ||
public function setContextBuilders(iterable $contextBuilders): void | ||
{ | ||
$this->contextBuilders = $contextBuilders; | ||
} | ||
|
||
/** | ||
* @param class-string $className | ||
* @param list<list<TemplateVariation>> $variants | ||
*/ | ||
private function warmClassTemplate(string $className, array $variants, string $format): void | ||
{ | ||
try { | ||
$context = [ | ||
'cache_dir' => $this->templateCacheDir, | ||
'template_exists' => false, | ||
]; | ||
|
||
foreach ($this->contextBuilders as $contextBuilder) { | ||
$context = $contextBuilder->build($context, true); | ||
} | ||
|
||
foreach ($variants as $variant) { | ||
$variantContext = $context; | ||
|
||
$groupVariations = array_filter($variant, fn (TemplateVariation $v): bool => 'group' === $v->type); | ||
if ([] !== $groupVariations) { | ||
$variantContext['groups'] = array_map(fn (TemplateVariation $v): string => $v->value, $groupVariations); | ||
} | ||
|
||
$variantFilename = $this->templateHelper->templateFilename($className, $format, $variantContext); | ||
|
||
$this->writeCacheFile( | ||
$this->templateCacheDir.\DIRECTORY_SEPARATOR.$variantFilename, | ||
serialize_generate($className, $format, $variantContext), | ||
); | ||
} | ||
} catch (ExceptionInterface $e) { | ||
$this->logger->debug('Cannot generate template for "{className}": {exception}', ['className' => $className, 'exception' => $e]); | ||
} | ||
} | ||
|
||
/** | ||
* @param class-string $className | ||
*/ | ||
private function warmClassLazyObject(string $className): void | ||
{ | ||
$path = sprintf('%s%s%s.php', $this->lazyObjectCacheDir, \DIRECTORY_SEPARATOR, hash('xxh128', $className)); | ||
|
||
$this->writeCacheFile($path, sprintf( | ||
'class %s%s', | ||
sprintf('%sGhost', preg_replace('/\\\\/', '', $className)), | ||
ProxyHelper::generateLazyGhost(new \ReflectionClass($className)), | ||
)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.