Example #1
0
File: Dir.php Project: mbcraft/piol
 /**
  * 
  * List all the elements inside this directory, excluding the ones that matches at least one of the specified patterns.
  * 
  * @param int $mode Only the following values, specified as constants inside this class, can be used :
  *  MODE_FILES_ONLY, MODE_FOLDERS_ONLY, MODE_FILES_AND_FOLDERS.
  * @param mixed $myExcludes an array of regexp patterns used to match excluded elements against their full name.
  * @param bool $deep if true searches inside all the contained folders, otherwise only in this one.
  * @return array an array of two arrays, with all the files that matched and all the directories that matched, 
  * as a \Mbcraft\Piol\File and \Mbcraft\Piol\Dir instances.
  * 
  * @api
  */
 public function listElements($mode = self::MODE_FILES_AND_FOLDERS, $myExcludes = self::DEFAULT_EXCLUDES, $deep = false)
 {
     $excludesSet = false;
     if (!$excludesSet && $myExcludes === self::NO_HIDDEN_FILES) {
         $excludesSet = true;
         $excludes = array(self::NO_HIDDEN_FILES_PATTERN);
     }
     if (!$excludesSet && $myExcludes === self::SHOW_HIDDEN_FILES) {
         $excludesSet = true;
         $excludes = array(self::SHOW_HIDDEN_FILES_PATTERN);
     }
     if (!isset($excludes) || !$excludesSet) {
         $excludes = $myExcludes;
     }
     $all_results = scandir($this->__full_path);
     $all_files = array();
     $all_dirs = array();
     foreach ($all_results as $element) {
         if ($element == "." || $element == "..") {
             //always skip . and ..
             continue;
         }
         $skip = false;
         foreach ($excludes as $pt) {
             if (preg_match($pt, $element)) {
                 $skip = true;
             }
         }
         $partial_path = $this->__path . $element;
         //รจ da saltare?
         if (!$skip) {
             if (($mode & self::MODE_FOLDERS_ONLY) === self::MODE_FOLDERS_ONLY && FileSystemUtils::isDir($partial_path)) {
                 $all_dirs[] = new Dir($partial_path);
             } else {
                 if (($mode & self::MODE_FILES_ONLY) === self::MODE_FILES_ONLY && FileSystemUtils::isFile($partial_path)) {
                     $all_files[] = new File($partial_path);
                     continue;
                 }
             }
         }
         if ($deep && FileSystemUtils::isDir($partial_path)) {
             $d = new Dir($partial_path);
             $partial_results = $d->listElements($mode, $myExcludes, true);
             $all_files = array_merge($all_files, $partial_results[0]);
             $all_dirs = array_merge($all_dirs, $partial_results[1]);
         }
     }
     return array($all_files, $all_dirs);
 }
Example #2
0
 /**
  * 
  * Returns the verified storage folder. If it does not exists, it is created.
  * 
  * @return \Mbcraft\Piol\Dir the directory, as a \Mbcraft\Piol\Dir instance, pointing to the verified storage.
  * @throws \Mbcraft\Piol\IOException if the storage root does not exists or is not valid.
  * 
  * @api
  */
 public static function getProtectedStorage()
 {
     $protected_storage_dir = new Dir(self::$storage_root);
     if (!$protected_storage_dir->exists()) {
         throw new IOException("The storage folder does not exist : " . self::$storage_root);
     }
     if (!$protected_storage_dir->isWritable()) {
         throw new IOException("The storage folder is not readable and writable : " . self::$storage_root);
     }
     $results = $protected_storage_dir->listElements();
     if (count($results[0]) == 0 && count($results[1]) <= 1) {
         if ($protected_storage_dir->isEmpty()) {
             $protected_storage_dir->newRandomSubdir();
         }
         return $protected_storage_dir->getUniqueSubdir();
     } else {
         throw new IOException("The storage root folder is invalid : it must contain at most just one folder.");
     }
 }