Total Complexity | 85 |
Total Lines | 669 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ResourceFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ResourceFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ResourceFactory implements ResourceFactoryInterface, \TYPO3\CMS\Core\SingletonInterface |
||
32 | { |
||
33 | /** |
||
34 | * Gets a singleton instance of this class. |
||
35 | * |
||
36 | * @return ResourceFactory |
||
37 | */ |
||
38 | public static function getInstance() |
||
39 | { |
||
40 | return GeneralUtility::makeInstance(__CLASS__); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @var ResourceStorage[] |
||
45 | */ |
||
46 | protected $storageInstances = []; |
||
47 | |||
48 | /** |
||
49 | * @var Collection\AbstractFileCollection[] |
||
50 | */ |
||
51 | protected $collectionInstances = []; |
||
52 | |||
53 | /** |
||
54 | * @var File[] |
||
55 | */ |
||
56 | protected $fileInstances = []; |
||
57 | |||
58 | /** |
||
59 | * @var FileReference[] |
||
60 | */ |
||
61 | protected $fileReferenceInstances = []; |
||
62 | |||
63 | /** |
||
64 | * A list of the base paths of "local" driver storages. Used to make the detection of base paths easier. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $localDriverStorageCache = null; |
||
69 | |||
70 | /** |
||
71 | * @var Dispatcher |
||
72 | */ |
||
73 | protected $signalSlotDispatcher; |
||
74 | |||
75 | /** |
||
76 | * Inject signal slot dispatcher |
||
77 | * |
||
78 | * @param Dispatcher $signalSlotDispatcher an instance of the signal slot dispatcher |
||
79 | */ |
||
80 | public function __construct(Dispatcher $signalSlotDispatcher = null) |
||
81 | { |
||
82 | $this->signalSlotDispatcher = $signalSlotDispatcher ?: GeneralUtility::makeInstance(Dispatcher::class); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Creates a driver object for a specified storage object. |
||
87 | * |
||
88 | * @param string $driverIdentificationString The driver class (or identifier) to use. |
||
89 | * @param array $driverConfiguration The configuration of the storage |
||
90 | * @return Driver\DriverInterface |
||
91 | * @throws \InvalidArgumentException |
||
92 | */ |
||
93 | public function getDriverObject($driverIdentificationString, array $driverConfiguration) |
||
94 | { |
||
95 | /** @var $driverRegistry Driver\DriverRegistry */ |
||
96 | $driverRegistry = GeneralUtility::makeInstance(Driver\DriverRegistry::class); |
||
97 | $driverClass = $driverRegistry->getDriverClass($driverIdentificationString); |
||
98 | $driverObject = GeneralUtility::makeInstance($driverClass, $driverConfiguration); |
||
99 | return $driverObject; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Returns the Default Storage |
||
104 | * |
||
105 | * The Default Storage is considered to be the replacement for the fileadmin/ construct. |
||
106 | * It is automatically created with the setting fileadminDir from install tool. |
||
107 | * getDefaultStorage->getDefaultFolder() will get you fileadmin/user_upload/ in a standard |
||
108 | * TYPO3 installation. |
||
109 | * |
||
110 | * @return ResourceStorage|null |
||
111 | */ |
||
112 | public function getDefaultStorage() |
||
113 | { |
||
114 | /** @var $storageRepository StorageRepository */ |
||
115 | $storageRepository = GeneralUtility::makeInstance(StorageRepository::class); |
||
116 | |||
117 | $allStorages = $storageRepository->findAll(); |
||
118 | foreach ($allStorages as $storage) { |
||
119 | if ($storage->isDefault()) { |
||
120 | return $storage; |
||
121 | } |
||
122 | } |
||
123 | return null; |
||
124 | } |
||
125 | /** |
||
126 | * Creates an instance of the storage from given UID. The $recordData can |
||
127 | * be supplied to increase performance. |
||
128 | * |
||
129 | * @param int $uid The uid of the storage to instantiate. |
||
130 | * @param array $recordData The record row from database. |
||
131 | * @param string $fileIdentifier Identifier for a file. Used for auto-detection of a storage, but only if $uid === 0 (Local default storage) is used |
||
132 | * |
||
133 | * @throws \InvalidArgumentException |
||
134 | * @return ResourceStorage |
||
135 | */ |
||
136 | public function getStorageObject($uid, array $recordData = [], &$fileIdentifier = null) |
||
137 | { |
||
138 | if (!is_numeric($uid)) { |
||
139 | throw new \InvalidArgumentException('The UID of storage has to be numeric. UID given: "' . $uid . '"', 1314085991); |
||
140 | } |
||
141 | $uid = (int)$uid; |
||
142 | if ($uid === 0 && $fileIdentifier !== null) { |
||
143 | $uid = $this->findBestMatchingStorageByLocalPath($fileIdentifier); |
||
144 | } |
||
145 | if (empty($this->storageInstances[$uid])) { |
||
146 | $storageConfiguration = null; |
||
147 | $storageObject = null; |
||
148 | list($_, $uid, $recordData, $fileIdentifier) = $this->emitPreProcessStorageSignal($uid, $recordData, $fileIdentifier); |
||
149 | // If the built-in storage with UID=0 is requested: |
||
150 | if ($uid === 0) { |
||
151 | $recordData = [ |
||
152 | 'uid' => 0, |
||
153 | 'pid' => 0, |
||
154 | 'name' => 'Fallback Storage', |
||
155 | 'description' => 'Internal storage, mounting the main TYPO3_site directory.', |
||
156 | 'driver' => 'Local', |
||
157 | 'processingfolder' => 'typo3temp/assets/_processed_/', |
||
158 | // legacy code |
||
159 | 'configuration' => '', |
||
160 | 'is_online' => true, |
||
161 | 'is_browsable' => true, |
||
162 | 'is_public' => true, |
||
163 | 'is_writable' => true, |
||
164 | 'is_default' => false, |
||
165 | ]; |
||
166 | $storageConfiguration = [ |
||
167 | 'basePath' => '/', |
||
168 | 'pathType' => 'relative' |
||
169 | ]; |
||
170 | } elseif (count($recordData) === 0 || (int)$recordData['uid'] !== $uid) { |
||
171 | /** @var $storageRepository StorageRepository */ |
||
172 | $storageRepository = GeneralUtility::makeInstance(StorageRepository::class); |
||
173 | /** @var $storage ResourceStorage */ |
||
174 | $storageObject = $storageRepository->findByUid($uid); |
||
175 | } |
||
176 | if (!$storageObject instanceof ResourceStorage) { |
||
177 | $storageObject = $this->createStorageObject($recordData, $storageConfiguration); |
||
178 | } |
||
179 | $this->emitPostProcessStorageSignal($storageObject); |
||
180 | $this->storageInstances[$uid] = $storageObject; |
||
181 | } |
||
182 | return $this->storageInstances[$uid]; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Emits a signal before a resource storage was initialized |
||
187 | * |
||
188 | * @param int $uid |
||
189 | * @param array $recordData |
||
190 | * @param string $fileIdentifier |
||
191 | * @return mixed |
||
192 | */ |
||
193 | protected function emitPreProcessStorageSignal($uid, $recordData, $fileIdentifier) |
||
194 | { |
||
195 | return $this->signalSlotDispatcher->dispatch(\TYPO3\CMS\Core\Resource\ResourceFactory::class, self::SIGNAL_PreProcessStorage, [$this, $uid, $recordData, $fileIdentifier]); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Emits a signal after a resource storage was initialized |
||
200 | * |
||
201 | * @param ResourceStorage $storageObject |
||
202 | */ |
||
203 | protected function emitPostProcessStorageSignal(ResourceStorage $storageObject) |
||
204 | { |
||
205 | $this->signalSlotDispatcher->dispatch(self::class, self::SIGNAL_PostProcessStorage, [$this, $storageObject]); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Checks whether a file resides within a real storage in local file system. |
||
210 | * If no match is found, uid 0 is returned which is a fallback storage pointing to PATH_site. |
||
211 | * |
||
212 | * The file identifier is adapted accordingly to match the new storage's base path. |
||
213 | * |
||
214 | * @param string $localPath |
||
215 | * |
||
216 | * @return int |
||
217 | */ |
||
218 | protected function findBestMatchingStorageByLocalPath(&$localPath) |
||
219 | { |
||
220 | if ($this->localDriverStorageCache === null) { |
||
221 | $this->initializeLocalStorageCache(); |
||
222 | } |
||
223 | |||
224 | $bestMatchStorageUid = 0; |
||
225 | $bestMatchLength = 0; |
||
226 | foreach ($this->localDriverStorageCache as $storageUid => $basePath) { |
||
227 | $matchLength = strlen(PathUtility::getCommonPrefix([$basePath, $localPath])); |
||
228 | $basePathLength = strlen($basePath); |
||
229 | |||
230 | if ($matchLength >= $basePathLength && $matchLength > $bestMatchLength) { |
||
231 | $bestMatchStorageUid = (int)$storageUid; |
||
232 | $bestMatchLength = $matchLength; |
||
233 | } |
||
234 | } |
||
235 | if ($bestMatchStorageUid !== 0) { |
||
236 | $localPath = substr($localPath, $bestMatchLength); |
||
237 | } |
||
238 | return $bestMatchStorageUid; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Creates an array mapping all uids to the basePath of storages using the "local" driver. |
||
243 | */ |
||
244 | protected function initializeLocalStorageCache() |
||
245 | { |
||
246 | /** @var $storageRepository StorageRepository */ |
||
247 | $storageRepository = GeneralUtility::makeInstance(StorageRepository::class); |
||
248 | /** @var $storageObjects ResourceStorage[] */ |
||
249 | $storageObjects = $storageRepository->findByStorageType('Local'); |
||
250 | |||
251 | $storageCache = []; |
||
252 | foreach ($storageObjects as $localStorage) { |
||
253 | $configuration = $localStorage->getConfiguration(); |
||
254 | $storageCache[$localStorage->getUid()] = $configuration['basePath']; |
||
255 | } |
||
256 | $this->localDriverStorageCache = $storageCache; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Converts a flexform data string to a flat array with key value pairs |
||
261 | * |
||
262 | * @param string $flexFormData |
||
263 | * @return array Array with key => value pairs of the field data in the FlexForm |
||
264 | */ |
||
265 | public function convertFlexFormDataToConfigurationArray($flexFormData) |
||
266 | { |
||
267 | $configuration = []; |
||
268 | if ($flexFormData) { |
||
269 | $flexFormService = GeneralUtility::makeInstance(FlexFormService::class); |
||
270 | $configuration = $flexFormService->convertFlexFormContentToArray($flexFormData); |
||
271 | } |
||
272 | return $configuration; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Creates an instance of the collection from given UID. The $recordData can be supplied to increase performance. |
||
277 | * |
||
278 | * @param int $uid The uid of the collection to instantiate. |
||
279 | * @param array $recordData The record row from database. |
||
280 | * |
||
281 | * @throws \InvalidArgumentException |
||
282 | * @return Collection\AbstractFileCollection |
||
283 | */ |
||
284 | public function getCollectionObject($uid, array $recordData = []) |
||
285 | { |
||
286 | if (!is_numeric($uid)) { |
||
287 | throw new \InvalidArgumentException('The UID of collection has to be numeric. UID given: "' . $uid . '"', 1314085999); |
||
288 | } |
||
289 | if (!$this->collectionInstances[$uid]) { |
||
290 | // Get mount data if not already supplied as argument to this function |
||
291 | if (empty($recordData) || $recordData['uid'] !== $uid) { |
||
292 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_collection'); |
||
293 | $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
294 | $recordData = $queryBuilder->select('*') |
||
295 | ->from('sys_file_collection') |
||
296 | ->where( |
||
297 | $queryBuilder->expr()->eq( |
||
298 | 'uid', |
||
299 | $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT) |
||
300 | ) |
||
301 | ) |
||
302 | ->execute() |
||
303 | ->fetch(); |
||
304 | if (empty($recordData)) { |
||
305 | throw new \InvalidArgumentException('No collection found for given UID: "' . $uid . '"', 1314085992); |
||
306 | } |
||
307 | } |
||
308 | $collectionObject = $this->createCollectionObject($recordData); |
||
309 | $this->collectionInstances[$uid] = $collectionObject; |
||
310 | } |
||
311 | return $this->collectionInstances[$uid]; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Creates a collection object. |
||
316 | * |
||
317 | * @param array $collectionData The database row of the sys_file_collection record. |
||
318 | * @return Collection\AbstractFileCollection |
||
319 | */ |
||
320 | public function createCollectionObject(array $collectionData) |
||
321 | { |
||
322 | /** @var $registry Collection\FileCollectionRegistry */ |
||
323 | $registry = GeneralUtility::makeInstance(Collection\FileCollectionRegistry::class); |
||
324 | |||
325 | /** @var \TYPO3\CMS\Core\Collection\AbstractRecordCollection $class */ |
||
326 | $class = $registry->getFileCollectionClass($collectionData['type']); |
||
327 | |||
328 | return $class::create($collectionData); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Creates a storage object from a storage database row. |
||
333 | * |
||
334 | * @param array $storageRecord |
||
335 | * @param array $storageConfiguration Storage configuration (if given, this won't be extracted from the FlexForm value but the supplied array used instead) |
||
336 | * @return ResourceStorage |
||
337 | */ |
||
338 | public function createStorageObject(array $storageRecord, array $storageConfiguration = null) |
||
339 | { |
||
340 | if (!$storageConfiguration) { |
||
341 | $storageConfiguration = $this->convertFlexFormDataToConfigurationArray($storageRecord['configuration']); |
||
342 | } |
||
343 | $driverType = $storageRecord['driver']; |
||
344 | $driverObject = $this->getDriverObject($driverType, $storageConfiguration); |
||
345 | return GeneralUtility::makeInstance(ResourceStorage::class, $driverObject, $storageRecord); |
||
|
|||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Creates a folder to directly access (a part of) a storage. |
||
350 | * |
||
351 | * @param ResourceStorage $storage The storage the folder belongs to |
||
352 | * @param string $identifier The path to the folder. Might also be a simple unique string, depending on the storage driver. |
||
353 | * @param string $name The name of the folder (e.g. the folder name) |
||
354 | * @return Folder |
||
355 | */ |
||
356 | public function createFolderObject(ResourceStorage $storage, $identifier, $name) |
||
357 | { |
||
358 | return GeneralUtility::makeInstance(Folder::class, $storage, $identifier, $name); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Creates an instance of the file given UID. The $fileData can be supplied |
||
363 | * to increase performance. |
||
364 | * |
||
365 | * @param int $uid The uid of the file to instantiate. |
||
366 | * @param array $fileData The record row from database. |
||
367 | * |
||
368 | * @throws \InvalidArgumentException |
||
369 | * @throws Exception\FileDoesNotExistException |
||
370 | * @return File |
||
371 | */ |
||
372 | public function getFileObject($uid, array $fileData = []) |
||
373 | { |
||
374 | if (!is_numeric($uid)) { |
||
375 | throw new \InvalidArgumentException('The UID of file has to be numeric. UID given: "' . $uid . '"', 1300096564); |
||
376 | } |
||
377 | if (empty($this->fileInstances[$uid])) { |
||
378 | // Fetches data in case $fileData is empty |
||
379 | if (empty($fileData)) { |
||
380 | $fileData = $this->getFileIndexRepository()->findOneByUid($uid); |
||
381 | if ($fileData === false) { |
||
382 | throw new Exception\FileDoesNotExistException('No file found for given UID: ' . $uid, 1317178604); |
||
383 | } |
||
384 | } |
||
385 | $this->fileInstances[$uid] = $this->createFileObject($fileData); |
||
386 | } |
||
387 | return $this->fileInstances[$uid]; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Gets an file object from an identifier [storage]:[fileId] |
||
392 | * |
||
393 | * @param string $identifier |
||
394 | * @return File |
||
395 | * @throws \InvalidArgumentException |
||
396 | */ |
||
397 | public function getFileObjectFromCombinedIdentifier($identifier) |
||
398 | { |
||
399 | if (!isset($identifier) || !is_string($identifier) || $identifier === '') { |
||
400 | throw new \InvalidArgumentException('Invalid file identifier given. It must be of type string and not empty. "' . gettype($identifier) . '" given.', 1401732564); |
||
401 | } |
||
402 | $parts = GeneralUtility::trimExplode(':', $identifier); |
||
403 | if (count($parts) === 2) { |
||
404 | $storageUid = $parts[0]; |
||
405 | $fileIdentifier = $parts[1]; |
||
406 | } else { |
||
407 | // We only got a path: Go into backwards compatibility mode and |
||
408 | // use virtual Storage (uid=0) |
||
409 | $storageUid = 0; |
||
410 | $fileIdentifier = $parts[0]; |
||
411 | } |
||
412 | |||
413 | // please note that getStorageObject() might modify $fileIdentifier when |
||
414 | // auto-detecting the best-matching storage to use |
||
415 | return $this->getFileObjectByStorageAndIdentifier($storageUid, $fileIdentifier); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Gets an file object from storage by file identifier |
||
420 | * If the file is outside of the process folder it gets indexed and returned as file object afterwards |
||
421 | * If the file is within processing folder the file object will be directly returned |
||
422 | * |
||
423 | * @param int $storageUid |
||
424 | * @param string $fileIdentifier |
||
425 | * @return File|ProcessedFile|null |
||
426 | */ |
||
427 | public function getFileObjectByStorageAndIdentifier($storageUid, &$fileIdentifier) |
||
428 | { |
||
429 | $storage = $this->getStorageObject($storageUid, [], $fileIdentifier); |
||
430 | if (!$storage->isWithinProcessingFolder($fileIdentifier)) { |
||
431 | $fileData = $this->getFileIndexRepository()->findOneByStorageUidAndIdentifier($storage->getUid(), $fileIdentifier); |
||
432 | if ($fileData === false) { |
||
433 | $fileObject = $this->getIndexer($storage)->createIndexEntry($fileIdentifier); |
||
434 | } else { |
||
435 | $fileObject = $this->getFileObject($fileData['uid'], $fileData); |
||
436 | } |
||
437 | } else { |
||
438 | $fileObject = $this->getProcessedFileRepository()->findByStorageAndIdentifier($storage, $fileIdentifier); |
||
439 | } |
||
440 | |||
441 | return $fileObject; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Bulk function, can be used for anything to get a file or folder |
||
446 | * |
||
447 | * 1. It's a UID |
||
448 | * 2. It's a combined identifier |
||
449 | * 3. It's just a path/filename (coming from the oldstyle/backwards compatibility) |
||
450 | * |
||
451 | * Files, previously laid on fileadmin/ or something, will be "mapped" to the storage the file is |
||
452 | * in now. Files like typo3temp/ or typo3conf/ will be moved to the first writable storage |
||
453 | * in its processing folder |
||
454 | * |
||
455 | * $input could be |
||
456 | * - "2:myfolder/myfile.jpg" (combined identifier) |
||
457 | * - "23" (file UID) |
||
458 | * - "uploads/myfile.png" (backwards-compatibility, storage "0") |
||
459 | * - "file:23" |
||
460 | * |
||
461 | * @param string $input |
||
462 | * @return File|Folder|null |
||
463 | */ |
||
464 | public function retrieveFileOrFolderObject($input) |
||
465 | { |
||
466 | // Remove PATH_site because absolute paths under Windows systems contain ':' |
||
467 | // This is done in all considered sub functions anyway |
||
468 | $input = str_replace(PATH_site, '', $input); |
||
469 | |||
470 | if (GeneralUtility::isFirstPartOfStr($input, 'file:')) { |
||
471 | $input = substr($input, 5); |
||
472 | return $this->retrieveFileOrFolderObject($input); |
||
473 | } |
||
474 | if (MathUtility::canBeInterpretedAsInteger($input)) { |
||
475 | return $this->getFileObject($input); |
||
476 | } |
||
477 | if (strpos($input, ':') > 0) { |
||
478 | list($prefix) = explode(':', $input); |
||
479 | if (MathUtility::canBeInterpretedAsInteger($prefix)) { |
||
480 | // path or folder in a valid storageUID |
||
481 | return $this->getObjectFromCombinedIdentifier($input); |
||
482 | } |
||
483 | if ($prefix === 'EXT') { |
||
484 | $input = GeneralUtility::getFileAbsFileName($input); |
||
485 | if (empty($input)) { |
||
486 | return null; |
||
487 | } |
||
488 | |||
489 | $input = PathUtility::getRelativePath(PATH_site, PathUtility::dirname($input)) . PathUtility::basename($input); |
||
490 | return $this->getFileObjectFromCombinedIdentifier($input); |
||
491 | } |
||
492 | return null; |
||
493 | } |
||
494 | // this is a backwards-compatible way to access "0-storage" files or folders |
||
495 | // eliminate double slashes, /./ and /../ |
||
496 | $input = PathUtility::getCanonicalPath(ltrim($input, '/')); |
||
497 | if (@is_file(PATH_site . $input)) { |
||
498 | // only the local file |
||
499 | return $this->getFileObjectFromCombinedIdentifier($input); |
||
500 | } |
||
501 | // only the local path |
||
502 | return $this->getFolderObjectFromCombinedIdentifier($input); |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Gets a folder object from an identifier [storage]:[fileId] |
||
507 | * |
||
508 | * @TODO check naming, inserted by SteffenR while working on filelist |
||
509 | * @param string $identifier |
||
510 | * @return Folder |
||
511 | */ |
||
512 | public function getFolderObjectFromCombinedIdentifier($identifier) |
||
513 | { |
||
514 | $parts = GeneralUtility::trimExplode(':', $identifier); |
||
515 | if (count($parts) === 2) { |
||
516 | $storageUid = $parts[0]; |
||
517 | $folderIdentifier = $parts[1]; |
||
518 | } else { |
||
519 | // We only got a path: Go into backwards compatibility mode and |
||
520 | // use virtual Storage (uid=0) |
||
521 | $storageUid = 0; |
||
522 | |||
523 | // please note that getStorageObject() might modify $folderIdentifier when |
||
524 | // auto-detecting the best-matching storage to use |
||
525 | $folderIdentifier = $parts[0]; |
||
526 | // make sure to not use an absolute path, and remove PATH_site if it is prepended |
||
527 | if (GeneralUtility::isFirstPartOfStr($folderIdentifier, PATH_site)) { |
||
528 | $folderIdentifier = PathUtility::stripPathSitePrefix($parts[0]); |
||
529 | } |
||
530 | } |
||
531 | return $this->getStorageObject($storageUid, [], $folderIdentifier)->getFolder($folderIdentifier); |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * Gets a storage object from a combined identifier |
||
536 | * |
||
537 | * @param string $identifier An identifier of the form [storage uid]:[object identifier] |
||
538 | * @return ResourceStorage |
||
539 | */ |
||
540 | public function getStorageObjectFromCombinedIdentifier($identifier) |
||
541 | { |
||
542 | $parts = GeneralUtility::trimExplode(':', $identifier); |
||
543 | $storageUid = count($parts) === 2 ? $parts[0] : null; |
||
544 | return $this->getStorageObject($storageUid); |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * Gets a file or folder object. |
||
549 | * |
||
550 | * @param string $identifier |
||
551 | * |
||
552 | * @throws Exception\ResourceDoesNotExistException |
||
553 | * @return FileInterface|Folder |
||
554 | */ |
||
555 | public function getObjectFromCombinedIdentifier($identifier) |
||
556 | { |
||
557 | list($storageId, $objectIdentifier) = GeneralUtility::trimExplode(':', $identifier); |
||
558 | $storage = $this->getStorageObject($storageId); |
||
559 | if ($storage->hasFile($objectIdentifier)) { |
||
560 | return $storage->getFile($objectIdentifier); |
||
561 | } |
||
562 | if ($storage->hasFolder($objectIdentifier)) { |
||
563 | return $storage->getFolder($objectIdentifier); |
||
564 | } |
||
565 | throw new Exception\ResourceDoesNotExistException('Object with identifier "' . $identifier . '" does not exist in storage', 1329647780); |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * Creates a file object from an array of file data. Requires a database |
||
570 | * row to be fetched. |
||
571 | * |
||
572 | * @param array $fileData |
||
573 | * @param ResourceStorage $storage |
||
574 | * @return File |
||
575 | */ |
||
576 | public function createFileObject(array $fileData, ResourceStorage $storage = null) |
||
577 | { |
||
578 | /** @var File $fileObject */ |
||
579 | if (array_key_exists('storage', $fileData) && MathUtility::canBeInterpretedAsInteger($fileData['storage'])) { |
||
580 | $storageObject = $this->getStorageObject((int)$fileData['storage']); |
||
581 | } elseif ($storage !== null) { |
||
582 | $storageObject = $storage; |
||
583 | $fileData['storage'] = $storage->getUid(); |
||
584 | } else { |
||
585 | throw new \RuntimeException('A file needs to reside in a Storage', 1381570997); |
||
586 | } |
||
587 | $fileObject = GeneralUtility::makeInstance(File::class, $fileData, $storageObject); |
||
588 | return $fileObject; |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Creates an instance of a FileReference object. The $fileReferenceData can |
||
593 | * be supplied to increase performance. |
||
594 | * |
||
595 | * @param int $uid The uid of the file usage (sys_file_reference) to instantiate. |
||
596 | * @param array $fileReferenceData The record row from database. |
||
597 | * @param bool $raw Whether to get raw results without performing overlays |
||
598 | * @return FileReference |
||
599 | * @throws \InvalidArgumentException |
||
600 | * @throws Exception\ResourceDoesNotExistException |
||
601 | */ |
||
602 | public function getFileReferenceObject($uid, array $fileReferenceData = [], $raw = false) |
||
603 | { |
||
604 | if (!is_numeric($uid)) { |
||
605 | throw new \InvalidArgumentException( |
||
606 | 'The reference UID for the file (sys_file_reference) has to be numeric. UID given: "' . $uid . '"', |
||
607 | 1300086584 |
||
608 | ); |
||
609 | } |
||
610 | if (!$this->fileReferenceInstances[$uid]) { |
||
611 | // Fetches data in case $fileData is empty |
||
612 | if (empty($fileReferenceData)) { |
||
613 | $fileReferenceData = $this->getFileReferenceData($uid, $raw); |
||
614 | if (!is_array($fileReferenceData)) { |
||
615 | throw new Exception\ResourceDoesNotExistException( |
||
616 | 'No file reference (sys_file_reference) was found for given UID: "' . $uid . '"', |
||
617 | 1317178794 |
||
618 | ); |
||
619 | } |
||
620 | } |
||
621 | $this->fileReferenceInstances[$uid] = $this->createFileReferenceObject($fileReferenceData); |
||
622 | } |
||
623 | return $this->fileReferenceInstances[$uid]; |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * Creates a file usage object from an array of fileReference data |
||
628 | * from sys_file_reference table. |
||
629 | * Requires a database row to be already fetched and present. |
||
630 | * |
||
631 | * @param array $fileReferenceData |
||
632 | * @return FileReference |
||
633 | */ |
||
634 | public function createFileReferenceObject(array $fileReferenceData) |
||
635 | { |
||
636 | /** @var FileReference $fileReferenceObject */ |
||
637 | $fileReferenceObject = GeneralUtility::makeInstance(FileReference::class, $fileReferenceData); |
||
638 | return $fileReferenceObject; |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * Gets data for the given uid of the file reference record. |
||
643 | * |
||
644 | * @param int $uid The uid of the file usage (sys_file_reference) to be fetched |
||
645 | * @param bool $raw Whether to get raw results without performing overlays |
||
646 | * @return array|null |
||
647 | */ |
||
648 | protected function getFileReferenceData($uid, $raw = false) |
||
669 | } |
||
670 | |||
671 | /** |
||
672 | * Returns an instance of the FileIndexRepository |
||
673 | * |
||
674 | * @return FileIndexRepository |
||
675 | */ |
||
676 | protected function getFileIndexRepository() |
||
677 | { |
||
678 | return FileIndexRepository::getInstance(); |
||
679 | } |
||
680 | |||
681 | /** |
||
682 | * Returns an instance of the ProcessedFileRepository |
||
683 | * |
||
684 | * @return ProcessedFileRepository |
||
685 | */ |
||
686 | protected function getProcessedFileRepository() |
||
687 | { |
||
688 | return GeneralUtility::makeInstance(ProcessedFileRepository::class); |
||
689 | } |
||
690 | |||
691 | /** |
||
692 | * Returns an instance of the Indexer |
||
693 | * |
||
694 | * @param ResourceStorage $storage |
||
695 | * @return Index\Indexer |
||
696 | */ |
||
697 | protected function getIndexer(ResourceStorage $storage) |
||
700 | } |
||
701 | } |
||
702 |