1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11: 
 12: 
 13: namespace Pry\File;
 14: 
 15: use Pry\File\Util;
 16: 
 17:  18:  19:  20:  21:  22:  23:  24: 
 25: class FileManager
 26: {
 27: 
 28:     const READ             = 'r';
 29:     const READ_BYTE        = 'rb';
 30:     const READ_WRITE       = 'r+';
 31:     const WRITE            = 'w';
 32:     const WRITE_BYTE       = 'wb';
 33:     const READ_WRITE_RESET = 'w+';
 34:     const ADD              = 'a';
 35:     const READ_WRITE_ADD   = 'a+';
 36: 
 37:      38:  39:  40:  41: 
 42:     private $fileHandler = null;
 43: 
 44:      45:  46:  47: 
 48:     private $fileInfo;
 49: 
 50:      51:  52:  53:  54: 
 55:     protected $pathToFile;
 56: 
 57:      58:  59:  60:  61: 
 62:     public $overwrite;
 63: 
 64:      65:  66:  67:  68: 
 69:     public function __construct($file)
 70:     {
 71:         $this->pathToFile = $file;
 72:         $this->fileInfo   = new \SplFileInfo($this->pathToFile);
 73:         $this->overwrite  = true;
 74:     }
 75: 
 76:      77:  78:  79:  80: 
 81:     public function isFile()
 82:     {
 83:         if ($this->fileInfo->isFile())
 84:             return true;
 85:         else
 86:             return false;
 87:     }
 88: 
 89:      90:  91:  92:  93: 
 94:     public function getInfo()
 95:     {
 96:         $arrInfo = array();
 97:         if ($this->fileInfo->isFile())
 98:         {
 99:             $arrInfo['extension'] = Util::getExtension($this->pathToFile);
100:             $arrInfo['name']      = Util::getNameFromPath($this->pathToFile);
101:             $arrInfo['size']      = $this->fileInfo->getSize();
102:             $arrInfo['type']      = 'file';
103:         }
104: 
105:         return $arrInfo;
106:     }
107: 
108:     109: 110: 111: 
112:     public function getFileInfo()
113:     {
114:         return $this->fileInfo;
115:     }
116: 
117:     118: 119: 120: 121: 122: 
123:     public function open($mode)
124:     {
125:         if (file_exists($this->pathToFile) || ($mode != self::READ && $mode != self::READ_WRITE))
126:         {
127:             if ($this->isFile() || ($mode != self::READ && $mode != self::READ_WRITE))
128:             {
129:                 try {
130:                     $this->fileHandler = $this->fileInfo->openFile($mode);
131:                     if ($this->fileHandler != null)
132:                         return true;
133:                 } catch (\RuntimeException $e) {
134:                     return false;
135:                 }
136: 
137:                 return false;
138:             }
139:             else
140:             {
141:                 throw new \InvalidArgumentException($this->pathToFile . ' n\'est pas un fichier valide');
142:             }
143:         }
144:         else
145:         {
146:             throw new \InvalidArgumentException('Le fichier ' . $this->pathToFile . ' n\'existe pas');
147:         }
148:     }
149: 
150:     151: 152: 153: 154: 
155:     public function close()
156:     {
157:         if ($this->fileHandler != null)
158:         {
159:             $this->fileHandler = null;
160:             return true;
161:         }
162:         return false;
163:     }
164: 
165:     166: 167: 168: 169: 
170:     public function read()
171:     {
172:         $datas = "";
173:         if ($this->fileHandler == null)
174:             $this->open(self::READ);
175:         if ($this->fileInfo->isReadable())
176:         {
177:             $this->fileHandler->rewind();
178:             while (!$this->fileHandler->eof())
179:                 $datas .= $this->fileHandler->fgets();
180: 
181:             return $datas;
182:         }
183: 
184:         return false;
185:     }
186: 
187:     188: 189: 190: 191: 192: 
193:     public function readLine($line2read = null)
194:     {
195:         if ($this->fileHandler == null)
196:             $this->open(self::READ);
197: 
198:         $this->fileHandler->rewind();
199: 
200:         if ($line2read == null)
201:         {
202:             $arrLine = array();
203:             foreach ($this->fileHandler as $lineNumber => $content)
204:                 $arrLine[$lineNumber] = $content;
205: 
206:             return $arrLine;
207:         }
208:         else
209:         {
210:             $line2read -= 1; 
211:             $this->fileHandler->seek($line2read);
212:             return $this->fileHandler->current();
213:         }
214:         return false;
215:     }
216: 
217:     218: 219: 220: 221: 222: 
223:     public function search($toSearch)
224:     {
225:         $search = $this->read();
226:         return stripos($search, $toSearch);
227:     }
228: 
229:     230: 231: 232: 233: 234: 
235:     public function write($contenu)
236:     {
237:         if ($this->fileHandler != null && $this->fileInfo->isWritable())
238:         {
239:             return $this->fileHandler->fwrite($contenu);
240:         }
241:         return false;
242:     }
243: 
244:     245: 246: 247: 248: 249: 250: 
251:     public function writeLine($line, $endLine = "\n")
252:     {
253:         if ($this->fileHandler != null && $this->fileInfo->isWritable())
254:         {
255:             return $this->fileHandler->fwrite($line . $endLine);
256:         }
257:         return false;
258:     }
259: 
260:     261: 262: 263: 264: 265: 
266:     public function insertLine($line, $numLineInsert)
267:     {
268:         $lignes = $this->readLine();
269: 
270:         if ($lignes && $this->fileHandler != null && $this->fileInfo->isWritable())
271:         {
272:             array_splice($lignes, $numLineInsert, 0, $line . "\n");
273:             file_put_contents($this->pathToFile, join("", $lignes));
274:         }
275:     }
276: 
277:     278: 279: 280: 281: 282: 283: 
284:     public function copy($pathToCopy)
285:     {
286:         $pathToCopy = $this->preparePath($pathToCopy, 'copy');
287:         if (!$this->overwrite)
288:         {
289:             if (!file_exists($pathToCopy))
290:             {
291:                 return false;
292:             }
293:             else
294:             {
295:                 return copy($this->pathToFile, $pathToCopy);
296:             }
297:         }
298:         else
299:         {
300:             return copy($this->pathToFile, $pathToCopy);
301:         }
302:     }
303: 
304:     305: 306: 307: 308: 309: 
310:     public function move($pathToMove)
311:     {
312:         $pathToMove = $this->preparePath($pathToMove, 'copy');
313:         if (!$this->overwrite)
314:         {
315:             if (file_exists($pathToMove))
316:             {
317:                 return false;
318:             }
319:             else
320:             {
321:                 return rename($this->pathToFile, $pathToMove);
322:             }
323:         }
324:         else
325:         {
326:             if (file_exists($pathToMove))
327:                 unlink($pathToMove);
328:             return rename($this->pathToFile, $pathToMove);
329:         }
330:     }
331: 
332:     333: 334: 
335:     public function delete()
336:     {
337:         $this->close();
338:         @unlink($this->pathToFile);
339:     }
340: 
341:     342: 343: 344: 345: 346: 347: 
348:     private function preparePath($path, $mode)
349:     {
350:         $path = trim($path);
351:         switch ($mode)
352:         {
353:             case 'copy' :
354:                 if ($path[0] == '/')
355:                 {
356:                     echo $path = substr($path, 1);
357:                 }
358:                 break;
359:         }
360:         return $path;
361:     }
362: 
363:     364: 365: 366: 367: 
368:     public function append($data)
369:     {
370:         if ($this->fileHandler == null)
371:             return file_put_contents($this->pathToFile, $data, FILE_APPEND);
372:         else
373:             throw new \Exception('Fichier déjà ouvert, utilisez write()');
374:     }
375: 
376:     377: 378: 379: 380: 
381:     public function changePermission($octal)
382:     {
383:         chmod($this->pathToFile, $octal);
384:     }
385: 
386:     387: 388: 389: 390: 391: 
392:     public function getLMTime($dateType = null)
393:     {
394:         $time = $this->fileInfo->getMTime();
395:         if ($time)
396:         {
397:             if ($dateType)
398:                 return date($dateType, $time);
399:             return $time;
400:         }
401:         return false;
402:     }
403: 
404:     public function __destruct()
405:     {
406:         $this->close();
407:     }
408: 
409: }
410: 
411: ?>