3 declare(strict_types=1);
5 namespace CuyZ\Valinor\Definition\Repository\Cache;
7 use CuyZ\Valinor\Definition\FunctionDefinition;
8 use CuyZ\Valinor\Definition\Repository\FunctionDefinitionRepository;
9 use CuyZ\Valinor\Utility\Reflection\Reflection;
10 use Psr\SimpleCache\CacheInterface;
13 final class CacheFunctionDefinitionRepository implements FunctionDefinitionRepository
15 public function __construct(
16 private FunctionDefinitionRepository $delegate,
17 /** @var CacheInterface<FunctionDefinition> */
18 private CacheInterface $cache
21 public function for(callable $function): FunctionDefinition
23 $reflection = Reflection::function($function);
24 $key = "function-definition-{$reflection->getFileName()}-{$reflection->getStartLine()}-{$reflection->getEndLine()}";
26 $entry = $this->cache->get($key);
32 $definition = $this->delegate->for($function);
34 $this->cache->set($key, $definition);