1
|
|
|
<?php |
2
|
|
|
namespace SeleniumSetup; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class System |
6
|
|
|
* @package SeleniumSetup\System |
7
|
|
|
* @todo Replace with http://symfony.com/doc/current/components/filesystem/introduction.html |
8
|
|
|
*/ |
9
|
|
|
class FileSystem implements FileSystemInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param $dirFullPath |
13
|
|
|
* @return bool |
14
|
|
|
*/ |
15
|
|
|
public function isDir($dirFullPath) |
16
|
|
|
{ |
17
|
|
|
return is_dir($dirFullPath); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $dirFullPath |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function createDir($dirFullPath) |
25
|
|
|
{ |
26
|
|
|
$makeDir = @mkdir($dirFullPath); |
27
|
|
|
if (!$makeDir) { |
28
|
|
|
throw new \RuntimeException(sprintf('Cannot create directory: %s', $dirFullPath)); |
29
|
|
|
} else { |
30
|
|
|
return true; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $fileFullPath |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function isFile($fileFullPath) |
39
|
|
|
{ |
40
|
|
|
return file_exists($fileFullPath); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $fileFullPath |
45
|
|
|
* @param string $contents |
46
|
|
|
*/ |
47
|
|
|
public function createFile($fileFullPath, $contents = '') |
48
|
|
|
{ |
49
|
|
|
if (!$this->isFile($fileFullPath)) { |
50
|
|
|
touch($fileFullPath); |
51
|
|
|
} |
52
|
|
|
$this->writeToFile($fileFullPath, $contents); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $fileFullPath |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function isWritable($fileFullPath) |
60
|
|
|
{ |
61
|
|
|
if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) { |
62
|
|
|
$handler = @fopen($fileFullPath, 'a'); |
63
|
|
|
if (!$handler) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
fclose($handler); |
67
|
|
|
return true; |
68
|
|
|
} else { |
69
|
|
|
if (!file_exists($fileFullPath)) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
return @is_writable($fileFullPath); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $fileFullPath |
78
|
|
|
* @param string $contents |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function writeToFile($fileFullPath, $contents = '') |
82
|
|
|
{ |
83
|
|
|
if (!$this->isWritable($fileFullPath)) { |
84
|
|
|
throw new \RuntimeException(sprintf('File %s is not writable.', $fileFullPath)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$handler = fopen($fileFullPath, 'w'); |
88
|
|
|
|
89
|
|
|
if (!$handler) { |
90
|
|
|
throw new \RuntimeException(sprintf('Cannot open %s file.', $fileFullPath)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$write = fwrite($handler, $contents); |
94
|
|
|
|
95
|
|
|
if ($write === false) { |
96
|
|
|
throw new \RuntimeException(sprintf('Cannot write to %s file.', $fileFullPath)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
fclose($handler); |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $fileFullPath |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function readFile($fileFullPath) |
109
|
|
|
{ |
110
|
|
|
$handler = fopen($fileFullPath, 'r'); |
111
|
|
|
|
112
|
|
|
if (!$handler) { |
113
|
|
|
throw new \RuntimeException(sprintf('Cannot open %s file.', $fileFullPath)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$contents = fread($handler, filesize($fileFullPath)); |
117
|
|
|
|
118
|
|
|
return $contents; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $fileFullPath |
123
|
|
|
* @return resource |
124
|
|
|
*/ |
125
|
|
|
public function openFileForReading($fileFullPath) |
126
|
|
|
{ |
127
|
|
|
return fopen($fileFullPath, 'r'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $handler |
132
|
|
|
* @param int $limit |
133
|
|
|
* @param string $separator |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function readFileLineAsCsv($handler, $limit = 0, $separator = '|') |
137
|
|
|
{ |
138
|
|
|
return fgetcsv($handler, $limit, $separator); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function rename($from, $to) |
142
|
|
|
{ |
143
|
|
|
return rename($from, $to); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|