1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HtaccessFirewall\Filesystem; |
4
|
|
|
|
5
|
|
|
use HtaccessFirewall\Filesystem\Exception\FileNotFoundException; |
6
|
|
|
use HtaccessFirewall\Filesystem\Exception\FileNotReadableException; |
7
|
|
|
use HtaccessFirewall\Filesystem\Exception\FileNotWritableException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Filesystem using PHP's built-in filesystem functions. |
11
|
|
|
*/ |
12
|
|
|
class BuiltInFilesystem implements Filesystem |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
private $writeLock; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Initialize BuiltInFilesystem. |
21
|
|
|
* |
22
|
|
|
* @param bool $writeLock whether to use a write lock (LOCK_EX). |
23
|
|
|
*/ |
24
|
|
|
public function __construct($writeLock = true) |
25
|
|
|
{ |
26
|
|
|
$this->writeLock = $writeLock; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Read a file into an array. |
31
|
|
|
* |
32
|
|
|
* @param string $file Path to the file. |
33
|
|
|
* |
34
|
|
|
* @throws FileNotFoundException |
35
|
|
|
* @throws FileNotReadableException |
36
|
|
|
* |
37
|
|
|
* @return string[] |
38
|
|
|
*/ |
39
|
|
|
public function read($file) |
40
|
|
|
{ |
41
|
|
|
if (!$this->exists($file)) { |
42
|
|
|
throw new FileNotFoundException(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!$this->readable($file)) { |
46
|
|
|
throw new FileNotReadableException(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return file($file, FILE_IGNORE_NEW_LINES); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Write an array to a file. |
54
|
|
|
* |
55
|
|
|
* @param string $file Path to the file. |
56
|
|
|
* @param string[] $lines Array of lines to write to the file. |
57
|
|
|
* |
58
|
|
|
* @throws FileNotFoundException |
59
|
|
|
* @throws FileNotWritableException |
60
|
|
|
*/ |
61
|
|
|
public function write($file, $lines) |
62
|
|
|
{ |
63
|
|
|
if (!$this->exists($file)) { |
64
|
|
|
throw new FileNotFoundException(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!$this->writable($file)) { |
68
|
|
|
throw new FileNotWritableException(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$contents = implode(PHP_EOL, $lines); |
72
|
|
|
file_put_contents($file, $contents, $this->writeLock ? LOCK_EX : 0); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check whether a file exists. |
77
|
|
|
* |
78
|
|
|
* @param string $file Path to the file. |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function exists($file) |
83
|
|
|
{ |
84
|
|
|
return file_exists($file); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check whether a file exists and is readable. |
89
|
|
|
* |
90
|
|
|
* @param string $file Path to the file. |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function readable($file) |
95
|
|
|
{ |
96
|
|
|
return is_readable($file); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Check whether a file exists and is writable. |
101
|
|
|
* |
102
|
|
|
* @param string $file Path to the file. |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function writable($file) |
107
|
|
|
{ |
108
|
|
|
return is_writable($file); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|