PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.1.0
Defender Security – Malware Scanner, Login Security & Firewall v6.1.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 6.1.0, at lib/packages/DI/Definition/Source/SourceCache.php

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