1
|
|
|
<?php |
2
|
|
|
namespace Balloon\Reader; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class FileReader |
6
|
|
|
* @package Balloon\Reader |
7
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class FileReader implements IFileReader |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $filePath; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
private $useIncludePath; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var resource |
23
|
|
|
*/ |
24
|
|
|
private $context; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $exists = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $filePath |
33
|
|
|
* @param bool $useIncludePath |
34
|
|
|
* @param resource $context |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function __construct($filePath, $useIncludePath = false, $context = null) |
37
|
|
|
{ |
38
|
|
|
$this->setFilePath($filePath); |
39
|
|
|
$this->setUseIncludePath($useIncludePath); |
40
|
|
|
$this->setContext($context); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function read() |
47
|
|
|
{ |
48
|
|
|
if(!$this->isExistingFile()){ |
49
|
|
|
return ''; |
50
|
|
|
} |
51
|
|
|
return file_get_contents($this->getFilePath(), $this->useIncludePath(), $this->getContext()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $data |
56
|
|
|
* @param int $mode |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
|
|
public function write($data, $mode = 0) |
60
|
|
|
{ |
61
|
|
|
$this->createDir(); |
62
|
|
|
return file_put_contents($this->getFilePath(), $data, $mode, $this->getContext()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function isExistingFile() |
69
|
|
|
{ |
70
|
|
|
if (!$this->exists) { |
71
|
|
|
$this->exists = file_exists($this->getFilePath()); |
72
|
|
|
} |
73
|
|
|
return $this->exists; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function isExistingDir() |
80
|
|
|
{ |
81
|
|
|
if (!$this->exists) { |
82
|
|
|
$this->exists = is_dir($this->getDir()); |
83
|
|
|
} |
84
|
|
|
return $this->exists; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Getter of $filePath |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getFilePath() |
93
|
|
|
{ |
94
|
|
|
return $this->filePath; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getDir() |
101
|
|
|
{ |
102
|
|
|
return dirname($this->getFilePath()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Getter of $useIncludePath |
107
|
|
|
* |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
|
|
public function useIncludePath() |
111
|
|
|
{ |
112
|
|
|
return $this->useIncludePath; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Getter of $context |
117
|
|
|
* |
118
|
|
|
* @return resource |
119
|
|
|
*/ |
120
|
|
|
public function getContext() |
121
|
|
|
{ |
122
|
|
|
return $this->context; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Setter of $filePath |
127
|
|
|
* |
128
|
|
|
* @param string $filePath |
129
|
|
|
*/ |
130
|
|
|
private function setFilePath($filePath) |
131
|
|
|
{ |
132
|
|
|
$this->filePath = (string)$filePath; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Setter of $useIncludePath |
137
|
|
|
* |
138
|
|
|
* @param boolean $useIncludePath |
139
|
|
|
*/ |
140
|
|
|
private function setUseIncludePath($useIncludePath) |
141
|
|
|
{ |
142
|
|
|
$this->useIncludePath = (boolean)$useIncludePath; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Setter of $context |
147
|
|
|
* |
148
|
|
|
* @param resource $context |
149
|
|
|
*/ |
150
|
|
|
private function setContext($context) |
151
|
|
|
{ |
152
|
|
|
$this->context = $context; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
*/ |
158
|
|
|
private function createDir() |
159
|
|
|
{ |
160
|
|
|
if (!$this->isExistingDir()) { |
161
|
|
|
mkdir($this->getDir(), 0777, true); |
162
|
|
|
$this->exists = true; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.