Example #1
0
 public static function listAll($path, $pattern = "", $params = [])
 {
     $result = [];
     $path = RepoManager::resolve($path);
     if (!is_dir($path)) {
         return $result;
     }
     ## paging
     $pageSize = 25;
     $currentPage = 1;
     if (isset($params['paging'])) {
         $paging = $params['paging'];
         if (is_array($paging)) {
             $pageSize = $paging['pageSize'];
             $currentPage = $paging['currentPage'];
         }
     }
     $pageStart = ($currentPage - 1) * $pageSize;
     $pageEnd = $currentPage * $pageSize;
     ## order
     $order = [];
     if (is_array(@$params['order']) && isset($params['order']['order_by'][0])) {
         $order = @$params['order']['order_by'][0];
     }
     ## filtering
     $where = [];
     if (isset($params['where']) && is_array($params['where'])) {
         $where = $params['where'];
     }
     ## splitting
     $preparedPattern = RepoManager::preparePattern($pattern);
     ## listing dir
     if ($handle = opendir($path)) {
         $i = 0;
         $count = 0;
         while (false !== ($entry = readdir($handle))) {
             if ($entry != "." && $entry != "..") {
                 $isPatternMatch = false;
                 if ($pattern != "") {
                     $f = RepoManager::parseName($entry, $preparedPattern, false);
                     if (count($f) > 1) {
                         $isPatternMatch = true;
                     }
                 } else {
                     $f = [];
                     $f['file'] = $entry;
                     $isPatternMatch = true;
                 }
                 $isNameMatch = true;
                 if (count($where) > 0) {
                     $isNameMatch = RepoManager::isColumnMatch($f, $where);
                 }
                 if ($isPatternMatch && $isNameMatch) {
                     if (count($order) > 0) {
                         $colname = $f[$order['field']];
                         while (isset($result[$colname])) {
                             $colname .= "_";
                         }
                         $result[$colname] = $f;
                     } else {
                         if ($count >= $pageStart && $count < $pageEnd && $isPatternMatch) {
                             $result[] = $f;
                         }
                     }
                     $count++;
                 }
                 $i++;
             }
         }
         closedir($handle);
     }
     RepoManager::$fileCounts[md5($path . $pattern . json_encode($params))] = $count;
     if (count($order) > 0) {
         if ($order['direction'] == "asc") {
             ksort($result);
         } else {
             krsort($result);
         }
         $result = array_values($result);
         $result = array_slice($result, $pageStart, $pageSize);
     }
     return $result;
 }