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

Legacy.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Image\IO;
8
9 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
10 use eZ\Publish\Core\IO\IOServiceInterface;
11 use eZ\Publish\Core\IO\Values\BinaryFile;
12 use eZ\Publish\Core\IO\Values\BinaryFileCreateStruct;
13 use eZ\Publish\Core\IO\Values\MissingBinaryFile;
14
29 class Legacy implements IOServiceInterface
30 {
36 private $publishedIOService;
37
43 private $draftIOService;
44
51 private $publishedPrefix;
52
59 private $draftPrefix;
60
62 private $optionsProvider;
63
75 public function __construct(IOServiceInterface $publishedIOService, IOServiceInterface $draftIOService, OptionsProvider $optionsProvider)
76 {
77 $this->publishedIOService = $publishedIOService;
78 $this->draftIOService = $draftIOService;
79 $this->optionsProvider = $optionsProvider;
80 $this->setPrefixes();
81 }
82
86 public function setPrefix($prefix)
87 {
88 $this->publishedIOService->setPrefix($prefix);
89 $this->draftIOService->setPrefix($prefix);
90 }
91
95 private function setPrefixes()
96 {
97 $pathArray = [$this->optionsProvider->getVarDir()];
98
99 // The storage dir itself might be null
100 if ($storageDir = $this->optionsProvider->getStorageDir()) {
101 $pathArray[] = $storageDir;
102 }
103
104 $this->draftPrefix = implode('/', array_merge($pathArray, [$this->optionsProvider->getDraftImagesDir()]));
105 $this->publishedPrefix = implode('/', array_merge($pathArray, [$this->optionsProvider->getPublishedImagesDir()]));
106 }
107
108 public function getExternalPath($internalId)
109 {
110 return $this->publishedIOService->getExternalPath($internalId);
111 }
112
113 public function newBinaryCreateStructFromLocalFile($localFile)
114 {
115 return $this->publishedIOService->newBinaryCreateStructFromLocalFile($localFile);
116 }
117
118 public function exists($binaryFileId)
119 {
120 return $this->publishedIOService->exists($binaryFileId);
121 }
122
123 public function getInternalPath($externalId)
124 {
125 return $this->publishedIOService->getInternalPath($externalId);
126 }
127
128 public function loadBinaryFile($binaryFileId)
129 {
130 // If the id is an internal (absolute) path to a draft image, use the draft service to get external path & load
131 if ($this->isDraftImagePath($binaryFileId)) {
132 return $this->draftIOService->loadBinaryFile($this->draftIOService->getExternalPath($binaryFileId));
133 }
134
135 // If the id is an internal path (absolute) to a published image, replace with the internal path
136 if ($this->isPublishedImagePath($binaryFileId)) {
137 $binaryFileId = $this->publishedIOService->getExternalPath($binaryFileId);
138 }
139
140 try {
141 $image = $this->publishedIOService->loadBinaryFile($binaryFileId);
142
143 if ($image instanceof MissingBinaryFile) {
144 throw new InvalidArgumentException('binaryFileId', sprintf("Can't find file with id %s", $binaryFileId));
145 }
146
147 return $image;
148 } catch (InvalidArgumentException $prefixException) {
149 // InvalidArgumentException means that the prefix didn't match, NotFound can pass through
150 try {
151 return $this->draftIOService->loadBinaryFile($binaryFileId);
152 } catch (InvalidArgumentException $e) {
153 throw $prefixException;
154 }
155 }
156 }
157
161 public function loadBinaryFileByUri($binaryFileUri)
162 {
163 try {
164 $image = $this->publishedIOService->loadBinaryFileByUri($binaryFileUri);
165
166 if ($image instanceof MissingBinaryFile) {
167 throw new InvalidArgumentException('binaryFileUri', sprintf("Can't find file with url %s", $binaryFileUri));
168 }
169
170 return $image;
171 } catch (InvalidArgumentException $prefixException) {
172 // InvalidArgumentException means that the prefix didn't match, NotFound can pass through
173 try {
174 return $this->draftIOService->loadBinaryFileByUri($binaryFileUri);
175 } catch (InvalidArgumentException $e) {
176 throw $prefixException;
177 }
178 }
179 }
180
181 public function getFileContents(BinaryFile $binaryFile)
182 {
183 if ($this->draftIOService->exists($binaryFile->id)) {
184 return $this->draftIOService->getFileContents($binaryFile);
185 }
186
187 return $this->publishedIOService->getFileContents($binaryFile);
188 }
189
190 public function createBinaryFile(BinaryFileCreateStruct $binaryFileCreateStruct)
191 {
192 return $this->publishedIOService->createBinaryFile($binaryFileCreateStruct);
193 }
194
195 public function getUri($binaryFileId)
196 {
197 return $this->publishedIOService->getUri($binaryFileId);
198 }
199
200 public function getMimeType($binaryFileId)
201 {
202 // If the id is an internal (absolute) path to a draft image, use the draft service to get external path & load
203 if ($this->isDraftImagePath($binaryFileId)) {
204 return $this->draftIOService->getMimeType($this->draftIOService->getExternalPath($binaryFileId));
205 }
206
207 // If the id is an internal path (absolute) to a published image, replace with the internal path
208 if ($this->isPublishedImagePath($binaryFileId)) {
209 $binaryFileId = $this->publishedIOService->getExternalPath($binaryFileId);
210 }
211
212 if ($this->draftIOService->exists($binaryFileId)) {
213 return $this->draftIOService->getMimeType($binaryFileId);
214 }
215
216 return $this->publishedIOService->getMimeType($binaryFileId);
217 }
218
219 public function getFileInputStream(BinaryFile $binaryFile)
220 {
221 return $this->publishedIOService->getFileInputStream($binaryFile);
222 }
223
224 public function deleteBinaryFile(BinaryFile $binaryFile)
225 {
226 $this->publishedIOService->deleteBinaryFile($binaryFile);
227 }
228
234 public function deleteDirectory($path)
235 {
236 $this->publishedIOService->deleteDirectory($path);
237 }
238
239 public function newBinaryCreateStructFromUploadedFile(array $uploadedFile)
240 {
241 return $this->publishedIOService->newBinaryCreateStructFromUploadedFile($uploadedFile);
242 }
243
251 protected function isPublishedImagePath($internalPath)
252 {
253 return strpos($internalPath, $this->publishedPrefix) === 0;
254 }
255
263 protected function isDraftImagePath($internalPath)
264 {
265 return strpos($internalPath, $this->draftPrefix) === 0;
266 }
267 }
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\FieldType\Image\IO\Legacy
Legacy Image IOService.
Definition: Legacy.php:30
eZ\Publish\Core\FieldType\Image\IO\Legacy\loadBinaryFileByUri
loadBinaryFileByUri($binaryFileUri)
Since both services should use the same uri, we can use any of them to GET the URI.
Definition: Legacy.php:161
eZ\Publish\Core\FieldType\Image\IO\Legacy\isPublishedImagePath
isPublishedImagePath($internalPath)
Checks if $internalPath is a published image path.
Definition: Legacy.php:251
eZ\Publish\Core\FieldType\Image\IO\Legacy\getInternalPath
getInternalPath($externalId)
Returns the internal, handler level path to $externalPath.
Definition: Legacy.php:123
eZ\Publish\Core\FieldType\Image\IO\Legacy\$publishedPrefix
$publishedPrefix
Definition: Legacy.php:51
eZ\Publish\Core\FieldType\Image\IO\Legacy\getFileContents
getFileContents(BinaryFile $binaryFile)
Returns the content of the binary file.
Definition: Legacy.php:181
eZ\Publish\Core\FieldType\Image\IO\Legacy\isDraftImagePath
isDraftImagePath($internalPath)
Checks if $internalPath is a published image path.
Definition: Legacy.php:263
eZ\Publish\Core\FieldType\Image\IO\Legacy\exists
exists($binaryFileId)
Checks if a Binary File with $binaryFileId exists.
Definition: Legacy.php:118
eZ\Publish\Core\FieldType\Image\IO\Legacy\getFileInputStream
getFileInputStream(BinaryFile $binaryFile)
Returns a read (mode: rb) file resource to the binary file identified by $path.
Definition: Legacy.php:219
eZ\Publish\Core\FieldType\Image\IO\Legacy\newBinaryCreateStructFromUploadedFile
newBinaryCreateStructFromUploadedFile(array $uploadedFile)
Creates a BinaryFileCreateStruct object from the uploaded file $uploadedFile.
Definition: Legacy.php:239
eZ\Publish\Core\FieldType\Image\IO\Legacy\deleteDirectory
deleteDirectory($path)
Deletes a directory.
Definition: Legacy.php:234
eZ\Publish\Core\FieldType\Image\IO\Legacy\$draftIOService
$draftIOService
Definition: Legacy.php:43
eZ\Publish\Core\FieldType\Image\IO\Legacy\getUri
getUri($binaryFileId)
Returns the public HTTP uri for $binaryFileId.
Definition: Legacy.php:195
eZ\Publish\Core\FieldType\Image\IO\Legacy\$draftPrefix
$draftPrefix
Definition: Legacy.php:59
eZ\Publish\Core\FieldType\Image\IO\Legacy\__construct
__construct(IOServiceInterface $publishedIOService, IOServiceInterface $draftIOService, OptionsProvider $optionsProvider)
Definition: Legacy.php:75
eZ\Publish\Core\FieldType\Image\IO\Legacy\$publishedIOService
$publishedIOService
Definition: Legacy.php:36
eZ\Publish\Core\FieldType\Image\IO\Legacy\newBinaryCreateStructFromLocalFile
newBinaryCreateStructFromLocalFile($localFile)
Creates a BinaryFileCreateStruct object from $localFile.
Definition: Legacy.php:113
eZ\Publish\Core\FieldType\Image\IO\Legacy\createBinaryFile
createBinaryFile(BinaryFileCreateStruct $binaryFileCreateStruct)
Creates a binary file in the repository.
Definition: Legacy.php:190
eZ\Publish\Core\FieldType\Image\IO\Legacy\$optionsProvider
$optionsProvider
Definition: Legacy.php:62
eZ\Publish\Core\FieldType\Image\IO\Legacy\getMimeType
getMimeType($binaryFileId)
Gets the mime-type of the BinaryFile.
Definition: Legacy.php:200
eZ\Publish\Core\FieldType\Image\IO\Legacy\deleteBinaryFile
deleteBinaryFile(BinaryFile $binaryFile)
Deletes the BinaryFile with $id.
Definition: Legacy.php:224
eZ\Publish\Core\FieldType\Image\IO\Legacy\setPrefixes
setPrefixes()
Computes the paths to published & draft images path using the options from the provider.
Definition: Legacy.php:95
eZ\Publish\Core\FieldType\Image\IO\Legacy\setPrefix
setPrefix($prefix)
Sets the IOService prefix.
Definition: Legacy.php:86
eZ\Publish\Core\FieldType\Image\IO\Legacy\loadBinaryFile
loadBinaryFile($binaryFileId)
Loads the binary file with $id.
Definition: Legacy.php:128
eZ\Publish\Core\FieldType\Image\IO\Legacy\getExternalPath
getExternalPath($internalId)
Returns the external path to $internalPath.
Definition: Legacy.php:108
eZ\Publish\Core\FieldType\Image\IO\OptionsProvider
Definition: OptionsProvider.php:10
eZ\Publish\Core\IO\Values\BinaryFileCreateStruct
Create struct for BinaryFile objects.
Definition: Core/IO/Values/BinaryFileCreateStruct.php:15
eZ\Publish\Core\IO\Values\BinaryFile
Definition: Core/IO/Values/BinaryFile.php:22
eZ\Publish\Core\IO\Values\MissingBinaryFile
Override of BinaryFile that indicates a non existing file.
Definition: MissingBinaryFile.php:15
eZ\Publish\Core\IO\IOServiceInterface
Interface for Input/Output handling of binary files.
Definition: IOServiceInterface.php:16
eZ\Publish\Core\FieldType\Image\IO
Definition: Legacy.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.