Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

IORepositoryResolver.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Imagine;
8
9 use eZ\Publish\Core\Base\Exceptions\NotFoundException;
10 use eZ\Publish\Core\IO\IOServiceInterface;
11 use eZ\Publish\Core\IO\Values\MissingBinaryFile;
12 use eZ\Publish\SPI\Variation\VariationPurger;
13 use Liip\ImagineBundle\Binary\BinaryInterface;
14 use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
15 use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;
16 use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
17 use Symfony\Component\Routing\RequestContext;
18
22 class IORepositoryResolver implements ResolverInterface
23 {
24 const VARIATION_ORIGINAL = 'original';
25
27 private $ioService;
28
30 private $requestContext;
31
33 private $filterConfiguration;
35 private $variationPurger;
36
38 private $variationPathGenerator;
39
40 public function __construct(
41 IOServiceInterface $ioService,
42 RequestContext $requestContext,
43 FilterConfiguration $filterConfiguration,
44 VariationPurger $variationPurger,
45 VariationPathGenerator $variationPathGenerator
46 ) {
47 $this->ioService = $ioService;
48 $this->requestContext = $requestContext;
49 $this->filterConfiguration = $filterConfiguration;
50 $this->variationPurger = $variationPurger;
51 $this->variationPathGenerator = $variationPathGenerator;
52 }
53
54 public function isStored($path, $filter)
55 {
56 return $this->ioService->exists($this->getFilePath($path, $filter));
57 }
58
59 public function resolve($path, $filter)
60 {
61 try {
62 $binaryFile = $this->ioService->loadBinaryFile($path);
63
64 // Treat a MissingBinaryFile as a not loadable file.
65 if ($binaryFile instanceof MissingBinaryFile) {
66 throw new NotResolvableException("Variation image not found in $path");
67 }
68
69 if ($filter !== static::VARIATION_ORIGINAL) {
70 $variationPath = $this->getFilePath($path, $filter);
71 $variationBinaryFile = $this->ioService->loadBinaryFile($variationPath);
72 $path = $variationBinaryFile->uri;
73 } else {
74 $path = $binaryFile->uri;
75 }
76
77 return sprintf(
78 '%s%s',
79 $path[0] === '/' ? $this->getBaseUrl() : '',
80 $path
81 );
82 } catch (NotFoundException $e) {
83 throw new NotResolvableException("Variation image not found in $path", 0, $e);
84 }
85 }
86
93 public function store(BinaryInterface $binary, $path, $filter)
94 {
95 $tmpFile = tmpfile();
96 fwrite($tmpFile, $binary->getContent());
97 $tmpMetadata = stream_get_meta_data($tmpFile);
98
99 $binaryCreateStruct = $this->ioService->newBinaryCreateStructFromLocalFile($tmpMetadata['uri']);
100 $binaryCreateStruct->id = $this->getFilePath($path, $filter);
101 $this->ioService->createBinaryFile($binaryCreateStruct);
102
103 fclose($tmpFile);
104 }
105
110 public function remove(array $paths, array $filters)
111 {
112 if (empty($filters)) {
113 $filters = array_keys($this->filterConfiguration->all());
114 }
115
116 if (empty($paths)) {
117 $this->variationPurger->purge($filters);
118 }
119
120 foreach ($paths as $path) {
121 foreach ($filters as $filter) {
122 $filteredImagePath = $this->getFilePath($path, $filter);
123 if (!$this->ioService->exists($filteredImagePath)) {
124 continue;
125 }
126
127 $binaryFile = $this->ioService->loadBinaryFile($filteredImagePath);
128 $this->ioService->deleteBinaryFile($binaryFile);
129 }
130 }
131 }
132
141 public function getFilePath($path, $filter)
142 {
143 return $this->variationPathGenerator->getVariationPath($path, $filter);
144 }
145
152 protected function getBaseUrl()
153 {
154 $port = '';
155 if ($this->requestContext->getScheme() === 'https' && $this->requestContext->getHttpsPort() != 443) {
156 $port = ":{$this->requestContext->getHttpsPort()}";
157 }
158
159 if ($this->requestContext->getScheme() === 'http' && $this->requestContext->getHttpPort() != 80) {
160 $port = ":{$this->requestContext->getHttpPort()}";
161 }
162
163 $baseUrl = $this->requestContext->getBaseUrl();
164 if (substr($this->requestContext->getBaseUrl(), -4) === '.php') {
165 $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME);
166 }
167 $baseUrl = rtrim($baseUrl, '/\\');
168
169 return sprintf(
170 '%s://%s%s%s',
171 $this->requestContext->getScheme(),
172 $this->requestContext->getHost(),
173 $port,
174 $baseUrl
175 );
176 }
177 }
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver
LiipImagineBundle cache resolver using eZ IO repository.
Definition: IORepositoryResolver.php:23
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\VARIATION_ORIGINAL
const VARIATION_ORIGINAL
Definition: IORepositoryResolver.php:24
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\getBaseUrl
getBaseUrl()
Returns base URL, with scheme, host and port, for current request context.
Definition: IORepositoryResolver.php:152
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\$variationPathGenerator
$variationPathGenerator
Definition: IORepositoryResolver.php:38
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\isStored
isStored($path, $filter)
Definition: IORepositoryResolver.php:54
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\__construct
__construct(IOServiceInterface $ioService, RequestContext $requestContext, FilterConfiguration $filterConfiguration, VariationPurger $variationPurger, VariationPathGenerator $variationPathGenerator)
Definition: IORepositoryResolver.php:40
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\$filterConfiguration
$filterConfiguration
Definition: IORepositoryResolver.php:33
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\$ioService
$ioService
Definition: IORepositoryResolver.php:27
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\$requestContext
$requestContext
Definition: IORepositoryResolver.php:30
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\resolve
resolve($path, $filter)
Definition: IORepositoryResolver.php:59
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\getFilePath
getFilePath($path, $filter)
Returns path for filtered image from original path, using the VariationPathGenerator.
Definition: IORepositoryResolver.php:141
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\store
store(BinaryInterface $binary, $path, $filter)
Stores image alias in the IO Repository.
Definition: IORepositoryResolver.php:93
eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver\$variationPurger
$variationPurger
Definition: IORepositoryResolver.php:35
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\Core\Base\Exceptions\NotFoundException
Not Found Exception implementation.
Definition: Core/Base/Exceptions/NotFoundException.php:21
eZ\Publish\Core\IO\Values\MissingBinaryFile
Override of BinaryFile that indicates a non existing file.
Definition: MissingBinaryFile.php:15
eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator
Generates the path to variations of original images.
Definition: VariationPathGenerator.php:13
eZ\Publish\Core\IO\IOServiceInterface
Interface for Input/Output handling of binary files.
Definition: IOServiceInterface.php:16
eZ\Publish\SPI\Variation\VariationPurger
Handles physical purging of image variations from storage.
Definition: VariationPurger.php:13
eZ\Bundle\EzPublishCoreBundle\Imagine
Definition: AliasCleaner.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.