|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vendi\Shared; |
|
4
|
|
|
|
|
5
|
|
|
final class fs_utils |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
const CREATE_DIRECTORY_IF_NOT_EXISTS_OR_FAIL = true; |
|
9
|
|
|
|
|
10
|
|
|
const OK_IF_PATH_DOES_NOT_EXIST = false; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* [create_random_temp_dir description] |
|
14
|
|
|
* @param string $prefix [description] |
|
15
|
|
|
* @param callable|null $rand_func [description] |
|
16
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
17
|
|
|
*/ |
|
18
|
3 |
|
public static function create_random_temp_dir( string $prefix, callable $rand_func = null ) : string |
|
19
|
|
|
{ |
|
20
|
3 |
|
if( ! $prefix ) |
|
21
|
|
|
{ |
|
22
|
1 |
|
throw new utils_exception( 'You must provide a prefix when calling create_random_temp_dir()' ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
$prefix = trim( $prefix, '_' ) . '_'; |
|
26
|
|
|
|
|
27
|
2 |
|
if( ! $rand_func ) |
|
28
|
|
|
{ |
|
29
|
1 |
|
$rand_func = function( $prefix ) |
|
30
|
|
|
{ |
|
31
|
1 |
|
return uniqid( $prefix, true ); |
|
32
|
1 |
|
}; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
$tmp_folder = sys_get_temp_dir(); |
|
36
|
2 |
|
while( true ) |
|
37
|
|
|
{ |
|
38
|
2 |
|
$sub_folder = $rand_func( $prefix ); |
|
39
|
|
|
|
|
40
|
|
|
//Normally we'd want combine_paths to make the directory for us but |
|
41
|
|
|
//in this case we only want to return if it doesn't already exist. |
|
42
|
2 |
|
$full_path = self::combine_paths( self::OK_IF_PATH_DOES_NOT_EXIST, $tmp_folder, $sub_folder ); |
|
43
|
|
|
|
|
44
|
|
|
//The directory exists, loop again |
|
45
|
2 |
|
if( is_dir( $full_path ) ) |
|
46
|
|
|
{ |
|
47
|
1 |
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
//Make the directory. |
|
51
|
|
|
//NOTE: This method throws if it cannot create the directory. |
|
52
|
2 |
|
self::mkdir( $full_path ); |
|
53
|
|
|
|
|
54
|
2 |
|
break; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
//Return this outside of the while loop so that code coverage can handle |
|
58
|
|
|
//the full function |
|
59
|
2 |
|
return $full_path; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
7 |
|
public static function combine_paths_with_file( bool $create, string $file, string ...$path_parts ) |
|
63
|
|
|
{ |
|
64
|
7 |
|
$ret = '/'; |
|
65
|
|
|
|
|
66
|
7 |
|
foreach( $path_parts as $pp ) |
|
67
|
|
|
{ |
|
68
|
6 |
|
$ret .= trim( $pp, '/\\' ) . '/'; |
|
69
|
|
|
|
|
70
|
6 |
|
if( $create ) |
|
71
|
|
|
{ |
|
72
|
6 |
|
self::mkdir( $ret ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
7 |
|
if( $file ) |
|
77
|
|
|
{ |
|
78
|
7 |
|
$ret .= trim( $file, '/\\' ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
7 |
|
return $ret; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
7 |
|
public static function combine_paths( bool $create, string ...$path_parts ) |
|
85
|
|
|
{ |
|
86
|
7 |
|
return self::combine_paths_with_file( $create, '', ...$path_parts ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Create the given path if it doesn't exist. |
|
91
|
|
|
* |
|
92
|
|
|
* NOTE: This method does not test user permissions to write to the |
|
93
|
|
|
* directory, that must be handled by callers. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $path The absolute path |
|
96
|
|
|
* @param string $context An optional context to help explain the mkdir |
|
|
|
|
|
|
97
|
|
|
* conditions for debugging. |
|
98
|
|
|
* @return true Returns true if successful, otherwise an |
|
|
|
|
|
|
99
|
|
|
* exception is thrown. |
|
100
|
|
|
*/ |
|
101
|
7 |
|
public static function mkdir( string $path, string $context = null ) : bool |
|
102
|
|
|
{ |
|
103
|
|
|
//Make sure we've got something to work with. |
|
104
|
7 |
|
if( ! $path || ! trim( $path ) ) |
|
105
|
|
|
{ |
|
106
|
2 |
|
throw new utils_exception( 'You must provide a path when calling mkdir.' ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
//Make sure the path is absolute |
|
110
|
5 |
|
if( 0 !== strpos( $path, '/' ) ) |
|
111
|
|
|
{ |
|
112
|
1 |
|
throw new utils_exception( 'You must provide an absolute path when calling mkdir.' ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
//Does the directory already exist? |
|
116
|
4 |
|
if( is_dir( $path ) ) |
|
117
|
|
|
{ |
|
118
|
1 |
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
//Try to make it |
|
122
|
3 |
|
@mkdir( $path ); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
//mkdir fails if the directory already exists however if this |
|
125
|
|
|
//command is executed simultaneously by two people there could be a |
|
126
|
|
|
//race condition. So before failing hard, see if someone else |
|
127
|
|
|
//actually succeeded in making in. |
|
128
|
3 |
|
if( is_dir( $path ) ) |
|
129
|
|
|
{ |
|
130
|
1 |
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
if( $context ) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
1 |
|
$msg = sprintf( 'Cannot create directory %1$s for context %2$s.', $path, $context ); |
|
136
|
|
|
} |
|
137
|
|
|
else |
|
138
|
|
|
{ |
|
139
|
1 |
|
$msg = sprintf( 'Cannot create directory %1$s.', $path, $context ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
throw new utils_exception( $msg ); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.