PluginProbe
Auto Alt Text / 3.0.3
Auto Alt Text v3.0.3
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / vendor-prefixed / php-di / php-di / src / Definition / Source / SourceCache.php

SourceCache.php in Auto Alt Text 3.0.3, at vendor-prefixed/php-di/php-di/src/Definition/Source/SourceCache.php

89 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AATXT\Vendor\DI\Definition\Source;
4
5 use AATXT\Vendor\DI\Definition\AutowireDefinition;
6 use AATXT\Vendor\DI\Definition\Definition;
7 use AATXT\Vendor\DI\Definition\ObjectDefinition;
8
9 /**
10 * Decorator that caches another definition source.
11 *
12 * @author Matthieu Napoli <matthieu@mnapoli.fr>
13 */
14 class SourceCache implements DefinitionSource, MutableDefinitionSource
15 {
16 /**
17 * @var string
18 */
19 const CACHE_KEY = 'php-di.definitions.';
20
21 /**
22 * @var DefinitionSource
23 */
24 private $cachedSource;
25
26 /**
27 * @var string
28 */
29 private $cacheNamespace;
30
31 public function __construct(DefinitionSource $cachedSource, string $cacheNamespace = '')
32 {
33 $this->cachedSource = $cachedSource;
34 $this->cacheNamespace = $cacheNamespace;
35 }
36
37 public function getDefinition(string $name)
38 {
39 $definition = apcu_fetch($this->getCacheKey($name));
40
41 if ($definition === false) {
42 $definition = $this->cachedSource->getDefinition($name);
43
44 // Update the cache
45 if ($this->shouldBeCached($definition)) {
46 apcu_store($this->getCacheKey($name), $definition);
47 }
48 }
49
50 return $definition;
51 }
52
53 /**
54 * Used only for the compilation so we can skip the cache safely.
55 */
56 public function getDefinitions() : array
57 {
58 return $this->cachedSource->getDefinitions();
59 }
60
61 public static function isSupported() : bool
62 {
63 return function_exists('apcu_fetch')
64 && ini_get('apc.enabled')
65 && ! ('cli' === \PHP_SAPI && ! ini_get('apc.enable_cli'));
66 }
67
68 public function getCacheKey(string $name) : string
69 {
70 return self::CACHE_KEY . $this->cacheNamespace . $name;
71 }
72
73 public function addDefinition(Definition $definition)
74 {
75 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.');
76 }
77
78 private function shouldBeCached(Definition $definition = null) : bool
79 {
80 return
81 // Cache missing definitions
82 ($definition === null)
83 // Object definitions are used with `make()`
84 || ($definition instanceof ObjectDefinition)
85 // Autowired definitions cannot be all compiled and are used with `make()`
86 || ($definition instanceof AutowireDefinition);
87 }
88 }
89