PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Dependencies / DI / Definition / Source / SourceCache.php

SourceCache.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.8.0, at includes/Dependencies/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 namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Source;
4
5 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\AutowireDefinition;
6 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
7 use WPDeveloper\BetterDocs\Dependencies\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 public function __construct(DefinitionSource $cachedSource)
27 {
28 $this->cachedSource = $cachedSource;
29 }
30
31 public function getDefinition(string $name)
32 {
33 $definition = apcu_fetch(self::CACHE_KEY . $name);
34
35 if ($definition === false) {
36 $definition = $this->cachedSource->getDefinition($name);
37
38 // Update the cache
39 if ($this->shouldBeCached($definition)) {
40 apcu_store(self::CACHE_KEY . $name, $definition);
41 }
42 }
43
44 return $definition;
45 }
46
47 /**
48 * Used only for the compilation so we can skip the cache safely.
49 */
50 public function getDefinitions() : array
51 {
52 return $this->cachedSource->getDefinitions();
53 }
54
55 public static function isSupported() : bool
56 {
57 return function_exists('apcu_fetch')
58 && ini_get('apc.enabled')
59 && ! ('cli' === PHP_SAPI && ! ini_get('apc.enable_cli'));
60 }
61
62 public function addDefinition(Definition $definition)
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-WPDeveloper\BetterDocs\Dependencies\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