vendor/ezsystems/ezpublish-kernel/eZ/Bundle/EzPublishCoreBundle/Imagine/IORepositoryResolver.php line 56

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Bundle\EzPublishCoreBundle\Imagine;
  7. use eZ\Publish\Core\Base\Exceptions\NotFoundException;
  8. use eZ\Publish\Core\IO\IOServiceInterface;
  9. use eZ\Publish\Core\IO\Values\MissingBinaryFile;
  10. use eZ\Publish\SPI\Variation\VariationPurger;
  11. use Liip\ImagineBundle\Binary\BinaryInterface;
  12. use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
  13. use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;
  14. use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
  15. use Symfony\Component\Routing\RequestContext;
  16. /**
  17.  * LiipImagineBundle cache resolver using eZ IO repository.
  18.  */
  19. class IORepositoryResolver implements ResolverInterface
  20. {
  21.     const VARIATION_ORIGINAL 'original';
  22.     /** @var \eZ\Publish\Core\IO\IOServiceInterface */
  23.     private $ioService;
  24.     /** @var \Symfony\Component\Routing\RequestContext */
  25.     private $requestContext;
  26.     /** @var FilterConfiguration */
  27.     private $filterConfiguration;
  28.     /** @var \eZ\Publish\SPI\Variation\VariationPurger */
  29.     private $variationPurger;
  30.     /** @var \eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator */
  31.     private $variationPathGenerator;
  32.     public function __construct(
  33.         IOServiceInterface $ioService,
  34.         RequestContext $requestContext,
  35.         FilterConfiguration $filterConfiguration,
  36.         VariationPurger $variationPurger,
  37.         VariationPathGenerator $variationPathGenerator
  38.     ) {
  39.         $this->ioService $ioService;
  40.         $this->requestContext $requestContext;
  41.         $this->filterConfiguration $filterConfiguration;
  42.         $this->variationPurger $variationPurger;
  43.         $this->variationPathGenerator $variationPathGenerator;
  44.     }
  45.     public function isStored($path$filter)
  46.     {
  47.         return $this->ioService->exists($this->getFilePath($path$filter));
  48.     }
  49.     public function resolve($path$filter)
  50.     {
  51.         try {
  52.             $binaryFile $this->ioService->loadBinaryFile($path);
  53.             // Treat a MissingBinaryFile as a not loadable file.
  54.             if ($binaryFile instanceof MissingBinaryFile) {
  55.                 throw new NotResolvableException("Variation image not found in $path");
  56.             }
  57.             if ($filter !== static::VARIATION_ORIGINAL) {
  58.                 $variationPath $this->getFilePath($path$filter);
  59.                 $variationBinaryFile $this->ioService->loadBinaryFile($variationPath);
  60.                 $path $variationBinaryFile->uri;
  61.             } else {
  62.                 $path $binaryFile->uri;
  63.             }
  64.             return sprintf(
  65.                 '%s%s',
  66.                 $path[0] === '/' $this->getBaseUrl() : '',
  67.                 $path
  68.             );
  69.         } catch (NotFoundException $e) {
  70.             throw new NotResolvableException("Variation image not found in $path"0$e);
  71.         }
  72.     }
  73.     /**
  74.      * Stores image alias in the IO Repository.
  75.      * A temporary file is created to dump the filtered image and is used as basis for creation in the IO Repository.
  76.      *
  77.      * {@inheritdoc}
  78.      */
  79.     public function store(BinaryInterface $binary$path$filter)
  80.     {
  81.         $tmpFile tmpfile();
  82.         fwrite($tmpFile$binary->getContent());
  83.         $tmpMetadata stream_get_meta_data($tmpFile);
  84.         $binaryCreateStruct $this->ioService->newBinaryCreateStructFromLocalFile($tmpMetadata['uri']);
  85.         $binaryCreateStruct->id $this->getFilePath($path$filter);
  86.         $this->ioService->createBinaryFile($binaryCreateStruct);
  87.         fclose($tmpFile);
  88.     }
  89.     /**
  90.      * @param string[] $paths The paths where the original files are expected to be.
  91.      * @param string[] $filters The imagine filters in effect.
  92.      */
  93.     public function remove(array $paths, array $filters)
  94.     {
  95.         if (empty($filters)) {
  96.             $filters array_keys($this->filterConfiguration->all());
  97.         }
  98.         if (empty($paths)) {
  99.             $this->variationPurger->purge($filters);
  100.         }
  101.         foreach ($paths as $path) {
  102.             foreach ($filters as $filter) {
  103.                 $filteredImagePath $this->getFilePath($path$filter);
  104.                 if (!$this->ioService->exists($filteredImagePath)) {
  105.                     continue;
  106.                 }
  107.                 $binaryFile $this->ioService->loadBinaryFile($filteredImagePath);
  108.                 $this->ioService->deleteBinaryFile($binaryFile);
  109.             }
  110.         }
  111.     }
  112.     /**
  113.      * Returns path for filtered image from original path, using the VariationPathGenerator.
  114.      *
  115.      * @param string $path
  116.      * @param string $filter
  117.      *
  118.      * @return string
  119.      */
  120.     public function getFilePath($path$filter)
  121.     {
  122.         return $this->variationPathGenerator->getVariationPath($path$filter);
  123.     }
  124.     /**
  125.      * Returns base URL, with scheme, host and port, for current request context.
  126.      * If no delivery URL is configured for current SiteAccess, will return base URL from current RequestContext.
  127.      *
  128.      * @return string
  129.      */
  130.     protected function getBaseUrl()
  131.     {
  132.         $port '';
  133.         if ($this->requestContext->getScheme() === 'https' && $this->requestContext->getHttpsPort() != 443) {
  134.             $port ":{$this->requestContext->getHttpsPort()}";
  135.         }
  136.         if ($this->requestContext->getScheme() === 'http' && $this->requestContext->getHttpPort() != 80) {
  137.             $port ":{$this->requestContext->getHttpPort()}";
  138.         }
  139.         $baseUrl $this->requestContext->getBaseUrl();
  140.         if (substr($this->requestContext->getBaseUrl(), -4) === '.php') {
  141.             $baseUrl pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME);
  142.         }
  143.         $baseUrl rtrim($baseUrl'/\\');
  144.         return sprintf(
  145.             '%s://%s%s%s',
  146.             $this->requestContext->getScheme(),
  147.             $this->requestContext->getHost(),
  148.             $port,
  149.             $baseUrl
  150.         );
  151.     }
  152. }