| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Tebru\Retrofit\Internal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\SimpleCache\CacheInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Cache\Adapter\ArrayAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Cache\Adapter\ChainAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Cache\Adapter\NullAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Cache\Adapter\PhpFilesAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Cache\Adapter\Psr16Adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Cache\Exception\CacheException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Cache\Psr16Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Cache\Simple\ArrayCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Cache\Simple\ChainCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Cache\Simple\NullCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Cache\Simple\PhpFilesCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @codeCoverageIgnore | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | final class CacheProvider | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * Create a "file cache", chained to a "memory cache" depending on symfony/cache version | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param string $cacheDir | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @return CacheInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @throws CacheException | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public static function createFileCache(string $cacheDir): CacheInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // >= Symfony 4.3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (class_exists('Symfony\Component\Cache\Psr16Cache')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             return new Psr16Cache(new ChainAdapter([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 new Psr16Adapter(self::createMemoryCache()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 new PhpFilesAdapter('', 0, $cacheDir), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return new ChainCache([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             self::createMemoryCache(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             new PhpFilesCache('', 0, $cacheDir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         ]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * Create a "memory cache" depending on symfony/cache version | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return CacheInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public static function createMemoryCache(): CacheInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         // >= Symfony 4.3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (class_exists('Symfony\Component\Cache\Psr16Cache')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             return new Psr16Cache(new ArrayAdapter(0, false)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return new ArrayCache(0, false); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * Create a "null" cache (for annotations) depending on symfony/cache version | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @return CacheInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public static function createNullCache(): CacheInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // >= Symfony 4.3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if (class_exists('Symfony\Component\Cache\Psr16Cache')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             return new Psr16Cache(new NullAdapter()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return new NullCache(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  |