|
| 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\JsonStreamer\CacheWarmer; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Psr\Log\NullLogger; |
| 16 | +use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
| 17 | +use Symfony\Component\JsonStreamer\Exception\ExceptionInterface; |
| 18 | +use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoaderInterface; |
| 19 | +use Symfony\Component\JsonStreamer\Read\StreamReaderGenerator; |
| 20 | +use Symfony\Component\JsonStreamer\Write\StreamWriterGenerator; |
| 21 | +use Symfony\Component\TypeInfo\Type; |
| 22 | + |
| 23 | +/** |
| 24 | + * Generates stream readers and stream writers PHP files. |
| 25 | + * |
| 26 | + * @author Mathias Arlaud <mathias.arlaud@gmail.com> |
| 27 | + * |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +final class StreamerCacheWarmer implements CacheWarmerInterface |
| 31 | +{ |
| 32 | + private StreamWriterGenerator $streamWriterGenerator; |
| 33 | + private StreamReaderGenerator $streamReaderGenerator; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param iterable<class-string, array{object: bool, list: bool}> $streamable |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + private iterable $streamable, |
| 40 | + PropertyMetadataLoaderInterface $streamWriterPropertyMetadataLoader, |
| 41 | + PropertyMetadataLoaderInterface $streamReaderPropertyMetadataLoader, |
| 42 | + string $streamWritersDir, |
| 43 | + string $streamReadersDir, |
| 44 | + private LoggerInterface $logger = new NullLogger(), |
| 45 | + ) { |
| 46 | + $this->streamWriterGenerator = new StreamWriterGenerator($streamWriterPropertyMetadataLoader, $streamWritersDir); |
| 47 | + $this->streamReaderGenerator = new StreamReaderGenerator($streamReaderPropertyMetadataLoader, $streamReadersDir); |
| 48 | + } |
| 49 | + |
| 50 | + public function warmUp(string $cacheDir, ?string $buildDir = null): array |
| 51 | + { |
| 52 | + foreach ($this->streamable as $className => $streamable) { |
| 53 | + if ($streamable['object']) { |
| 54 | + $type = Type::object($className); |
| 55 | + |
| 56 | + $this->warmUpStreamWriter($type); |
| 57 | + $this->warmUpStreamReaders($type); |
| 58 | + } |
| 59 | + |
| 60 | + if ($streamable['list']) { |
| 61 | + $type = Type::list(Type::object($className)); |
| 62 | + |
| 63 | + $this->warmUpStreamWriter($type); |
| 64 | + $this->warmUpStreamReaders($type); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return []; |
| 69 | + } |
| 70 | + |
| 71 | + public function isOptional(): bool |
| 72 | + { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + private function warmUpStreamWriter(Type $type): void |
| 77 | + { |
| 78 | + try { |
| 79 | + $this->streamWriterGenerator->generate($type); |
| 80 | + } catch (ExceptionInterface $e) { |
| 81 | + $this->logger->debug('Cannot generate "json" stream writer for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private function warmUpStreamReaders(Type $type): void |
| 86 | + { |
| 87 | + try { |
| 88 | + $this->streamReaderGenerator->generate($type, false); |
| 89 | + } catch (ExceptionInterface $e) { |
| 90 | + $this->logger->debug('Cannot generate "json" string stream reader for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + $this->streamReaderGenerator->generate($type, true); |
| 95 | + } catch (ExceptionInterface $e) { |
| 96 | + $this->logger->debug('Cannot generate "json" resource stream reader for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments