|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\JsonEncoder\CacheWarmer; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Psr\Log\NullLogger; |
| 16 | +use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
| 17 | +use Symfony\Component\JsonEncoder\Decode\DecoderGenerator; |
| 18 | +use Symfony\Component\JsonEncoder\Encode\EncoderGenerator; |
| 19 | +use Symfony\Component\JsonEncoder\Exception\ExceptionInterface; |
| 20 | +use Symfony\Component\JsonEncoder\Mapping\PropertyMetadataLoaderInterface; |
| 21 | +use Symfony\Component\TypeInfo\Type; |
| 22 | + |
| 23 | +/** |
| 24 | + * Generates encoders and decoders PHP files. |
| 25 | + * |
| 26 | + * @author Mathias Arlaud <mathias.arlaud@gmail.com> |
| 27 | + * |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +final class EncoderDecoderCacheWarmer implements CacheWarmerInterface |
| 31 | +{ |
| 32 | + private EncoderGenerator $encoderGenerator; |
| 33 | + private DecoderGenerator $decoderGenerator; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param iterable<class-string> $encodableClassNames |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + private iterable $encodableClassNames, |
| 40 | + PropertyMetadataLoaderInterface $encodePropertyMetadataLoader, |
| 41 | + PropertyMetadataLoaderInterface $decodePropertyMetadataLoader, |
| 42 | + string $encodersDir, |
| 43 | + string $decodersDir, |
| 44 | + bool $forceEncodeChunks = false, |
| 45 | + private LoggerInterface $logger = new NullLogger(), |
| 46 | + ) { |
| 47 | + $this->encoderGenerator = new EncoderGenerator($encodePropertyMetadataLoader, $encodersDir, $forceEncodeChunks); |
| 48 | + $this->decoderGenerator = new DecoderGenerator($decodePropertyMetadataLoader, $decodersDir); |
| 49 | + } |
| 50 | + |
| 51 | + public function warmUp(string $cacheDir, ?string $buildDir = null): array |
| 52 | + { |
| 53 | + foreach ($this->encodableClassNames as $className) { |
| 54 | + $type = Type::object($className); |
| 55 | + |
| 56 | + $this->warmUpEncoder($type); |
| 57 | + $this->warmUpDecoders($type); |
| 58 | + } |
| 59 | + |
| 60 | + return []; |
| 61 | + } |
| 62 | + |
| 63 | + public function isOptional(): bool |
| 64 | + { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + private function warmUpEncoder(Type $type): void |
| 69 | + { |
| 70 | + try { |
| 71 | + $this->encoderGenerator->generate($type); |
| 72 | + } catch (ExceptionInterface $e) { |
| 73 | + $this->logger->debug('Cannot generate "json" encoder for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private function warmUpDecoders(Type $type): void |
| 78 | + { |
| 79 | + try { |
| 80 | + $this->decoderGenerator->generate($type, decodeFromStream: false); |
| 81 | + } catch (ExceptionInterface $e) { |
| 82 | + $this->logger->debug('Cannot generate "json" decoder for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + $this->decoderGenerator->generate($type, decodeFromStream: true); |
| 87 | + } catch (ExceptionInterface $e) { |
| 88 | + $this->logger->debug('Cannot generate "json" streaming decoder for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments