Piwik\Filesystem::sortFilesDescByPathLength PHP 메소드

sortFilesDescByPathLength() 공개 정적인 메소드

Sort all given paths/filenames by its path length. Long path names will be listed first. This method can be useful if you have for instance a bunch of files/directories to delete. By sorting them by lengh you can make sure to delete all files within the folders before deleting the actual folder.
public static sortFilesDescByPathLength ( string[] $files ) : string[]
$files string[]
리턴 string[]
    public static function sortFilesDescByPathLength($files)
    {
        usort($files, function ($a, $b) {
            // sort by filename length so we kinda make sure to remove files before its directories
            if ($a == $b) {
                return 0;
            }
            return strlen($a) > strlen($b) ? -1 : 1;
        });
        return $files;
    }

Usage Example

예제 #1
0
 public function test_sortFilesDescByPathLength_shouldOrderDesc_IfDifferentLengthsGiven()
 {
     $input = array('xyz/1.gif', '1.gif', 'x', 'x/xyz.gif', 'xyz', 'xxyyzzgg', 'xyz/long.gif');
     $result = Filesystem::sortFilesDescByPathLength($input);
     $expected = array('xyz/long.gif', 'x/xyz.gif', 'xyz/1.gif', 'xxyyzzgg', '1.gif', 'xyz', 'x');
     $this->assertEquals($expected, $result);
 }