| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Churn\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Filesystem\Exception\InvalidArgumentException as FilesystemException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @internal | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | final class FileHelper | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param string $path The path of an item. | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @param string $basePath The absolute base path. | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @return string The absolute path of the given item. | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public static function toAbsolutePath(string $path, string $basePath): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         return (new Filesystem())->isAbsolutePath($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             ? $path | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |             : $basePath . '/' . $path; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $path The absolute path of an item. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $basePath The absolute base path. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @return string The relative path of the given item. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public static function toRelativePath(string $path, string $basePath): string | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             $relativePath = (new Filesystem())->makePathRelative($path, $basePath); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             return \rtrim($relativePath, '/\\'); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         } catch (FilesystemException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             return $path; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * Check whether the path is writable and create the missing folders if needed. | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @param string $filePath The file path to check. | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @throws InvalidArgumentException If the path is invalid. | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public static function ensureFileIsWritable(string $filePath): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if ('' === $filePath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             throw new InvalidArgumentException('Path cannot be empty'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (\is_dir($filePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             throw new InvalidArgumentException('Path cannot be a folder'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (!\is_file($filePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             (new Filesystem())->mkdir(\dirname($filePath)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (!\is_writable($filePath)) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |             throw new InvalidArgumentException('File is not writable'); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  |