vendor/fri0z/mldev-base-bundle/src/Imagine/CacheManager.php line 76

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Lezhnev Maxim <info@mldev.ru>
  4.  * @copyright 2019
  5.  */
  6. namespace MLDev\BaseBundle\Imagine;
  7. use Imagine\Image\ImagineInterface;
  8. use Psr\Container\ContainerInterface;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. /**
  14.  * Class CacheManager
  15.  * @package MLDev\BaseBundle\Imagine
  16.  */
  17. class CacheManager
  18. {
  19.     /**
  20.      * @var string
  21.      */
  22.     private $webRoot;
  23.     /**
  24.      * @var Request
  25.      */
  26.     private $request;
  27.     /**
  28.      * @var ImagineInterface
  29.      */
  30.     private $imagine;
  31.     /**
  32.      * @var Filesystem
  33.      */
  34.     private $filesystem;
  35.     /**
  36.      * @var CacheResolver
  37.      */
  38.     private $cacheResolver;
  39.     /**
  40.      * @var FilterManager
  41.      */
  42.     private $filterManager;
  43.     /**
  44.      * CacheManager constructor.
  45.      *
  46.      * @param RequestStack $request
  47.      * @param Filesystem $filesystem
  48.      * @param ImagineInterface $imagine
  49.      * @param CacheResolver $cacheResolver
  50.      * @param FilterManager $filterManager
  51.      * @param string $publicDir
  52.      */
  53.     public function __construct(
  54.         RequestStack $request,
  55.         Filesystem $filesystem,
  56.         ImagineInterface $imagine,
  57.         CacheResolver $cacheResolver,
  58.         FilterManager $filterManager,
  59.         string $publicDir
  60.     )
  61.     {
  62.         $this->request $request->getCurrentRequest();
  63.         $this->imagine $imagine;
  64.         $this->filesystem $filesystem;
  65.         $this->cacheResolver $cacheResolver;
  66.         $this->filterManager $filterManager;
  67.         $this->publicDir $publicDir;
  68.     }
  69.     /**
  70.      * Forces image caching and returns path to cached image.
  71.      *
  72.      * @param string $path
  73.      * @param string $filter
  74.      * @param array $options
  75.      *
  76.      * @return string|null
  77.      */
  78.     public function cacheImage($path$filter$options = [])
  79.     {
  80.         /** @var string $basePath */
  81.         $basePath $this->request->getBaseUrl();
  82.         $path '/' ltrim($path'/');
  83.         // @TODO: find out why I need double urldecode to get a valid path
  84.         $browserPath urldecode(
  85.             urldecode(
  86.                 $this->cacheResolver->getBrowserPath($path$filter$options)
  87.             )
  88.         );
  89.         if (!empty($basePath) && === strpos($browserPath$basePath)) {
  90.             $browserPath substr($browserPathstrlen($basePath));
  91.         }
  92. //        // if cache path cannot be determined, return 404
  93.         if (null === $browserPath) {
  94.             return $path;
  95.         }
  96.         $realPath realpath($this->publicDir) . $browserPath;
  97.         $sourcePath realpath($this->publicDir) . $path;
  98.         // if the file has already been cached, just return path
  99.         if (file_exists($realPath)) {
  100.             return $browserPath;
  101.         }
  102.         if (!file_exists($sourcePath)) {
  103.             return $path;
  104.         }
  105.         $dir pathinfo($realPathPATHINFO_DIRNAME);
  106.         if (!is_dir($dir)) {
  107.             if (false === $this->filesystem->mkdir($dir)) {
  108.                 throw new \RuntimeException(sprintf(
  109.                     'Could not create directory %s'$dir
  110.                 ));
  111.             }
  112.         }
  113.         // TODO: get rid of hard-coded quality and format
  114.         $this->filterManager->getFilter($filter$options)
  115.             ->apply($this->imagine->open($sourcePath))
  116.             ->save($realPath, array(
  117.                 'quality' => $this->filterManager->getOption($filter"quality"100),
  118.                 'format' => $this->filterManager->getOption($filter"format"null)
  119.             ));
  120.         return $browserPath;
  121.     }
  122. }