blob: f17154dd09c0b6f19ba411c346e864b2fcd3ddfe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
<?php
namespace SMW\Utils;
use RuntimeException;
use SMW\Exception\FileNotWritableException;
/**
* @license GNU GPL v2+
* @since 3.0
*
* @author mwjames
*/
class File {
/**
* @since 3.1
*
* @param string $file
*
* @return string
*/
public static function dir( $file ) {
return str_replace( [ '\\', '//', '/' ], DIRECTORY_SEPARATOR, $file );
}
/**
* @since 3.0
*
* @param string $file
* @param string $content
* @param integer $flags
*/
public function write( $file, $contents, $flags = 0 ) {
$file = self::dir( $file );
if ( !is_writable( dirname( $file ) ) ) {
throw new FileNotWritableException( "$file" );
}
file_put_contents( $file, $contents, $flags );
}
/**
* @since 3.0
*
* @param string $file
*
* @return boolean
*/
public function exists( $file ) {
return file_exists( $file );
}
/**
* @since 3.0
*
* @param string $file
* @param integer|null $checkSum
*
* @return string
* @throws RuntimeException
*/
public function read( $file, $checkSum = null ) {
if ( !is_readable( $file ) ) {
throw new RuntimeException( "$file is not readable." );
}
if ( $checkSum !== null && $this->getCheckSum( $file ) !== $checkSum ) {
throw new RuntimeException( "Processing of $file failed with a checkSum error." );
}
return file_get_contents( $file );
}
/**
* @since 3.0
*
* @param string $file
*/
public function delete( $file ) {
@unlink( $file );
}
/**
* @since 3.0
*
* @param string $file
*
* @return integer
*/
public function getCheckSum( $file ) {
return md5_file( $file );
}
}
|