vendor/liip/imagine-bundle/Imagine/Cache/Resolver/WebPathResolver.php line 86

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