ISGArchiveDelegate.php
3 years ago
SGBGArchive.php
3 years ago
SGBGArchiveCdr.php
3 years ago
SGBGArchiveHelper.php
3 years ago
SGBGCache.php
3 years ago
SGBGCacheableFile.php
3 years ago
SGBGDirectoryTreeFile.php
3 years ago
SGBGFile.php
3 years ago
SGBGFileHelper.php
3 years ago
SGBGJsonFile.php
3 years ago
SGBGLog.php
3 years ago
SGBGReloader.php
3 years ago
SGBGStateFile.php
3 years ago
SGBGTask.php
3 years ago
SGBGDirectoryTreeFile.php
263 lines
| 1 | <?php |
| 2 | /* |
| 3 | @ class DirectoryTreeFile |
| 4 | @ version 1.1.0 |
| 5 | @ updated 12/02/2021 |
| 6 | */ |
| 7 | |
| 8 | require_once(__DIR__.'/SGBGCacheableFile.php'); |
| 9 | require_once(__DIR__.'/SGBGTask.php'); |
| 10 | |
| 11 | class SGBGDirectoryTreeFile extends SGBGCacheableFile |
| 12 | { |
| 13 | private $rootPath = ''; |
| 14 | private $filesCount = 0; |
| 15 | private $_excludePaths = array(); |
| 16 | private $_addPaths = array(); |
| 17 | private $dontExclude = array(); |
| 18 | |
| 19 | /** |
| 20 | * |
| 21 | * Get the root path from which the tree starts. |
| 22 | * |
| 23 | * @return string the root path |
| 24 | */ |
| 25 | public function getRootPath() |
| 26 | { |
| 27 | return $this->rootPath; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * |
| 32 | * Set the root path from which the tree should start. |
| 33 | * |
| 34 | * @param string $rootPath the root path |
| 35 | * @return null |
| 36 | */ |
| 37 | public function setRootPath($rootPath) |
| 38 | { |
| 39 | $this->rootPath = $rootPath; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | * Get the number of files and directories in the tree. |
| 45 | * |
| 46 | * @return int the number of files in the tree |
| 47 | */ |
| 48 | public function getFilesCount() |
| 49 | { |
| 50 | return $this->filesCount; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * |
| 55 | * Set the number of files and directories in the tree. |
| 56 | * |
| 57 | * @param int $filesCount the number of files in the tree |
| 58 | * @return null |
| 59 | */ |
| 60 | public function setFilesCount($filesCount) |
| 61 | { |
| 62 | $this->filesCount = $filesCount; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | public function addDontExclude($dontExclude) |
| 67 | { |
| 68 | $this->dontExclude[] = $dontExclude; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * |
| 75 | * Get Excluded File Paths; |
| 76 | * |
| 77 | * @return array Excluded File Paths |
| 78 | */ |
| 79 | public function getExcludedFilePaths() |
| 80 | { |
| 81 | return $this->_excludePaths; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * |
| 86 | * Set Excluded File Paths |
| 87 | * |
| 88 | * @param array $excludePaths excluded paths array |
| 89 | * @return null |
| 90 | */ |
| 91 | public function setExcludedFilePaths($excludePaths) |
| 92 | { |
| 93 | $this->_excludePaths = $excludePaths; |
| 94 | } |
| 95 | |
| 96 | public function getAddedFilePaths() |
| 97 | { |
| 98 | return $this->_addPaths; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | public function setAddedFilePaths($addPaths) |
| 103 | { |
| 104 | $this->_addPaths = $addPaths; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * |
| 109 | * Get recursive directory iterator. |
| 110 | * Set desired offset first. |
| 111 | * |
| 112 | * @param int $dirOffest directory iterator offset |
| 113 | * @return LimitIterator instance of LimitIterator |
| 114 | */ |
| 115 | private function getDirectoryIteratorAtOffset($dirOffest) |
| 116 | { |
| 117 | $directory = new \RecursiveDirectoryIterator( |
| 118 | $this->getRootPath(), |
| 119 | FilesystemIterator::FOLLOW_SYMLINKS|FilesystemIterator::SKIP_DOTS|FilesystemIterator::UNIX_PATHS |
| 120 | ); |
| 121 | |
| 122 | $iterator = new \RecursiveIteratorIterator( |
| 123 | $directory, |
| 124 | RecursiveIteratorIterator::SELF_FIRST, |
| 125 | RecursiveIteratorIterator::CATCH_GET_CHILD |
| 126 | ); |
| 127 | |
| 128 | $iterator = new \LimitIterator($iterator, $dirOffest); |
| 129 | |
| 130 | return $iterator; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * |
| 135 | * Get file at the specified index in the tree. |
| 136 | * First index starts from 0. |
| 137 | * |
| 138 | * @param int $index index to seek in the tree |
| 139 | * @return string file path |
| 140 | */ |
| 141 | public function getFileAtIndex($index) |
| 142 | { |
| 143 | $file = $this->getSplFileObject(); |
| 144 | $file->seek($index); |
| 145 | return trim($file->current()); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * |
| 150 | * Save tree file. |
| 151 | * Iterate recursively starting from the root path. |
| 152 | * Create a plain tree file where each row represents a directory or file. |
| 153 | * Rows are separated by a newline symbol (\n). |
| 154 | * |
| 155 | * @return null |
| 156 | */ |
| 157 | public function save() |
| 158 | { |
| 159 | if (empty($this->getRootPath())) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | $this->open('a'); |
| 164 | |
| 165 | $task = new SGBGTask(); |
| 166 | $task->prepare(SG_BACKUP_DIRECTORY.JBWP_DIRECTORY_STATE_FILE_NAME); |
| 167 | |
| 168 | //we consider this as a single operation, that's why we pass 1 as count |
| 169 | $task->start(1); |
| 170 | |
| 171 | $iterator = $this->getDirectoryIteratorAtOffset( |
| 172 | (int)$task->getParam('dir_offset') |
| 173 | ); |
| 174 | |
| 175 | $filesCount = 0; |
| 176 | |
| 177 | foreach ($iterator as $info) { |
| 178 | if ( $this->shouldExcludeFile($info->getPathname()) ) { |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | if ( !$this->shouldAddFile($info->getPathname()) ) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | $slash = $info->isDir() ? '/' : ''; |
| 187 | |
| 188 | if ($info->isDir() && !SGBGArchiveHelper::is_dir_empty($info)) { |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | $this->write($info->getPathname().$slash."\n"); |
| 193 | |
| 194 | $task->setParam('dir_offset', $iterator->getPosition() + 1); |
| 195 | $filesCount++; |
| 196 | $task->continueTask(function() { |
| 197 | //flush cache before exit |
| 198 | $this->getCache()->flush(); |
| 199 | $this->close(); |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | $this->setFilesCount((int)$filesCount); |
| 204 | |
| 205 | //use this just in case at the end, so all buffers are flushed |
| 206 | $this->getCache()->flush(); |
| 207 | |
| 208 | //increment state offset by one |
| 209 | $task->endChunk(); |
| 210 | |
| 211 | //finalize task, passing false doesn't remove the state file |
| 212 | $task->end(true); |
| 213 | |
| 214 | $this->close(); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | private function pathWithoutRootDirectory($path) |
| 219 | { |
| 220 | return substr($path, strlen( rtrim(SGConfig::get('SG_APP_ROOT_DIRECTORY'), '/') . '/')); |
| 221 | } |
| 222 | |
| 223 | private function shouldExcludeFile($path) |
| 224 | { |
| 225 | |
| 226 | if (in_array($path, $this->dontExclude)) { |
| 227 | return false; |
| 228 | } |
| 229 | //get the name of the file/directory removing the root directory |
| 230 | $file = $this->pathWithoutRootDirectory($path); |
| 231 | //check if file/directory must be excluded |
| 232 | foreach ($this->getExcludedFilePaths() as $exPath) { |
| 233 | $exPath = trim($exPath); |
| 234 | $exPath = trim($exPath, '/'); |
| 235 | if (strpos($file, $exPath) === 0) { |
| 236 | return true; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | private function shouldAddFile($path) |
| 244 | { |
| 245 | |
| 246 | if (in_array($path, $this->dontExclude)) { |
| 247 | return true; |
| 248 | } |
| 249 | //get the name of the file/directory removing the root directory |
| 250 | $file = $this->pathWithoutRootDirectory($path); |
| 251 | //check if file/directory must be excluded |
| 252 | foreach ($this->getAddedFilePaths() as $addPath) { |
| 253 | $addPath = trim($addPath); |
| 254 | $addPath = trim($addPath, '/'); |
| 255 | if (strpos($file, $addPath) === 0) { |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 |