[BUGFIX] Wrong variable name used
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / ResourceFactory.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Andreas Wolf <andreas.wolf@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 // TODO implement constructor-level caching
30 /**
31 * Factory class for FAL objects.
32 *
33 * NOTE: This class is part of the lowlevel FAL API and should not be used from outside the FAL package.
34 *
35 * @author Andreas Wolf <andreas.wolf@typo3.org>
36 */
37 class ResourceFactory implements \TYPO3\CMS\Core\SingletonInterface {
38
39 /**
40 * Gets a singleton instance of this class.
41 *
42 * @return ResourceFactory
43 */
44 static public function getInstance() {
45 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
46 }
47
48 /**
49 * @var ResourceStorage[]
50 */
51 protected $storageInstances = array();
52
53 /**
54 * @var Collection\AbstractFileCollection[]
55 */
56 protected $collectionInstances = array();
57
58 /**
59 * @var File[]
60 */
61 protected $fileInstances = array();
62
63 /**
64 * @var FileReference[]
65 */
66 protected $fileReferenceInstances = array();
67
68 /**
69 * Creates a driver object for a specified storage object.
70 *
71 * @param string $driverIdentificationString The driver class (or identifier) to use.
72 * @param array $driverConfiguration The configuration of the storage
73 * @return Driver\AbstractDriver
74 * @throws \InvalidArgumentException
75 */
76 public function getDriverObject($driverIdentificationString, array $driverConfiguration) {
77 /** @var $driverRegistry Driver\DriverRegistry */
78 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Driver\\DriverRegistry');
79 $driverClass = $driverRegistry->getDriverClass($driverIdentificationString);
80 $driverObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($driverClass, $driverConfiguration);
81 return $driverObject;
82 }
83
84 /**
85 * Creates an instance of the storage from given UID. The $recordData can
86 * be supplied to increase performance.
87 *
88 * @param integer $uid The uid of the storage to instantiate.
89 * @param array $recordData The record row from database.
90 *
91 * @throws \InvalidArgumentException
92 * @return ResourceStorage
93 */
94 public function getStorageObject($uid, array $recordData = array()) {
95 if (!is_numeric($uid)) {
96 throw new \InvalidArgumentException('uid of Storage has to be numeric.', 1314085991);
97 }
98 if (!$this->storageInstances[$uid]) {
99 $storageConfiguration = NULL;
100 $storageObject = NULL;
101 // If the built-in storage with UID=0 is requested:
102 if (intval($uid) === 0) {
103 $recordData = array(
104 'uid' => 0,
105 'pid' => 0,
106 'name' => 'Default Storage',
107 'description' => 'Internal storage, mounting the main TYPO3_site directory.',
108 'driver' => 'Local',
109 'processingfolder' => 'typo3temp/_processed_/',
110 // legacy code
111 'configuration' => '',
112 'is_online' => TRUE,
113 'is_browsable' => TRUE,
114 'is_public' => TRUE,
115 'is_writable' => TRUE
116 );
117 $storageConfiguration = array(
118 'basePath' => '/',
119 'pathType' => 'relative'
120 );
121 } elseif (count($recordData) === 0 || $recordData['uid'] !== $uid) {
122 /** @var $storageRepository StorageRepository */
123 $storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
124 /** @var $storage ResourceStorage */
125 $storageObject = $storageRepository->findByUid($uid);
126 }
127 if (!$storageObject instanceof ResourceStorage) {
128 $storageObject = $this->createStorageObject($recordData, $storageConfiguration);
129 }
130 $this->storageInstances[$uid] = $storageObject;
131 }
132 return $this->storageInstances[$uid];
133 }
134
135 /**
136 * Converts a flexform data string to a flat array with key value pairs
137 *
138 * @param string $flexFormData
139 * @return array Array with key => value pairs of the field data in the FlexForm
140 */
141 public function convertFlexFormDataToConfigurationArray($flexFormData) {
142 $configuration = array();
143 if ($flexFormData) {
144 $flexFormContents = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($flexFormData);
145 if (!empty($flexFormContents['data']['sDEF']['lDEF']) && is_array($flexFormContents['data']['sDEF']['lDEF'])) {
146 foreach ($flexFormContents['data']['sDEF']['lDEF'] as $key => $value) {
147 if (isset($value['vDEF'])) {
148 $configuration[$key] = $value['vDEF'];
149 }
150 }
151 }
152 }
153 return $configuration;
154 }
155
156 /**
157 * Creates an instance of the collection from given UID. The $recordData can be supplied to increase performance.
158 *
159 * @param integer $uid The uid of the collection to instantiate.
160 * @param array $recordData The record row from database.
161 *
162 * @throws \InvalidArgumentException
163 * @return Collection\AbstractFileCollection
164 */
165 public function getCollectionObject($uid, array $recordData = array()) {
166 if (!is_numeric($uid)) {
167 throw new \InvalidArgumentException('uid of collection has to be numeric.', 1314085999);
168 }
169 if (!$this->collectionInstances[$uid]) {
170 // Get mount data if not already supplied as argument to this function
171 if (count($recordData) === 0 || $recordData['uid'] !== $uid) {
172 /** @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
173 $recordData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'sys_file_collection', 'uid=' . intval($uid) . ' AND deleted=0');
174 if (!is_array($recordData)) {
175 throw new \InvalidArgumentException('No collection found for given UID.', 1314085992);
176 }
177 }
178 $collectionObject = $this->createCollectionObject($recordData);
179 $this->collectionInstances[$uid] = $collectionObject;
180 }
181 return $this->collectionInstances[$uid];
182 }
183
184 /**
185 * Creates a collection object.
186 *
187 * @param array $collectionData The database row of the sys_file_collection record.
188 * @return Collection\AbstractFileCollection
189 */
190 public function createCollectionObject(array $collectionData) {
191 switch ($collectionData['type']) {
192 case 'static':
193 $collection = Collection\StaticFileCollection::create($collectionData);
194 break;
195 case 'folder':
196 $collection = Collection\FolderBasedFileCollection::create($collectionData);
197 break;
198 default:
199 $collection = NULL;
200 }
201 return $collection;
202 }
203
204 /**
205 * Creates a storage object from a storage database row.
206 *
207 * @param array $storageRecord
208 * @param array $storageConfiguration Storage configuration (if given, this won't be extracted from the FlexForm value but the supplied array used instead)
209 * @return ResourceStorage
210 */
211 public function createStorageObject(array $storageRecord, array $storageConfiguration = NULL) {
212 $className = 'TYPO3\\CMS\\Core\\Resource\\ResourceStorage';
213 if (!$storageConfiguration) {
214 $storageConfiguration = $this->convertFlexFormDataToConfigurationArray($storageRecord['configuration']);
215 }
216 $driverType = $storageRecord['driver'];
217 $driverObject = $this->getDriverObject($driverType, $storageConfiguration);
218 /** @var $storage ResourceStorage */
219 $storage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className, $driverObject, $storageRecord);
220 // TODO handle publisher
221 return $storage;
222 }
223
224 /**
225 * Creates a folder to directly access (a part of) a storage.
226 *
227 * @param ResourceStorage $storage The storage the folder belongs to
228 * @param string $identifier The path to the folder. Might also be a simple unique string, depending on the storage driver.
229 * @param string $name The name of the folder (e.g. the folder name)
230 * @return Folder
231 */
232 public function createFolderObject(ResourceStorage $storage, $identifier, $name) {
233 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Folder', $storage, $identifier, $name);
234 }
235
236 protected function createPublisherFromConfiguration(array $configuration) {
237 $publishingTarget = $this->getStorageObject($configuration['publisherConfiguration']['publishingTarget']);
238 $publisher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($configuration['publisher'], $publishingTarget, $configuration['publisherConfiguration']);
239 return $publisher;
240 }
241
242 /**
243 * Creates an instance of the file given UID. The $fileData can be supplied
244 * to increase performance.
245 *
246 * @param integer $uid The uid of the file to instantiate.
247 * @param array $fileData The record row from database.
248 *
249 * @throws \InvalidArgumentException
250 * @return File
251 */
252 public function getFileObject($uid, array $fileData = array()) {
253 if (!is_numeric($uid)) {
254 throw new \InvalidArgumentException('uid of file has to be numeric.', 1300096564);
255 }
256 if (!$this->fileInstances[$uid]) {
257 // Fetches data in case $fileData is empty
258 if (empty($fileData)) {
259 /** @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
260 $fileData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'sys_file', 'uid=' . intval($uid) . ' AND deleted=0');
261 if (!is_array($fileData)) {
262 throw new \InvalidArgumentException('No file found for given UID.', 1317178604);
263 }
264 }
265 $this->fileInstances[$uid] = $this->createFileObject($fileData);
266 }
267 return $this->fileInstances[$uid];
268 }
269
270 /**
271 * Gets an file object from an identifier [storage]:[fileId]
272 *
273 * @param string $identifier
274 * @return File
275 */
276 public function getFileObjectFromCombinedIdentifier($identifier) {
277 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(':', $identifier);
278 if (count($parts) === 2) {
279 $storageUid = $parts[0];
280 $fileIdentifier = $parts[1];
281 } else {
282 // We only got a path: Go into backwards compatibility mode and
283 // use virtual Storage (uid=0)
284 $storageUid = 0;
285 $fileIdentifier = $parts[0];
286 }
287 return $this->getStorageObject($storageUid)->getFile($fileIdentifier);
288 }
289
290 /**
291 * Bulk function, can be used for anything to get a file or folder
292 *
293 * 1. It's a UID
294 * 2. It's a combined identifier
295 * 3. It's just a path/filename (coming from the oldstyle/backwards compatibility)
296 *
297 * Files, previously laid on fileadmin/ or something, will be "mapped" to the storage the file is
298 * in now. Files like typo3temp/ or typo3conf/ will be moved to the first writable storage
299 * in its processing folder
300 *
301 * $input could be
302 * - "2:myfolder/myfile.jpg" (combined identifier)
303 * - "23" (file UID)
304 * - "uploads/myfile.png" (backwards-compatibility, storage "0")
305 * - "file:23"
306 *
307 * @param string $input
308 * @return FileInterface|Folder
309 */
310 public function retrieveFileOrFolderObject($input) {
311 // Easy function to deal with that, could be dropped in the future
312 // if we know where to use this function
313 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($input, 'file:')) {
314 $input = substr($input, 5);
315 return $this->retrieveFileOrFolderObject($input);
316 } elseif (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($input)) {
317 return $this->getFileObject($input);
318 } elseif (strpos($input, ':') > 0) {
319 list($prefix, $folderIdentifier) = explode(':', $input);
320 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($prefix)) {
321 // path or folder in a valid storageUID
322 return $this->getObjectFromCombinedIdentifier($input);
323 } elseif ($prefix == 'EXT') {
324 $input = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($input);
325 $input = \t3lib_Utility_Path::getRelativePath(PATH_site, dirname($input)) . basename($input);
326 return $this->getFileObjectFromCombinedIdentifier($input);
327 }
328 } else {
329 // only the path
330 return $this->getFileObjectFromCombinedIdentifier($input);
331 }
332 }
333
334 /**
335 * Gets an file object from an identifier [storage]:[fileId]
336 *
337 * @TODO check naming, inserted by SteffenR while working on filelist
338 * @param string $identifier
339 * @return Folder
340 */
341 public function getFolderObjectFromCombinedIdentifier($identifier) {
342 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(':', $identifier);
343 if (count($parts) === 2) {
344 $storageUid = $parts[0];
345 $folderIdentifier = $parts[1];
346 } else {
347 // We only got a path: Go into backwards compatibility mode and
348 // use virtual Storage (uid=0)
349 $storageUid = 0;
350 $folderIdentifier = substr($parts[0], strlen(PATH_site));
351 }
352 return $this->getStorageObject($storageUid)->getFolder($folderIdentifier);
353 }
354
355 /**
356 * Gets a storage object from a combined identifier
357 *
358 * @param string $identifier An identifier of the form [storage uid]:[object identifier]
359 * @return ResourceStorage
360 */
361 public function getStorageObjectFromCombinedIdentifier($identifier) {
362 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(':', $identifier);
363 if (count($parts) === 2) {
364 $storageUid = $parts[0];
365 }
366
367 return $this->getStorageObject($storageUid);
368 }
369
370 /**
371 * Gets a file or folder object.
372 *
373 * @param string $identifier
374 *
375 * @throws \RuntimeException
376 * @return FileInterface|Folder
377 */
378 public function getObjectFromCombinedIdentifier($identifier) {
379 list($storageId, $objectIdentifier) = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(':', $identifier);
380 $storage = $this->getStorageObject($storageId);
381 if ($storage->hasFile($objectIdentifier)) {
382 return $storage->getFile($objectIdentifier);
383 } elseif ($storage->hasFolder($objectIdentifier)) {
384 return $storage->getFolder($objectIdentifier);
385 } else {
386 throw new \RuntimeException('Object with identifier "' . $identifier . '" does not exist in storage', 1329647780);
387 }
388 }
389
390 /**
391 * Creates a file object from an array of file data. Requires a database
392 * row to be fetched.
393 *
394 * @param array $fileData
395 * @return File
396 */
397 public function createFileObject(array $fileData) {
398 /** @var File $fileObject */
399 $fileObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\File', $fileData);
400 if (is_numeric($fileData['storage'])) {
401 $storageObject = $this->getStorageObject($fileData['storage']);
402 $fileObject->setStorage($storageObject);
403 }
404 return $fileObject;
405 }
406
407 /**
408 * Creates an instance of a FileReference object. The $fileReferenceData can
409 * be supplied to increase performance.
410 *
411 * @param integer $uid The uid of the file usage (sys_file_reference) to instantiate.
412 * @param array $fileReferenceData The record row from database.
413 *
414 * @throws \InvalidArgumentException
415 * @return FileReference
416 */
417 public function getFileReferenceObject($uid, array $fileReferenceData = array()) {
418 if (!is_numeric($uid)) {
419 throw new \InvalidArgumentException('uid of fileusage (sys_file_reference) has to be numeric.', 1300086584);
420 }
421 if (!$this->fileReferenceInstances[$uid]) {
422 // Fetches data in case $fileData is empty
423 if (empty($fileReferenceData)) {
424 // fetch the reference record of the current workspace
425 if (TYPO3_MODE === 'BE') {
426 $fileReferenceData = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordWSOL('sys_file_reference', $uid);
427 } elseif (is_object($GLOBALS['TSFE'])) {
428 $fileReferenceData = $GLOBALS['TSFE']->sys_page->checkRecord('sys_file_reference', $uid);
429 } else {
430 /** @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
431 $fileReferenceData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'sys_file_reference', 'uid=' . intval($uid) . ' AND deleted=0');
432 }
433 if (!is_array($fileReferenceData)) {
434 throw new \InvalidArgumentException('No fileusage (sys_file_reference) found for given UID.', 1317178794);
435 }
436 }
437 $this->fileReferenceInstances[$uid] = $this->createFileReferenceObject($fileReferenceData);
438 }
439 return $this->fileReferenceInstances[$uid];
440 }
441
442 /**
443 * Creates a file usage object from an array of fileReference data
444 * from sys_file_reference table.
445 * Requires a database row to be already fetched and present.
446 *
447 * @param array $fileReferenceData
448 * @return FileReference
449 */
450 public function createFileReferenceObject(array $fileReferenceData) {
451 /** @var FileReference $fileReferenceObject */
452 $fileReferenceObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileReference', $fileReferenceData);
453 return $fileReferenceObject;
454 }
455 }
456
457
458 ?>