|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of rekalogika/mapper package. |
| 7 | + * |
| 8 | + * (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE file |
| 11 | + * that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Rekalogika\Mapper\Debug; |
| 15 | + |
| 16 | +use Rekalogika\Mapper\Mapping\Mapping; |
| 17 | +use Rekalogika\Mapper\Mapping\MappingFactoryInterface; |
| 18 | +use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
| 19 | + |
| 20 | +final class TraceableMappingFactory implements |
| 21 | + MappingFactoryInterface, |
| 22 | + CacheWarmerInterface |
| 23 | +{ |
| 24 | + private bool $mappingCollected = false; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private MappingFactoryInterface $decorated, |
| 28 | + private MapperDataCollector $dataCollector |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function isOptional(): bool |
| 33 | + { |
| 34 | + return $this->decorated instanceof CacheWarmerInterface && $this->decorated->isOptional(); |
| 35 | + } |
| 36 | + |
| 37 | + public function warmUp(string $cacheDir, ?string $buildDir = null): array |
| 38 | + { |
| 39 | + if ($this->decorated instanceof CacheWarmerInterface) { |
| 40 | + // @phpstan-ignore-next-line |
| 41 | + return $this->decorated->warmUp($cacheDir, $buildDir); |
| 42 | + } |
| 43 | + |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + public function getMapping(): Mapping |
| 48 | + { |
| 49 | + if ($this->mappingCollected) { |
| 50 | + return $this->decorated->getMapping(); |
| 51 | + } |
| 52 | + |
| 53 | + $mapping = $this->decorated->getMapping(); |
| 54 | + |
| 55 | + $this->dataCollector->collectMappingTable($mapping); |
| 56 | + $this->mappingCollected = true; |
| 57 | + |
| 58 | + return $mapping; |
| 59 | + } |
| 60 | +} |
0 commit comments