Complex classes like Folder 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Folder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Folder extends File { |
||
21 | |||
22 | private static $singular_name = "Folder"; |
||
|
|||
23 | |||
24 | private static $plural_name = "Folders"; |
||
25 | |||
26 | private static $default_sort = "\"Name\""; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | */ |
||
31 | public function populateDefaults() { |
||
32 | parent::populateDefaults(); |
||
33 | |||
34 | if(!$this->Name) $this->Name = _t('AssetAdmin.NEWFOLDER',"NewFolder"); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Find the given folder or create it both as {@link Folder} database records |
||
39 | * and on the filesystem. If necessary, creates parent folders as well. If it's |
||
40 | * unable to find or make the folder, it will return null (as /assets is unable |
||
41 | * to be represented by a Folder DataObject) |
||
42 | * |
||
43 | * @param $folderPath string Absolute or relative path to the file. |
||
44 | * If path is relative, its interpreted relative to the "assets/" directory. |
||
45 | * @return Folder|null |
||
46 | */ |
||
47 | public static function find_or_make($folderPath) { |
||
48 | // Create assets directory, if it is missing |
||
49 | if(!file_exists(ASSETS_PATH)) Filesystem::makeFolder(ASSETS_PATH); |
||
50 | |||
51 | $folderPath = trim(Director::makeRelative($folderPath)); |
||
52 | // replace leading and trailing slashes |
||
53 | $folderPath = preg_replace('/^\/?(.*)\/?$/', '$1', $folderPath); |
||
54 | $parts = explode("/",$folderPath); |
||
55 | |||
56 | $parentID = 0; |
||
57 | $item = null; |
||
58 | $filter = FileNameFilter::create(); |
||
59 | foreach($parts as $part) { |
||
60 | if(!$part) continue; // happens for paths with a trailing slash |
||
61 | |||
62 | // Ensure search includes folders with illegal characters removed, but |
||
63 | // err in favour of matching existing folders if $folderPath |
||
64 | // includes illegal characters itself. |
||
65 | $partSafe = $filter->filter($part); |
||
66 | $item = Folder::get()->filter(array( |
||
67 | 'ParentID' => $parentID, |
||
68 | 'Name' => array($partSafe, $part) |
||
69 | ))->first(); |
||
70 | |||
71 | if(!$item) { |
||
72 | $item = new Folder(); |
||
73 | $item->ParentID = $parentID; |
||
74 | $item->Name = $partSafe; |
||
75 | $item->Title = $part; |
||
76 | $item->write(); |
||
77 | } |
||
78 | if(!file_exists($item->getFullPath())) { |
||
79 | Filesystem::makeFolder($item->getFullPath()); |
||
80 | } |
||
81 | $parentID = $item->ID; |
||
82 | } |
||
83 | |||
84 | return $item; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Synchronize the file database with the actual content of the assets |
||
89 | * folder. |
||
90 | */ |
||
91 | public function syncChildren() { |
||
92 | $parentID = (int)$this->ID; // parentID = 0 on the singleton, used as the 'root node'; |
||
93 | $added = 0; |
||
94 | $deleted = 0; |
||
95 | $skipped = 0; |
||
96 | |||
97 | // First, merge any children that are duplicates |
||
98 | $duplicateChildrenNames = DB::prepared_query( |
||
99 | 'SELECT "Name" FROM "File" WHERE "ParentID" = ? GROUP BY "Name" HAVING count(*) > 1', |
||
100 | array($parentID) |
||
101 | )->column(); |
||
102 | if($duplicateChildrenNames) foreach($duplicateChildrenNames as $childName) { |
||
103 | // Note, we do this in the database rather than object-model; otherwise we get all sorts of problems |
||
104 | // about deleting files |
||
105 | $children = DB::prepared_query( |
||
106 | 'SELECT "ID" FROM "File" WHERE "Name" = ? AND "ParentID" = ?', |
||
107 | array($childName, $parentID) |
||
108 | )->column(); |
||
109 | if($children) { |
||
110 | $keptChild = array_shift($children); |
||
111 | foreach($children as $removedChild) { |
||
112 | DB::prepared_query('UPDATE "File" SET "ParentID" = ? WHERE "ParentID" = ?', |
||
113 | array($keptChild, $removedChild)); |
||
114 | DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($removedChild)); |
||
115 | } |
||
116 | } else { |
||
117 | user_error("Inconsistent database issue: SELECT ID FROM \"File\" WHERE Name = '$childName'" |
||
118 | . " AND ParentID = $parentID should have returned data", E_USER_WARNING); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | |||
123 | // Get index of database content |
||
124 | // We don't use DataObject so that things like subsites doesn't muck with this. |
||
125 | $dbChildren = DB::prepared_query('SELECT * FROM "File" WHERE "ParentID" = ?', array($parentID)); |
||
126 | $hasDbChild = array(); |
||
127 | |||
128 | if($dbChildren) { |
||
129 | foreach($dbChildren as $dbChild) { |
||
130 | $className = $dbChild['ClassName']; |
||
131 | if(!$className) $className = "File"; |
||
132 | $hasDbChild[$dbChild['Name']] = new $className($dbChild); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | $unwantedDbChildren = $hasDbChild; |
||
137 | |||
138 | // if we're syncing a folder with no ID, we assume we're syncing the root assets folder |
||
139 | // however the Filename field is populated with "NewFolder", so we need to set this to empty |
||
140 | // to satisfy the baseDir variable below, which is the root folder to scan for new files in |
||
141 | if(!$parentID) $this->Filename = ''; |
||
142 | |||
143 | // Iterate through the actual children, correcting the database as necessary |
||
144 | $baseDir = $this->FullPath; |
||
145 | |||
146 | // @todo this shouldn't call die() but log instead |
||
147 | if($parentID && !$this->Filename) die($this->ID . " - " . $this->FullPath); |
||
148 | |||
149 | if(file_exists($baseDir)) { |
||
150 | $actualChildren = scandir($baseDir); |
||
151 | $ignoreRules = Filesystem::config()->sync_blacklisted_patterns; |
||
152 | $allowedExtensions = File::config()->allowed_extensions; |
||
153 | $checkExtensions = $this->config()->apply_restrictions_to_admin || !Permission::check('ADMIN'); |
||
154 | |||
155 | foreach($actualChildren as $actualChild) { |
||
156 | $skip = false; |
||
157 | |||
158 | // Check ignore patterns |
||
159 | if($ignoreRules) foreach($ignoreRules as $rule) { |
||
160 | if(preg_match($rule, $actualChild)) { |
||
161 | $skip = true; |
||
162 | break; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | // Check allowed extensions, unless admin users are allowed to bypass these exclusions |
||
167 | if($checkExtensions |
||
168 | && ($extension = self::get_file_extension($actualChild)) |
||
169 | && !in_array(strtolower($extension), $allowedExtensions) |
||
170 | ) { |
||
171 | $skip = true; |
||
172 | } |
||
173 | |||
174 | if($skip) { |
||
175 | $skipped++; |
||
176 | continue; |
||
177 | } |
||
178 | |||
179 | // A record with a bad class type doesn't deserve to exist. It must be purged! |
||
180 | if(isset($hasDbChild[$actualChild])) { |
||
181 | $child = $hasDbChild[$actualChild]; |
||
182 | if(( !( $child instanceof Folder ) && is_dir($baseDir . $actualChild) ) |
||
183 | || (( $child instanceof Folder ) && !is_dir($baseDir . $actualChild)) ) { |
||
184 | DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($child->ID)); |
||
185 | unset($hasDbChild[$actualChild]); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if(isset($hasDbChild[$actualChild])) { |
||
190 | $child = $hasDbChild[$actualChild]; |
||
191 | unset($unwantedDbChildren[$actualChild]); |
||
192 | } else { |
||
193 | $added++; |
||
194 | $childID = $this->constructChild($actualChild); |
||
195 | $child = DataObject::get_by_id("File", $childID); |
||
196 | } |
||
197 | |||
198 | if( $child && is_dir($baseDir . $actualChild)) { |
||
199 | $childResult = $child->syncChildren(); |
||
200 | $added += $childResult['added']; |
||
201 | $deleted += $childResult['deleted']; |
||
202 | $skipped += $childResult['skipped']; |
||
203 | } |
||
204 | |||
205 | // Clean up the child record from memory after use. Important! |
||
206 | $child->destroy(); |
||
207 | $child = null; |
||
208 | } |
||
209 | |||
210 | // Iterate through the unwanted children, removing them all |
||
211 | if(isset($unwantedDbChildren)) foreach($unwantedDbChildren as $unwantedDbChild) { |
||
212 | DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($unwantedDbChild->ID)); |
||
213 | $deleted++; |
||
214 | } |
||
215 | } else { |
||
216 | DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($this->ID)); |
||
217 | } |
||
218 | |||
219 | return array( |
||
220 | 'added' => $added, |
||
221 | 'deleted' => $deleted, |
||
222 | 'skipped' => $skipped |
||
223 | ); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Construct a child of this Folder with the given name. |
||
228 | * It does this without actually using the object model, as this starts messing |
||
229 | * with all the data. Rather, it does a direct database insert. |
||
230 | * |
||
231 | * @param string $name Name of the file or folder |
||
232 | * @return integer the ID of the newly saved File record |
||
233 | */ |
||
234 | public function constructChild($name) { |
||
235 | // Determine the class name - File, Folder or Image |
||
236 | $baseDir = $this->FullPath; |
||
237 | if(is_dir($baseDir . $name)) { |
||
238 | $className = "Folder"; |
||
239 | } else { |
||
240 | $className = File::get_class_for_file_extension(pathinfo($name, PATHINFO_EXTENSION)); |
||
241 | } |
||
242 | |||
243 | $ownerID = Member::currentUserID(); |
||
244 | |||
245 | $filename = $this->Filename . $name; |
||
246 | if($className == 'Folder' ) $filename .= '/'; |
||
247 | |||
248 | $nowExpression = DB::get_conn()->now(); |
||
249 | DB::prepared_query("INSERT INTO \"File\" |
||
250 | (\"ClassName\", \"ParentID\", \"OwnerID\", \"Name\", \"Filename\", \"Created\", \"LastEdited\", \"Title\") |
||
251 | VALUES (?, ?, ?, ?, ?, $nowExpression, $nowExpression, ?)", |
||
252 | array($className, $this->ID, $ownerID, $name, $filename, $name) |
||
253 | ); |
||
254 | |||
255 | return DB::get_generated_id("File"); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Take a file uploaded via a POST form, and save it inside this folder. |
||
260 | * File names are filtered through {@link FileNameFilter}, see class documentation |
||
261 | * on how to influence this behaviour. |
||
262 | */ |
||
263 | public function addUploadToFolder($tmpFile) { |
||
264 | if(!is_array($tmpFile)) { |
||
265 | user_error("Folder::addUploadToFolder() Not passed an array." |
||
266 | . " Most likely, the form hasn't got the right enctype", E_USER_ERROR); |
||
267 | } |
||
268 | if(!isset($tmpFile['size'])) { |
||
269 | return; |
||
270 | } |
||
271 | |||
272 | $base = BASE_PATH; |
||
273 | // $parentFolder = Folder::findOrMake("Uploads"); |
||
274 | |||
275 | // Generate default filename |
||
276 | $nameFilter = FileNameFilter::create(); |
||
277 | $file = $nameFilter->filter($tmpFile['name']); |
||
278 | while($file[0] == '_' || $file[0] == '.') { |
||
279 | $file = substr($file, 1); |
||
280 | } |
||
281 | |||
282 | $file = $this->RelativePath . $file; |
||
283 | Filesystem::makeFolder(dirname("$base/$file")); |
||
284 | |||
285 | $doubleBarrelledExts = array('.gz', '.bz', '.bz2'); |
||
286 | |||
287 | $ext = ""; |
||
288 | if(preg_match('/^(.*)(\.[^.]+)$/', $file, $matches)) { |
||
289 | $file = $matches[1]; |
||
290 | $ext = $matches[2]; |
||
291 | // Special case for double-barrelled |
||
292 | if(in_array($ext, $doubleBarrelledExts) && preg_match('/^(.*)(\.[^.]+)$/', $file, $matches)) { |
||
293 | $file = $matches[1]; |
||
294 | $ext = $matches[2] . $ext; |
||
295 | } |
||
296 | } |
||
297 | $origFile = $file; |
||
298 | |||
299 | $i = 1; |
||
300 | while(file_exists("$base/$file$ext")) { |
||
301 | $i++; |
||
302 | $oldFile = $file; |
||
303 | |||
304 | if(strpos($file, '.') !== false) { |
||
305 | $file = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $file); |
||
306 | } elseif(strpos($file, '_') !== false) { |
||
307 | $file = preg_replace('/_([^_]+$)/', '_' . $i, $file); |
||
308 | } else { |
||
309 | $file .= '_'.$i; |
||
310 | } |
||
311 | |||
312 | if($oldFile == $file && $i > 2) user_error("Couldn't fix $file$ext with $i", E_USER_ERROR); |
||
313 | } |
||
314 | |||
315 | if (move_uploaded_file($tmpFile['tmp_name'], "$base/$file$ext")) { |
||
316 | // Update with the new image |
||
317 | return $this->constructChild(basename($file . $ext)); |
||
318 | } else { |
||
319 | if(!file_exists($tmpFile['tmp_name'])) { |
||
320 | user_error("Folder::addUploadToFolder: '$tmpFile[tmp_name]' doesn't exist", E_USER_ERROR); |
||
321 | } else { |
||
322 | user_error("Folder::addUploadToFolder: Couldn't copy '$tmpFile[tmp_name]' to '$base/$file$ext'", |
||
323 | E_USER_ERROR); |
||
324 | } |
||
325 | return false; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | protected function validate() { |
||
332 | |||
333 | //------------------------------------------------------------------------------------------------- |
||
334 | // Data Model Definition |
||
335 | |||
336 | public function getRelativePath() { |
||
339 | |||
340 | public function onBeforeDelete() { |
||
341 | if($this->ID && ($children = $this->AllChildren())) { |
||
342 | foreach($children as $child) { |
||
343 | if(!$this->Filename || !$this->Name || !file_exists($this->getFullPath())) { |
||
344 | $child->setField('Name',null); |
||
361 | |||
362 | /** |
||
363 | * Return the relative URL of an icon for this file type |
||
364 | * |
||
365 | * @return String |
||
366 | */ |
||
367 | public function Icon() { |
||
370 | |||
371 | /** Override setting the Title of Folders to that Name, Filename and Title are always in sync. |
||
372 | * Note that this is not appropriate for files, because someone might want to create a human-readable name |
||
373 | * of a file that is different from its name on disk. But folders should always match their name on disk. */ |
||
374 | public function setTitle($title) { |
||
377 | |||
378 | public function getTitle() { |
||
381 | |||
382 | public function setName($name) { |
||
386 | |||
387 | public function setFilename($filename) { |
||
391 | |||
392 | /** |
||
393 | * A folder doesn't have a (meaningful) file size. |
||
394 | * |
||
395 | * @return Null |
||
396 | */ |
||
397 | public function getSize() { |
||
400 | |||
401 | /** |
||
402 | * Delete the database record (recursively for folders) without touching the filesystem |
||
403 | */ |
||
404 | public function deleteDatabaseOnly() { |
||
411 | |||
412 | /** |
||
413 | * Returns all children of this folder |
||
414 | * |
||
415 | * @return DataList |
||
416 | */ |
||
417 | public function myChildren() { |
||
420 | |||
421 | /** |
||
422 | * Returns true if this folder has children |
||
423 | * |
||
424 | * @return bool |
||
425 | */ |
||
426 | public function hasChildren() { |
||
429 | |||
430 | /** |
||
431 | * Returns true if this folder has children |
||
432 | * |
||
433 | * @return bool |
||
434 | */ |
||
435 | public function hasChildFolders() { |
||
438 | |||
439 | /** |
||
440 | * Overloaded to call recursively on all contained {@link File} records. |
||
441 | */ |
||
442 | public function updateFilesystem() { |
||
454 | |||
455 | /** |
||
456 | * Return the FieldList used to edit this folder in the CMS. |
||
457 | * You can modify this FieldList by subclassing folder, or by creating a {@link DataExtension} |
||
458 | * and implemeting updateCMSFields(FieldList $fields) on that extension. |
||
459 | */ |
||
460 | public function getCMSFields() { |
||
476 | |||
477 | /** |
||
478 | * Get the children of this folder that are also folders. |
||
479 | * |
||
480 | * @return DataList |
||
481 | */ |
||
482 | public function ChildFolders() { |
||
485 | |||
486 | /** |
||
487 | * Get the number of children of this folder that are also folders. |
||
488 | */ |
||
489 | public function numChildFolders() { |
||
492 | /** |
||
493 | * @return String |
||
494 | */ |
||
495 | public function CMSTreeClasses() { |
||
508 | |||
509 | /** |
||
510 | * @return string |
||
511 | */ |
||
512 | public function getTreeTitle() { |
||
518 | } |
||
519 |