117 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| namespace nulib;
 | |
| 
 | |
| use nulib\file\FileReader;
 | |
| use nulib\file\FileWriter;
 | |
| use nulib\file\MemoryStream;
 | |
| use nulib\file\SharedFile;
 | |
| use nulib\file\TempStream;
 | |
| use nulib\file\TmpfileWriter;
 | |
| use nulib\os\path;
 | |
| 
 | |
| /**
 | |
|  * Class file: outils pour gérer les fichiers
 | |
|  */
 | |
| class file {
 | |
|   static function fix_dash($file) {
 | |
|     if ($file === "-") $file = null;
 | |
|     return $file;
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * si le fichier $file existe, le retourner.
 | |
|    * sinon, essayer avec l'une des extensions spécifiées, jusqu'à ce qu'un
 | |
|    * fichier soit trouvé.
 | |
|    * si aucun fichier n'est trouvé, retourner null
 | |
|    *
 | |
|    * par exemple, si le fichier "file.yaml" n'existe pas et que le fichier
 | |
|    * "file.yml" existe, alors <code>file::try("file.yaml", ".yml")</code>
 | |
|    * retourne "file.yml"
 | |
|    *
 | |
|    * @param string|array $exts
 | |
|    * @param string|array $replace_ext
 | |
|    */
 | |
|   static function try_ext(string $file, $exts=null, $replace_ext=null): ?string {
 | |
|     if (file_exists($file)) return $file;
 | |
|     if ($exts !== null) {
 | |
|       foreach (cl::with($exts) as $ext) {
 | |
|         $tmpfile = path::ensure_ext($file, $ext, $replace_ext);
 | |
|         if (file_exists($tmpfile)) return $tmpfile;
 | |
|       }
 | |
|     }
 | |
|     return null;
 | |
|   }
 | |
| 
 | |
|   static function reader($input, ?callable $func=null): FileReader {
 | |
|     $file = new FileReader(self::fix_dash($input));
 | |
|     if ($func !== null) {
 | |
|       try {
 | |
|         $func($file);
 | |
|       } finally {
 | |
|         $file->close();
 | |
|       }
 | |
|     }
 | |
|     return $file;
 | |
|   }
 | |
|   
 | |
|   static function writer($output, ?string $mode="w+b", ?callable $func=null): FileWriter {
 | |
|     $file = new FileWriter(self::fix_dash($output), $mode);
 | |
|     if ($func !== null) {
 | |
|       try {
 | |
|         $func($file);
 | |
|       } finally {
 | |
|         $file->close();
 | |
|       }
 | |
|     }
 | |
|     return $file;
 | |
|   }
 | |
| 
 | |
|   static function shared($file, ?callable $func=null): SharedFile {
 | |
|     $file  = new SharedFile($file);
 | |
|     if ($func !== null) {
 | |
|       try {
 | |
|         $func($file);
 | |
|       } finally {
 | |
|         $file ->close();
 | |
|       }
 | |
|     }
 | |
|     return $file;
 | |
|   }
 | |
| 
 | |
|   static function tmpwriter($destdir=null, ?callable $func=null): TmpfileWriter {
 | |
|     $file = new TmpfileWriter($destdir);
 | |
|     if ($func !== null) {
 | |
|       try {
 | |
|         $func($file);
 | |
|       } finally {
 | |
|         $file->close();
 | |
|       }
 | |
|     }
 | |
|     return $file;
 | |
|   }
 | |
| 
 | |
|   static function memory(?callable $func=null): MemoryStream {
 | |
|     $file = new MemoryStream();
 | |
|     if ($func !== null) {
 | |
|       try {
 | |
|         $func($file);
 | |
|       } finally {
 | |
|         $file->close();
 | |
|       }
 | |
|     }
 | |
|     return $file;
 | |
|   }
 | |
| 
 | |
|   static function temp(?callable $func=null): TempStream {
 | |
|     $file = new TempStream();
 | |
|     if ($func !== null) {
 | |
|       try {
 | |
|         $func($file);
 | |
|       } finally {
 | |
|         $file->close();
 | |
|       }
 | |
|     }
 | |
|     return $file;
 | |
|   }
 | |
| }
 |