| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace PhpMyAdmin\MoTranslator\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PhpMyAdmin\MoTranslator\CacheException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PhpMyAdmin\MoTranslator\MoParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function apcu_enabled; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function apcu_entry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use function apcu_exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function apcu_fetch; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function apcu_store; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function array_combine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function array_keys; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function array_map; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use function assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use function function_exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use function is_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use function is_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | final class ApcuCache implements CacheInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public const LOADED_KEY = '__TRANSLATIONS_LOADED__'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var MoParser */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $locale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $domain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $ttl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $reloadOnMiss; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $prefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 34 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         MoParser $parser, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         string $locale, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         string $domain, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         int $ttl = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         bool $reloadOnMiss = true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         string $prefix = 'mo_' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 34 |  |         if (! (function_exists('apcu_enabled') && apcu_enabled())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |             throw new CacheException('ACPu extension must be installed and enabled'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 32 |  |         $this->parser = $parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 32 |  |         $this->locale = $locale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 32 |  |         $this->domain = $domain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 32 |  |         $this->ttl = $ttl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 32 |  |         $this->reloadOnMiss = $reloadOnMiss; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 32 |  |         $this->prefix = $prefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 32 |  |         $this->ensureTranslationsLoaded(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 16 |  |     public function get(string $msgid): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 16 |  |         $msgstr = apcu_fetch($this->getKey($msgid), $success); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 16 |  |         if ($success && is_string($msgstr)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 10 |  |             return $msgstr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 6 |  |         if (! $this->reloadOnMiss) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |             return $msgid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |         return $this->reloadOnMiss($msgid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 6 |  |     private function reloadOnMiss(string $msgid): string | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         // store original if translation is not present | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 6 |  |         $cached = apcu_entry($this->getKey($msgid), static function () use ($msgid) { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 4 |  |             return $msgid; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 6 |  |         }, $this->ttl); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         // if another process has updated cache, return early | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 6 |  |         if ($cached !== $msgid && is_string($cached)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  |             return $cached; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         // reload .mo file, in case entry has been evicted | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 4 |  |         $this->parser->parseIntoCache($this); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 4 |  |         $msgstr = apcu_fetch($this->getKey($msgid), $success); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 4 |  |         return $success && is_string($msgstr) ? $msgstr : $msgid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 20 |  |     public function set(string $msgid, string $msgstr): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 20 |  |         apcu_store($this->getKey($msgid), $msgstr, $this->ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 4 |  |     public function has(string $msgid): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 4 |  |         return apcu_exists($this->getKey($msgid)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |     public function setAll(array $translations): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |         $keys = array_map(function (string $msgid): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |             return $this->getKey($msgid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |         }, array_keys($translations)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         $translations = array_combine($keys, $translations); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         assert(is_array($translations)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |         apcu_store($translations, null, $this->ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 32 |  |     private function getKey(string $msgid): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 32 |  |         return $this->prefix . $this->locale . '.' . $this->domain . '.' . $msgid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 32 |  |     private function ensureTranslationsLoaded(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         // Try to prevent cache slam if multiple processes are trying to load translations. There is still a race | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         // between the exists check and creating the entry, but at least it's small | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 32 |  |         $key = $this->getKey(self::LOADED_KEY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 32 |  |         $loaded = apcu_exists($key) || apcu_entry($key, static function (): int { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 30 |  |             return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 30 |  |         }, $this->ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 32 |  |         if ($loaded) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 2 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 30 |  |         $this->parser->parseIntoCache($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 30 |  |         apcu_store($this->getKey(self::LOADED_KEY), 1, $this->ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  |