PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / DI / Definition / Source / SourceCache.php

SourceCache.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.0, at lib/packages/DI/Definition/Source/SourceCache.php

89 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPMU_DEV\Defender\Vendor\DI\Definition\Source;
4
5 use WPMU_DEV\Defender\Vendor\DI\Definition\AutowireDefinition;
6 use WPMU_DEV\Defender\Vendor\DI\Definition\Definition;
7 use WPMU_DEV\Defender\Vendor\DI\Definition\ObjectDefinition;
8
9 /**
10 * Decorator that caches another definition source.
11 *
12 * @author Matthieu Napoli <[email protected]>
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-WPMU_DEV\Defender\Vendor\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