|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\Mapper\Mapping\Provider; |
| 6 | + |
| 7 | +use TypeLang\Mapper\Mapping\Reader\ReaderInterface; |
| 8 | + |
| 9 | +abstract class CacheProvider extends Decorator |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var non-empty-string |
| 13 | + */ |
| 14 | + protected const DEFAULT_CACHE_PREFIX = 'tlm_'; |
| 15 | + |
| 16 | + protected const DEFAULT_CACHE_TTL = null; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + protected readonly string $prefix = self::DEFAULT_CACHE_PREFIX, |
| 20 | + protected readonly \DateInterval|int|null $ttl = self::DEFAULT_CACHE_TTL, |
| 21 | + ReaderInterface|ProviderInterface $delegate = new NullProvider(), |
| 22 | + ) { |
| 23 | + parent::__construct($delegate); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @param \ReflectionClass<object> $class |
| 28 | + * |
| 29 | + * @return non-empty-string |
| 30 | + */ |
| 31 | + protected function getKey(\ReflectionClass $class): string |
| 32 | + { |
| 33 | + return $this->prefix |
| 34 | + . self::getKeyName($class) |
| 35 | + . self::getKeySuffix($class); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param \ReflectionClass<object> $class |
| 40 | + * |
| 41 | + * @return non-empty-string |
| 42 | + */ |
| 43 | + protected static function getKeyName(\ReflectionClass $class): string |
| 44 | + { |
| 45 | + return \str_replace(['\\', "\0"], '_', \strtolower($class->name)); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param \ReflectionClass<object> $class |
| 50 | + */ |
| 51 | + protected static function getKeySuffix(\ReflectionClass $class): string |
| 52 | + { |
| 53 | + $pathname = $class->getFileName(); |
| 54 | + |
| 55 | + if ($pathname === false) { |
| 56 | + return ''; |
| 57 | + } |
| 58 | + |
| 59 | + return (string) \filemtime($pathname); |
| 60 | + } |
| 61 | +} |
0 commit comments