public function explore($sPath, $iOrderField = OrderField::NONE, $iOrderDirection = OrderDirection::ASC, array $aAllowedExtensions = [], array $aAllowedBasenamePatterns = []) { // Initialize $aFiles = []; // Get files $aList = scandir($sPath); if (!$aList) { throw new RuntimeException(sprintf('Error while exploring %s', $sPath)); } // Add file foreach ($aList as $sBasename) { // Create file $oFile = $this->metadata(sprintf('%s/%s', $sPath, $sBasename)); // Add file Toolbox::addFile($aFiles, $oFile, $aAllowedExtensions, $aAllowedBasenamePatterns); } // Order Toolbox::sortFiles($aFiles, $iOrderField, $iOrderDirection); // Return return $aFiles; }
public function testSortFiles() { // Initialize $aFiles = [new File('/tmp/path1', 202, DateTime::createFromFormat('Y-m-d', '2016-02-19')), new File('/tmp/path2', 200, DateTime::createFromFormat('Y-m-d', '2016-02-18')), new File('/tmp/path3', 205, DateTime::createFromFormat('Y-m-d', '2016-02-19'))]; // No sorting Toolbox::sortFiles($aFiles, OrderField::NONE); // Assert $this->assertEquals('path1', $aFiles[0]->getBasename()); $this->assertEquals('path2', $aFiles[1]->getBasename()); $this->assertEquals('path3', $aFiles[2]->getBasename()); // Sorting by modification date Toolbox::sortFiles($aFiles, OrderField::MODIFICATION_DATE); // Assert $this->assertEquals('path1', $aFiles[1]->getBasename()); $this->assertEquals('path2', $aFiles[0]->getBasename()); $this->assertEquals('path3', $aFiles[2]->getBasename()); // Sorting DESC Toolbox::sortFiles($aFiles, OrderField::SIZE, OrderDirection::DESC); // Assert $this->assertEquals('path1', $aFiles[1]->getBasename()); $this->assertEquals('path2', $aFiles[2]->getBasename()); $this->assertEquals('path3', $aFiles[0]->getBasename()); }
public function explore($sPath, $iOrderField = OrderField::NONE, $iOrderDirection = OrderDirection::ASC, array $aAllowedExtensions = [], array $aAllowedBasenamePatterns = []) { // Initialize $aFiles = []; // Get files $aList = $this->exec(sprintf('ls -l \'%s\'', $this->escapeSingleQuotes($sPath))); // Remove total if (isset($aList[0]) && preg_match('/^total[\\s]+[0-9]+/', $aList[0]) > 0) { array_shift($aList); } // Add file foreach ($aList as $sFile) { // Initialize $oFile = Toolbox::parseRawList($sFile, $sPath); // Add file Toolbox::addFile($aFiles, $oFile, $aAllowedExtensions, $aAllowedBasenamePatterns); } // Order Toolbox::sortFiles($aFiles, $iOrderField, $iOrderDirection); // Return return $aFiles; }
public function explore($sPath, $iOrderField = OrderField::NONE, $iOrderDirection = OrderDirection::ASC, array $aAllowedExtensions = [], array $aAllowedBasenamePatterns = []) { // Initialize $aFiles = []; // Get CURL $oCurl = $this->curlInit(); // Execute CURL $sResponse = $this->curlExec($oCurl, $sPath, ObjectType::DIRECTORY, 'LIST -a'); // Get files $aList = explode("\n", $sResponse); // Add file foreach ($aList as $sFile) { if ($sFile !== '') { // Initialize $oFile = Toolbox::parseRawList($sFile, $sPath); // Add file Toolbox::addFile($aFiles, $oFile, $aAllowedExtensions, $aAllowedBasenamePatterns); } } // Order Toolbox::sortFiles($aFiles, $iOrderField, $iOrderDirection); // Return return $aFiles; }