| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Liip\ImagineBundle\Imagine\Cache\Resolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Liip\ImagineBundle\Binary\BinaryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Routing\RequestContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class WebPathResolver implements ResolverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var Filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var RequestContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $requestContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $webRoot; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $cachePrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $cacheRoot; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param Filesystem     $filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param RequestContext $requestContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string         $webRootDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string         $cachePrefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         Filesystem $filesystem, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         RequestContext $requestContext, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $webRootDir, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $cachePrefix = 'media/cache' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->filesystem = $filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->requestContext = $requestContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function resolve($path, $filter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return sprintf('%s/%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $this->getBaseUrl(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $this->getFileUrl($path, $filter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function isStored($path, $filter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return is_file($this->getFilePath($path, $filter)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function store(BinaryInterface $binary, $path, $filter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->filesystem->dumpFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->getFilePath($path, $filter), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $binary->getContent() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function remove(array $paths, array $filters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (empty($paths) && empty($filters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if (empty($paths)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $filtersCacheDir = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             foreach ($filters as $filter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $filtersCacheDir[] = $this->cacheRoot.'/'.$filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $this->filesystem->remove($filtersCacheDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         foreach ($paths as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             foreach ($filters as $filter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $this->filesystem->remove($this->getFilePath($path, $filter)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     protected function getFilePath($path, $filter) | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         return $this->webRoot.'/'.$this->getFileUrl($path, $filter); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     protected function getFileUrl($path, $filter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         // crude way of sanitizing URL scheme ("protocol") part | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $path = str_replace('://', '---', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     protected function getBaseUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $port = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         if ('https' == $this->requestContext->getScheme() && $this->requestContext->getHttpsPort() != 443) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             $port = ":{$this->requestContext->getHttpsPort()}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if ('http' == $this->requestContext->getScheme() && $this->requestContext->getHttpPort() != 80) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             $port = ":{$this->requestContext->getHttpPort()}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $baseUrl = $this->requestContext->getBaseUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         if ('.php' == substr($this->requestContext->getBaseUrl(), -4)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $baseUrl = rtrim($baseUrl, '/\\'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return sprintf('%s://%s%s%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $this->requestContext->getScheme(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             $this->requestContext->getHost(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             $port, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             $baseUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 159 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |  |