/
githubmirror
/
symfony
Обзор
Документация
Войти
/
githubmirror
/
symfony
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
8.2
src/Symfony/Component/JsonStreamer/CacheWarmer/StreamerCacheWarmer.php
115 строк
4 KB
Mathias Arlaud
[JsonStreamer] Handle value objects
06 мар 2026, 17:50
06 мар 2026, 17:50
8cf1714
Код
Авторство
О чём код?
<?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\JsonStreamer\CacheWarmer; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use Symfony\Component\Config\ConfigCacheFactoryInterface; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; use Symfony\Component\JsonStreamer\Exception\ExceptionInterface; use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoaderInterface; use Symfony\Component\JsonStreamer\Read\StreamReaderGenerator; use Symfony\Component\JsonStreamer\Write\StreamWriterGenerator; use Symfony\Component\TypeInfo\Type; /** * Generates stream readers and stream writers PHP files. * * @author Mathias Arlaud <mathias.arlaud@gmail.com> * * @internal */ final class StreamerCacheWarmer implements CacheWarmerInterface { private StreamWriterGenerator $streamWriterGenerator; private StreamReaderGenerator $streamReaderGenerator; /** * @param iterable<class-string, array{object: bool, list: bool}> $streamable */ public function __construct( private iterable $streamable, PropertyMetadataLoaderInterface $streamWriterPropertyMetadataLoader, PropertyMetadataLoaderInterface $streamReaderPropertyMetadataLoader, string $streamWritersDir, string $streamReadersDir, private LoggerInterface $logger = new NullLogger(), ?ConfigCacheFactoryInterface $configCacheFactory = null, ?ContainerInterface $transformers = null, ) { $transformers ??= new class implements ContainerInterface { public function has(string $id): bool { return false; } public function get(string $id): mixed { throw new \LogicException('No transformers available.'); } }; $this->streamWriterGenerator = new StreamWriterGenerator($streamWriterPropertyMetadataLoader, $transformers, $streamWritersDir, $configCacheFactory); $this->streamReaderGenerator = new StreamReaderGenerator($streamReaderPropertyMetadataLoader, $transformers, $streamReadersDir, $configCacheFactory); } public function warmUp(string $cacheDir, ?string $buildDir = null): array { foreach ($this->streamable as $className => $streamable) { if ($streamable['object']) { $type = Type::object($className); $this->warmUpStreamWriter($type); $this->warmUpStreamReaders($type); } if ($streamable['list']) { $type = Type::list(Type::object($className)); $this->warmUpStreamWriter($type); $this->warmUpStreamReaders($type); } } return []; } public function isOptional(): bool { return true; } private function warmUpStreamWriter(Type $type): void { try { $this->streamWriterGenerator->generate($type); } catch (ExceptionInterface $e) { $this->logger->debug('Cannot generate "json" stream writer for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); } } private function warmUpStreamReaders(Type $type): void { try { $this->streamReaderGenerator->generate($type, false); } catch (ExceptionInterface $e) { $this->logger->debug('Cannot generate "json" string stream reader for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); } try { $this->streamReaderGenerator->generate($type, true); } catch (ExceptionInterface $e) { $this->logger->debug('Cannot generate "json" resource stream reader for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); } } }