Revert "[TASK] Introduce DeprecationUtility and move methods"
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / ResourceUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Utility class for the File Abstraction Layer (aka subpackage Resource in EXT:core)
19 */
20 class ResourceUtility
21 {
22 /**
23 * This is a helper method that can be used with u?sort methods to sort a list of (relative) file paths, e.g.
24 * array("someDir/fileA", "fileA", "fileB", "anotherDir/fileA").
25 *
26 * Directories are sorted first in the lists, with the deepest structures first (while every level is sorted
27 * alphabetically)
28 *
29 * @param string $elementA
30 * @param string $elementB
31 * @return int
32 */
33 public static function recursiveFileListSortingHelper($elementA, $elementB)
34 {
35 if (strpos($elementA, '/') === false) {
36 // first element is a file
37 if (strpos($elementB, '/') === false) {
38 $result = self::nameCompareSortingHelper($elementA, $elementB);
39 } else {
40 // second element is a directory => always sort it first
41 $result = 1;
42 }
43 } else {
44 // first element is a directory
45 if (strpos($elementB, '/') === false) {
46 // second element is a file => always sort it last
47 $result = -1;
48 } else {
49 // both elements are directories => we have to recursively sort here
50 list($pathPartA, $elementA) = explode('/', $elementA, 2);
51 list($pathPartB, $elementB) = explode('/', $elementB, 2);
52
53 if ($pathPartA === $pathPartB) {
54 // same directory => sort by subpaths
55 $result = self::recursiveFileListSortingHelper($elementA, $elementB);
56 } else {
57 // different directories => sort by current directories
58 $result = self::nameCompareSortingHelper($pathPartA, $pathPartB);
59 }
60 }
61 }
62
63 return $result;
64 }
65
66 /**
67 * This is a helper method that can be used with u?sort methods to sort a list of names in natural order. With
68 * capitalized first if both equal in lowercase.
69 *
70 * @param string $elementA
71 * @param string $elementB
72 * @return int
73 */
74 public static function nameCompareSortingHelper($elementA, $elementB)
75 {
76 $result = strnatcasecmp($elementA, $elementB);
77 if ($result === 0) {
78 // Both are same in case insensitive so it's ok to check then now unnaturally.
79 $result = strcmp($elementA, $elementB);
80 }
81
82 return $result;
83 }
84 }