1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simplario\Checker\Checker; |
4
|
|
|
|
5
|
|
|
use Simplario\Checker\ResultException\FailException; |
6
|
|
|
use Simplario\Checker\ResultException\SuccessException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Filesystem |
10
|
|
|
* |
11
|
|
|
* @package Simplario\Checker\Checker |
12
|
|
|
*/ |
13
|
|
|
class Filesystem extends AbstractChecker |
14
|
4 |
|
{ |
15
|
|
|
/** |
16
|
4 |
|
* @var string |
17
|
|
|
*/ |
18
|
4 |
|
protected $target = 'path'; |
19
|
3 |
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $path |
22
|
3 |
|
* @param boolean $expectExists |
23
|
|
|
* @param array $task |
24
|
3 |
|
* |
25
|
|
|
* @throws FailException |
26
|
|
|
* @throws SuccessException |
27
|
2 |
|
*/ |
28
|
|
|
protected function testExists($path, $expectExists, array $task) |
29
|
2 |
|
{ |
30
|
1 |
|
$exists = is_file($path) || is_dir($path); |
31
|
|
|
|
32
|
|
|
if ($exists === $expectExists) { |
33
|
1 |
|
throw new SuccessException('Ok', $task); |
34
|
|
|
} |
35
|
1 |
|
|
36
|
|
|
$msg = $expectExists ? "Path '{$path}' is not exists'" : "Path '{$path}' is exists"; |
37
|
|
|
|
38
|
|
|
throw new FailException($msg, $task); |
39
|
2 |
|
} |
40
|
|
|
|
41
|
2 |
|
/** |
42
|
1 |
|
* @param string $path |
43
|
|
|
* @param boolean $expectFile |
44
|
|
|
* @param array $task |
45
|
1 |
|
* |
46
|
|
|
* @throws FailException |
47
|
1 |
|
* @throws SuccessException |
48
|
|
|
*/ |
49
|
|
|
protected function testFile($path, $expectFile, array $task) |
50
|
2 |
|
{ |
51
|
|
|
if (is_file($path) === $expectFile) { |
52
|
2 |
|
throw new SuccessException('Ok', $task); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
$msg = $expectFile ? "Path '{$path}' is not a file'" : "Path '{$path}' is a file"; |
56
|
1 |
|
|
57
|
|
|
throw new FailException($msg, $task); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
2 |
|
* @param string $path |
62
|
|
|
* @param boolean $expectFolder |
63
|
2 |
|
* @param array $task |
64
|
1 |
|
* |
65
|
|
|
* @throws FailException |
66
|
|
|
* @throws SuccessException |
67
|
1 |
|
*/ |
68
|
|
|
protected function testFolder($path, $expectFolder, array $task) |
69
|
1 |
|
{ |
70
|
|
|
if (is_dir($path) === $expectFolder) { |
71
|
|
|
throw new SuccessException('Ok', $task); |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
$msg = $expectFolder ? "Path '{$path}' is not a folder'" : "Path '{$path}' is a folder"; |
75
|
|
|
|
76
|
2 |
|
throw new FailException($msg, $task); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
1 |
|
* @param string $path |
81
|
|
|
* @param boolean $expectWritable |
82
|
1 |
|
* @param array $task |
83
|
|
|
* |
84
|
|
|
* @throws FailException |
85
|
|
|
* @throws SuccessException |
86
|
|
|
*/ |
87
|
|
|
protected function testWritable($path, $expectWritable, array $task) |
88
|
|
|
{ |
89
|
|
|
if (is_writeable($path) === $expectWritable) { |
90
|
|
|
throw new SuccessException('Ok', $task); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$msg = $expectWritable ? "Path '{$path}' is not writable'" : "Path '{$path}' is writable"; |
94
|
|
|
|
95
|
|
|
throw new FailException($msg, $task); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $path |
100
|
|
|
* @param boolean $expectReadable |
101
|
|
|
* @param array $task |
102
|
|
|
* |
103
|
|
|
* @throws FailException |
104
|
|
|
* @throws SuccessException |
105
|
|
|
*/ |
106
|
|
|
protected function testReadable($path, $expectReadable, array $task) |
107
|
|
|
{ |
108
|
|
|
if (is_readable($path) === $expectReadable) { |
109
|
|
|
throw new SuccessException('Ok', $task); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$msg = $expectReadable ? "Path '{$path}' is not readable'" : "Path '{$path}' is readable"; |
113
|
|
|
|
114
|
|
|
throw new FailException($msg, $task); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $path |
119
|
|
|
* @param boolean $expectChmod |
120
|
|
|
* @param array $task |
121
|
|
|
* |
122
|
|
|
* @throws FailException |
123
|
|
|
* @throws SuccessException |
124
|
|
|
*/ |
125
|
|
|
protected function testChmod($path, $expectChmod, array $task) |
126
|
|
|
{ |
127
|
|
|
$chmod = substr(sprintf('%o', fileperms($path)), -4); |
128
|
|
|
|
129
|
|
|
if ($chmod === $expectChmod) { |
130
|
|
|
throw new SuccessException('Ok', $task); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$msg = "Path '{$path}' chmod expected: '{$expectChmod}', but have: '{$chmod}'"; |
134
|
|
|
|
135
|
|
|
throw new FailException($msg, $task); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $path |
140
|
|
|
* @param string $find |
141
|
|
|
* @param array $task |
142
|
|
|
* |
143
|
|
|
* @throws FailException |
144
|
|
|
* @throws SuccessException |
145
|
|
|
*/ |
146
|
|
|
protected function testContain($path, $find, array $task) |
147
|
|
|
{ |
148
|
|
|
$source = file_get_contents($path); |
149
|
|
|
|
150
|
|
|
if (strpos($source, $find) !== false) { |
151
|
|
|
throw new SuccessException('Ok', $task); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$msg = "Path '{$path}' must contain '{$find}'"; |
155
|
|
|
|
156
|
|
|
throw new FailException($msg, $task); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|