PluginProbe
Elementor Website Builder – more than just a page builder / 3.26.2
Elementor Website Builder – more than just a page builder v3.26.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / vendor_prefixed / php-di / php-di / src / Definition / Source / SourceCache.php

SourceCache.php in Elementor Website Builder – more than just a page builder 3.26.2, at vendor_prefixed/php-di/php-di/src/Definition/Source/SourceCache.php

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