1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace DI\Definition\Source;
|
|
|
6 |
|
|
|
7 |
use DI\Definition\AutowireDefinition;
|
|
|
8 |
use DI\Definition\Definition;
|
|
|
9 |
use DI\Definition\ObjectDefinition;
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Decorator that caches another definition source.
|
|
|
13 |
*
|
|
|
14 |
* @author Matthieu Napoli <matthieu@mnapoli.fr>
|
|
|
15 |
*/
|
|
|
16 |
class SourceCache implements DefinitionSource, MutableDefinitionSource
|
|
|
17 |
{
|
|
|
18 |
public const CACHE_KEY = 'php-di.definitions.';
|
|
|
19 |
|
|
|
20 |
public function __construct(
|
|
|
21 |
private DefinitionSource $cachedSource,
|
|
|
22 |
private string $cacheNamespace = '',
|
|
|
23 |
) {
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
public function getDefinition(string $name) : Definition|null
|
|
|
27 |
{
|
|
|
28 |
$definition = apcu_fetch($this->getCacheKey($name));
|
|
|
29 |
|
|
|
30 |
if ($definition === false) {
|
|
|
31 |
$definition = $this->cachedSource->getDefinition($name);
|
|
|
32 |
|
|
|
33 |
// Update the cache
|
|
|
34 |
if ($this->shouldBeCached($definition)) {
|
|
|
35 |
apcu_store($this->getCacheKey($name), $definition);
|
|
|
36 |
}
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
return $definition;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Used only for the compilation so we can skip the cache safely.
|
|
|
44 |
*/
|
|
|
45 |
public function getDefinitions() : array
|
|
|
46 |
{
|
|
|
47 |
return $this->cachedSource->getDefinitions();
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public static function isSupported() : bool
|
|
|
51 |
{
|
|
|
52 |
return function_exists('apcu_fetch')
|
|
|
53 |
&& ini_get('apc.enabled')
|
|
|
54 |
&& ! ('cli' === \PHP_SAPI && ! ini_get('apc.enable_cli'));
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function getCacheKey(string $name) : string
|
|
|
58 |
{
|
|
|
59 |
return self::CACHE_KEY . $this->cacheNamespace . $name;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public function addDefinition(Definition $definition) : void
|
|
|
63 |
{
|
|
|
64 |
throw new \LogicException('You cannot set a definition at runtime on a container that has caching enabled. Doing so would risk caching the definition for the next execution, where it might be different. You can either put your definitions in a file, remove the cache or ->set() a raw value directly (PHP object, string, int, ...) instead of a PHP-DI definition.');
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
private function shouldBeCached(Definition $definition = null) : bool
|
|
|
68 |
{
|
|
|
69 |
return
|
|
|
70 |
// Cache missing definitions
|
|
|
71 |
($definition === null)
|
|
|
72 |
// Object definitions are used with `make()`
|
|
|
73 |
|| ($definition instanceof ObjectDefinition)
|
|
|
74 |
// Autowired definitions cannot be all compiled and are used with `make()`
|
|
|
75 |
|| ($definition instanceof AutowireDefinition);
|
|
|
76 |
}
|
|
|
77 |
}
|