|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2015 zepi |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
15
|
|
|
* all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23
|
|
|
* THE SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The FileBackend saves and loads the content in and from a file. |
|
29
|
|
|
* |
|
30
|
|
|
* @package Zepi\Turbo\Backend |
|
31
|
|
|
* @author Matthias Zobrist <[email protected]> |
|
32
|
|
|
* @copyright Copyright (c) 2015 zepi |
|
33
|
|
|
*/ |
|
34
|
|
|
|
|
35
|
|
|
namespace Zepi\Turbo\Backend; |
|
36
|
|
|
|
|
37
|
|
|
use \Zepi\Turbo\Exception; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The FileBackend saves and loads the content in and from a file. |
|
41
|
|
|
* |
|
42
|
|
|
* @author Matthias Zobrist <[email protected]> |
|
43
|
|
|
* @copyright Copyright (c) 2015 zepi |
|
44
|
|
|
*/ |
|
45
|
|
|
class FileBackend |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @access protected |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $path; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Constructs the object |
|
55
|
|
|
* |
|
56
|
|
|
* @access public |
|
57
|
|
|
* @param string $path |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct($path) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->path = $path; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Saves the content to the file |
|
66
|
|
|
* |
|
67
|
|
|
* @access public |
|
68
|
|
|
* @param string $content |
|
69
|
|
|
* @param string $additionalPath |
|
70
|
|
|
* @return integer |
|
71
|
|
|
* |
|
72
|
|
|
* @throws Zepi\Turbo\Exception The file "$path" isn't writable! |
|
73
|
|
|
*/ |
|
74
|
|
|
public function saveToFile($content, $additionalPath = '') |
|
75
|
|
|
{ |
|
76
|
|
|
$path = $this->realPath($additionalPath); |
|
77
|
|
|
|
|
78
|
|
|
// If the path does not exists create the directory |
|
79
|
|
|
$this->createTargetDirectory($path); |
|
80
|
|
|
|
|
81
|
|
|
// If the file exists but isn't writeable we need to throw an exception |
|
82
|
|
|
if (file_exists($path) && !is_writable($path)) { |
|
83
|
|
|
throw new Exception('The file "' . $path . '" isn\'t writable!'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return file_put_contents($path, $content); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Loads the content from the file |
|
91
|
|
|
* |
|
92
|
|
|
* @access public |
|
93
|
|
|
* @param string $additionalPath |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function loadFromFile($additionalPath = '') |
|
97
|
|
|
{ |
|
98
|
|
|
$path = $this->testPath($this->realPath($additionalPath)); |
|
99
|
|
|
|
|
100
|
|
|
if ($path === false) { |
|
101
|
|
|
return ''; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return file_get_contents($path); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Deletes a file on the hard disk. |
|
109
|
|
|
* |
|
110
|
|
|
* @access public |
|
111
|
|
|
* @param string $additionalPath |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
public function deleteFile($additionalPath = '') |
|
115
|
|
|
{ |
|
116
|
|
|
$path = $this->testPath($this->realPath($additionalPath)); |
|
117
|
|
|
|
|
118
|
|
|
if ($path === false) { |
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return unlink($path); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns true if a file or a directory exists and |
|
127
|
|
|
* is writable. Otherwise return false. |
|
128
|
|
|
* |
|
129
|
|
|
* @access public |
|
130
|
|
|
* @param string $additionalPath |
|
131
|
|
|
* @return boolean |
|
132
|
|
|
*/ |
|
133
|
|
|
public function isWritable($additionalPath = '') |
|
134
|
|
|
{ |
|
135
|
|
|
$path = $this->realPath($additionalPath); |
|
136
|
|
|
|
|
137
|
|
|
if ($path === false || !is_writable($path)) { |
|
138
|
|
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns the real path for the given additional path and the |
|
146
|
|
|
* file path which was given to the backend in the constructor. |
|
147
|
|
|
* |
|
148
|
|
|
* @access public |
|
149
|
|
|
* @param string $additionalPath |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function realPath($additionalPath) |
|
153
|
|
|
{ |
|
154
|
|
|
if (substr($additionalPath, 0, 1) === '/') { |
|
155
|
|
|
$path = $additionalPath; |
|
156
|
|
|
} else if ($additionalPath !== '') { |
|
157
|
|
|
$path = $this->path . $additionalPath; |
|
158
|
|
|
} else { |
|
159
|
|
|
$path = $this->path; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $path; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* If the path exists return the path. Otherwise return false |
|
167
|
|
|
* or throw an exception. |
|
168
|
|
|
* |
|
169
|
|
|
* @access protected |
|
170
|
|
|
* @param string $path |
|
171
|
|
|
* @return false|string |
|
172
|
|
|
* |
|
173
|
|
|
* @throws \Zepi\Turbo\Exception The file path "{path}" is not readable and not writeable! |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function testPath($path) |
|
176
|
|
|
{ |
|
177
|
|
|
if (!file_exists($path)) { |
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (!is_readable($path) && !is_writeable($path)) { |
|
182
|
|
|
throw new Exception('The file path "' . $path . '" is not readable and not writeable!'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $path; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Creates the target directory |
|
190
|
|
|
* |
|
191
|
|
|
* @access protected |
|
192
|
|
|
* @param string $path |
|
193
|
|
|
* |
|
194
|
|
|
* @throws \Zepi\Turbo\Exception The directory "{directory}" doesn't exists |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function createTargetDirectory($path) |
|
197
|
|
|
{ |
|
198
|
|
|
$directory = dirname($path); |
|
199
|
|
|
if (!file_exists($directory)) { |
|
200
|
|
|
if (!is_writeable($this->getExistingPath($directory))) { |
|
201
|
|
|
throw new Exception('The directory "' . $directory . '" isn\'t writeable!'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
mkdir($directory, 0755, true); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Returns the existing part of a path |
|
210
|
|
|
* |
|
211
|
|
|
* @access protected |
|
212
|
|
|
* @param string $path |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function getExistingPath($path) |
|
216
|
|
|
{ |
|
217
|
|
|
$numberOfSlashes = substr_count($path, '/'); |
|
218
|
|
|
$existingPath = $path; |
|
219
|
|
|
|
|
220
|
|
|
for ($i = 0; $i < $numberOfSlashes; $i++) { |
|
221
|
|
|
if (!file_exists($existingPath)) { |
|
222
|
|
|
$existingPath = dirname($existingPath); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $existingPath; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|