[TASK] Re-work/simplify copyright header in PHP files - Part 3
[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 * This is a helper method that can be used with u?sort methods to sort a list of (relative) file paths, e.g.
23 * array("someDir/fileA", "fileA", "fileB", "anotherDir/fileA").
24 *
25 * Directories are sorted first in the lists, with the deepest structures first (while every level is sorted
26 * alphabetically)
27 *
28 * @param string $elementA
29 * @param string $elementB
30 * @return int
31 */
32 public static function recursiveFileListSortingHelper($elementA, $elementB) {
33 if (strpos($elementA, '/') === FALSE) {
34 // first element is a file
35 if (strpos($elementB, '/') === FALSE) {
36 $result = strnatcasecmp($elementA, $elementB);
37 } else {
38 // second element is a directory => always sort it first
39 $result = 1;
40 }
41 } else {
42 // first element is a directory
43 if (strpos($elementB, '/') === FALSE) {
44 // second element is a file => always sort it last
45 $result = -1;
46 } else {
47 // both elements are directories => we have to recursively sort here
48 list($pathPartA, $elementA) = explode('/', $elementA, 2);
49 list($pathPartB, $elementB) = explode('/', $elementB, 2);
50
51 if ($pathPartA === $pathPartB) {
52 // same directory => sort by subpaths
53 $result = self::recursiveFileListSortingHelper($elementA, $elementB);
54 } else {
55 // different directories => sort by current directories
56 $result = strnatcasecmp($pathPartA, $pathPartB);
57 }
58 }
59 }
60
61 return $result;
62 }
63 }