1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\File;
14:
15: use Pry\File\Decorator\Filter;
16: use Pry\File\Util;
17: use Pry\Util\Strings;
18:
19: 20: 21: 22: 23: 24: 25: 26:
27: class FolderManager
28: {
29:
30: private $dossier;
31: private $dossierRecur;
32: private $listeFichier;
33: private $listeDossier;
34: private $filtre;
35:
36: 37: 38: 39: 40:
41: public function __construct($path)
42: {
43: $this->dossier = $path;
44: if (!is_dir($this->dossier))
45: throw new \InvalidArgumentException('Le dossier recherché ne semble pas exister');
46: $this->dossierRecur = $this->dossier;
47: $this->listeFichier = array();
48: $this->folderTree = array();
49: $this->fileTree = array();
50: $this->tree = '';
51: $this->filtre = array();
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function listFile($humanSize = false)
61: {
62: $folder = new Filter(new \DirectoryIterator($this->dossier));
63: $folder->setExtension($this->filtre);
64: $compteur = 0;
65: foreach ($folder as $file) {
66: if (!$file->isDot() && !$file->isDir())
67: {
68: $this->listeFichier[$compteur]['name'] = $file->getFilename();
69: if ($humanSize)
70: {
71: $taille = $file->getSize();
72: $this->listeFichier[$compteur]['size'] = Util::getHumanSize($taille);
73: }
74: else
75: $this->listeFichier[$compteur]['size'] = round($file->getSize() / 1024, 3);
76: $this->listeFichier[$compteur]['type'] = Util::getExtension($this->listeFichier[$compteur]['name']);
77: $compteur++;
78: }
79: }
80: sort($this->listeFichier);
81: return $this->listeFichier;
82: }
83:
84: 85: 86: 87: 88: 89:
90: public function listRecursive()
91: {
92: $folder = new Filter(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dossier), true));
93: $folder->setExtension($this->filtre);
94: $liste = array();
95: $compteur = 0;
96: foreach ($folder as $file) {
97: $liste[$compteur]['name'] = $file->getFilename();
98: $liste[$compteur]['size'] = round($file->getSize() / 1024, 3);
99: $liste[$compteur]['depth'] = $folder->getDepth();
100: $compteur++;
101:
102: }
103: return $liste;
104: }
105:
106: 107: 108: 109: 110: 111: 112: 113: 114:
115: public function getLastFiles($nbFile = 5, $humanSize = false)
116: {
117: $dossier = new Filter(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dossier), true));
118: $dossier->setExtension($this->filtre);
119: $liste = array();
120: $compteur = 0;
121: foreach ($dossier as $file) {
122: if (!$file->isDir())
123: {
124:
125: $liste[$compteur]['name'] = $file->getFilename();
126: $liste[$compteur]['date'] = $file->getMTime();
127: $liste[$compteur]['path'] = $file->getPathName();
128:
129: $size = $file->getSize();
130: if ($humanSize)
131: $liste[$compteur]['size'] = Util::getHumanSize($size);
132: else
133: $liste[$compteur]['size'] = round($size / 1024, 3);
134:
135:
136: }
137: $compteur++;
138: }
139: usort($liste, array('Pry\File\FolderManager', 'triParDate'));
140: array_splice($liste, $nbFile);
141: return array_reverse($liste);
142: }
143:
144: private static function triParDate($a, $b)
145: {
146: if ($a['date'] == $b['date'])
147: return 0;
148: return ($a['date'] > $b['date']) ? -1 : 1;
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function getSize()
158: {
159: $dossier = new Filter(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dossier), true));
160: $dossier->setExtension($this->filtre);
161: $size = 0;
162: foreach ($dossier as $file)
163: if (!$file->isDir())
164: $size+=round($file->getSize() / 1024, 3);
165:
166: return $size;
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176: 177:
178: public static function getSizeStatic($folder)
179: {
180: if (!is_dir($folder))
181: throw new \InvalidArgumentException('Le dossier ne semble pas exister');
182: $dossier = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder), true);
183: $size = 0;
184: foreach ($dossier as $file)
185: if (!$file->isDir())
186: $size+=round($file->getSize() / 1024, 3);
187:
188: return $size;
189: }
190:
191: 192: 193: 194: 195:
196: public function liste()
197: {
198: $folder = new \DirectoryIterator($this->dossier);
199: $compteur = 0;
200: foreach ($folder as $file) {
201: if (!$file->isDot() && $file->isDir())
202: {
203: $this->listeDossier[$compteur] = $file->getFilename();
204: $compteur++;
205: }
206: }
207: return $this->listeDossier;
208: }
209:
210: 211: 212: 213: 214:
215: public function setFilter($filter)
216: {
217: if (!is_array($filter))
218: $this->filtre[] = $filter;
219: else
220: $this->filtre = array_merge($this->filtre, $filter);
221: }
222:
223: 224: 225: 226: 227:
228: public function rmFilter($filter)
229: {
230: $cle = array_search($filter, $this->filtre);
231: if ($cle != FALSE)
232: unset($this->filtre[$cle]);
233: else
234: throw new \InvalidArgumentException('Le filtre demandé n\'existe pas');
235: }
236:
237: 238: 239: 240: 241: 242:
243: public static function create($newDir, $chmod = 0644)
244: {
245: if (!is_file($newDir))
246: mkdir(Strings::clean($newDir), $chmod);
247: }
248:
249: 250: 251: 252: 253:
254: public function remove($dir = '')
255: {
256: if ($dir == '')
257: $dir = $this->dossier;
258:
259: $folder = new \RecursiveDirectoryIterator($dir);
260: while ($folder->valid()) {
261: if ($folder->isDir() && !$folder->isDot())
262: {
263: if ($folder->hasChildren())
264: self::remove($folder->getPathName());
265: }
266: elseif ($folder->isFile() && !$folder->isDot())
267: {
268: unlink($folder->getPathname());
269: }
270: $folder->next();
271: }
272: $folder = null;
273: rmdir($dir);
274:
275: }
276:
277: 278: 279: 280:
281: public function removeFiles()
282: {
283:
284: $folder = new Filter(new \DirectoryIterator($this->dossier));
285: $folder->setExtension($this->filtre);
286: foreach ($folder as $file) {
287: if ($file->isFile() && !$file->isDot())
288: {
289: unlink($file->getPathname());
290: }
291: }
292: $folder = null;
293: }
294:
295: 296: 297: 298: 299: 300: 301:
302: public function copy($dest, $dir = '')
303: {
304: if (empty($dest))
305: throw new \UnexpectedValueException('Le dossier de destination ne peut être vide');
306: if (!is_dir($dest))
307: self::create($dest, 0777);
308:
309: if (empty($dir))
310: $dir = $this->dossier;
311:
312: $folder = new \RecursiveDirectoryIterator($dir);
313: while ($folder->valid()) {
314: if ($folder->isDir() && !$folder->isDot())
315: {
316: if ($folder->hasChildren())
317: {
318: $tmpPath = $dest . $folder->getFileName();
319: if (!is_dir($tmpPath))
320: self::create($tmpPath, 0777);
321: $this->copy($dest, $folder->getPathname());
322: }
323: }
324: elseif ($folder->isFile() && !$folder->isDot())
325: {
326: $src = $folder->getPathname();
327:
328: $pos = stripos($src, DIRECTORY_SEPARATOR);
329: copy($src, $dest . substr($src, $pos + 1));
330: }
331: $folder->next();
332: }
333: }
334:
335: 336: 337: 338: 339: 340:
341: public function ZipFiles($fileName)
342: {
343: if (extension_loaded('zip'))
344: {
345: $this->listFile();
346: $zip = new \ZipArchive;
347: $res = $zip->open($this->dossier . $fileName, \ZipArchive::CREATE);
348: if ($res === TRUE)
349: {
350: foreach ($this->listeFichier as $file) {
351: $zip->addFile($this->dossier . $file['name'], $file['name']);
352: }
353: $zip->close();
354: }
355: else
356: {
357: throw new \RuntimeException('Impossible de créer le zip');
358: }
359: }
360: else
361: {
362: throw new \RuntimeException('Extension Zip non activée. Impossible de compresser les fichiers');
363: }
364: }
365:
366: }