65dec14647e03215401de14ab3e2feee739e3397
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\Definition\Repository\Cache;
6
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;
11
12 /** @internal */
13 final class CacheFunctionDefinitionRepository implements FunctionDefinitionRepository
14 {
15 public function __construct(
16 private FunctionDefinitionRepository $delegate,
17 /** @var CacheInterface<FunctionDefinition> */
18 private CacheInterface $cache
19 ) {}
20
21 public function for(callable $function): FunctionDefinition
22 {
23 $reflection = Reflection::function($function);
24 $key = "function-definition-{$reflection->getFileName()}-{$reflection->getStartLine()}-{$reflection->getEndLine()}";
25
26 $entry = $this->cache->get($key);
27
28 if ($entry) {
29 return $entry;
30 }
31
32 $definition = $this->delegate->for($function);
33
34 $this->cache->set($key, $definition);
35
36 return $definition;
37 }
38 }