[BUGFIX] LocalDriver: Recursive file listing is broken
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / ResourceUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 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 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Utility class for the File Abstraction Layer (aka subpackage Resource in EXT:core)
29 */
30 class ResourceUtility {
31 /**
32 * This is a helper method that can be used with u?sort methods to sort a list of (relative) file paths, e.g.
33 * array("someDir/fileA", "fileA", "fileB", "anotherDir/fileA").
34 *
35 * Directories are sorted first in the lists, with the deepest structures first (while every level is sorted
36 * alphabetically)
37 *
38 * @param string $elementA
39 * @param string $elementB
40 * @return int
41 */
42 public static function recursiveFileListSortingHelper($elementA, $elementB) {
43 if (strpos($elementA, '/') === FALSE) {
44 // first element is a file
45 if (strpos($elementB, '/') === FALSE) {
46 $result = strnatcasecmp($elementA, $elementB);
47 } else {
48 // second element is a directory => always sort it first
49 $result = 1;
50 }
51 } else {
52 // first element is a directory
53 if (strpos($elementB, '/') === FALSE) {
54 // second element is a file => always sort it last
55 $result = -1;
56 } else {
57 // both elements are directories => we have to recursively sort here
58 list($pathPartA, $elementA) = explode('/', $elementA, 2);
59 list($pathPartB, $elementB) = explode('/', $elementB, 2);
60
61 if ($pathPartA === $pathPartB) {
62 // same directory => sort by subpaths
63 $result = self::recursiveFileListSortingHelper($elementA, $elementB);
64 } else {
65 // different directories => sort by current directories
66 $result = strnatcasecmp($pathPartA, $pathPartB);
67 }
68 }
69 }
70
71 return $result;
72 }
73 }
74
75 ?>