Added Feature #16641: Here comes FAL - beta2 version
[Packages/TYPO3.CMS.git] / typo3 / sysext / fal / classes / class.tx_fal_Mount.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 FAL development team <fal@wmdb.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * File Abtraction Layer Mount
30 *
31 * @author FAL development team <fal@wmdb.de>
32 * @package TYPO3
33 * @subpackage tx_fal
34 * @version $Id$
35 */
36 class tx_fal_Mount {
37
38 /**
39 * The backend this mount uses
40 *
41 * @var tx_fal_storage_Interface
42 */
43 protected $storageBackend;
44
45 /**
46 * DESCRIPTION
47 *
48 * @var [to be defined]
49 */
50 protected $basePath;
51
52 /**
53 * DESCRIPTION
54 *
55 * @var [to be defined]
56 */
57 protected $uid;
58
59 /**
60 * DESCRIPTION
61 *
62 * @todo Andy Grunwald, 01.12.2010, why private? protected for access in xclasses?
63 *
64 * @var array<tx_fal_Mount>
65 */
66 private static $instances = array();
67
68 /**
69 * DESCRIPTION
70 *
71 * @param [to be defined] $mountInformation DESCRIPTION
72 */
73 public function __construct($mountInformation) {
74 $this->uid = $mountInformation['uid'];
75 $this->alias = $mountInformation['alias'];
76 }
77
78 /**
79 * DESCRIPTION
80 *
81 * @static
82 * @param integer $mountUid DESCRIPTION
83 * @return [to be defined] DESCRIPTION
84 *
85 * @throws RuntimeException
86 */
87 public static function getInstanceForUid($mountUid) {
88 if (is_null($mountUid)) {
89 $mountUid = 0;
90 }
91 if (count(self::$instances) == 0) {
92 self::loadMountsFromDatabase();
93 }
94
95 if (self::$instances[$mountUid]) {
96 return self::$instances[$mountUid];
97 }
98
99 throw new RuntimeException("Mount with uid $mountUid was not found.");
100 }
101
102 /**
103 * Returns an instance of a file mount by its alias
104 *
105 * @static
106 * @param string $alias DESCRIPTION
107 * @return void
108 *
109 * @throws RuntimeException
110 */
111 public static function getInstanceForAlias($alias) {
112 if (count(self::$instances) == 0) {
113 self::loadMountsFromDatabase();
114 }
115
116 foreach (self::$instances as $instance) {
117 if ($instance->getAlias() == $alias) {
118 return $instance;
119 }
120 }
121
122 throw new RuntimeException("FAL mount $alias was not found.");
123 }
124
125 /**
126 * Loads all existing FAL mounts from the database and initializes them for later use.
127 *
128 * @static
129 * @return void
130 */
131 protected static function loadMountsFromDatabase() {
132 $mountRecord = array('alias' => 'fileadmin', 'uid' => 0, 'storage_backend' => 'tx_fal_storage_FileSystemStorage');
133 $storageConfiguration = array('relative' => TRUE, 'path' => 'fileadmin/');
134
135 self::$instances[0] = self::mountObjectFactory($mountRecord, $storageConfiguration);
136
137 $mountRecords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_files_mounts', 'deleted=0');
138 foreach ($mountRecords as $mountRecord) {
139 $storageConfiguration = t3lib_div::xml2array($mountRecord['backend_configuration']);
140 $storageConfiguration = self::extractValuesFromFlexformArray($storageConfiguration['data']);
141
142 self::$instances[$mountRecord['uid']] = self::mountObjectFactory($mountRecord, $storageConfiguration);
143 }
144 }
145
146 /**
147 * Factory method for mount objects.
148 *
149 * @static
150 * @param [to be defined] $mountRecord DESCRIPTION
151 * @param [to be defined] $storageConfiguration DESCRIPTION
152 * @return [to be defined] DESCRIPTION
153 *
154 * @throws RuntimeException
155 */
156 protected static function mountObjectFactory($mountRecord, $storageConfiguration) {
157 $storageBackendClass = $mountRecord['storage_backend'];
158 if (!class_exists($storageBackendClass)) {
159 throw new RuntimeException("Class $storageBackendClass does not exist.");
160 }
161 $storageBackend = t3lib_div::makeInstance($storageBackendClass, $storageConfiguration);
162
163 $mountObject = new tx_fal_Mount($mountRecord);
164 $mountObject->setStorageBackend($storageBackend);
165
166 return $mountObject;
167 }
168
169 /**
170 * Helper function to ease unit testing. Ignore in normal operation.
171 *
172 * @static
173 * @param array $instances DESCRIPTION
174 * @return void
175 */
176 public static function _setInstances(array $instances) {
177 self::$instances = $instances;
178 }
179
180 /**
181 * DESCRIPTION
182 *
183 * @static
184 * @param array $flexformArray DESCRIPTION
185 * @return [to be defined] DESCRIPTION
186 */
187 protected static function extractValuesFromFlexformArray(array $flexformArray) {
188 foreach ($flexformArray as $sheet) {
189 foreach ($sheet as $language) {
190 foreach ($language as $fieldName => $fieldValue) {
191 $values[$fieldName] = $fieldValue['vDEF'];
192 }
193 }
194 }
195
196 return $values;
197 }
198
199 /**
200 * DESCRIPTION
201 *
202 * @param tx_fal_storage_Interface $storageBackend The backend to use
203 */
204 public function setStorageBackend(tx_fal_storage_Interface $storageBackend) {
205 $this->storageBackend = $storageBackend;
206 }
207
208 /**
209 * Returns the storage backend used by this file mount.
210 *
211 * @return tx_fal_storage_Interface
212 */
213 public function getStorageBackend() {
214 return $this->storageBackend;
215 }
216
217 /**
218 * Creates a directory inside this mount. Success of this operation depends - among other things - on the directory
219 * support by the backend.
220 *
221 * @param [to be defined] $path The path to create the directory in
222 * @param [to be defined] $directoryName The directory to create
223 * @return boolean TRUE if the directory could be created
224 */
225 public function createDirectory($path, $directoryName) {
226 return $this->storageBackend->createDirectory($path, $directoryName);
227 }
228
229 /**
230 * DESCRIPTION
231 *
232 * @todo Implement tx_fal_Mount::getDirectoryListing
233 *
234 * @param [to be defined] $path DESCRIPTION
235 * @return [to be defined] DESCRIPTION
236 */
237 public function getDirectoryListing($path) {
238 }
239
240 /**
241 * Getter for mount uid
242 *
243 * @return integer DESCRIPTION
244 */
245 public function getUid() {
246 return $this->uid;
247 }
248
249 /**
250 * DESCRIPTION
251 *
252 * @return [to be defined]
253 */
254 public function getAlias() {
255 return $this->alias;
256 }
257
258 /**
259 * DESCRIPTION
260 *
261 * @return [to be defined]
262 */
263 public function getBasePath() {
264 return $this->basePath;
265 }
266 }
267
268 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/fal/classes/class.tx_fal_Mount.php'])) {
269 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/fal/classes/class.tx_fal_Mount.php']);
270 }
271 ?>